千家信息网

Java中mybatis-plus怎么用

发表于:2024-10-10 作者:千家信息网编辑
千家信息网最后更新 2024年10月10日,这篇文章主要为大家展示了"Java中mybatis-plus怎么用",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Java中mybatis-plus怎么用"
千家信息网最后更新 2024年10月10日Java中mybatis-plus怎么用

这篇文章主要为大家展示了"Java中mybatis-plus怎么用",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Java中mybatis-plus怎么用"这篇文章吧。

    1、简介

    MyBatis-Plus 是一个 Mybatis 增强版工具,在 MyBatis 上扩充了其他功能没有改变其基本功能,为了简化开发提交效率而存在。

    2、适用情况

    1、对于只进行单表操作来说,mybatis-plus代码量比mybatis的代码量少很多,极大的提高了开发效率
    2、对于多表操作来说,更推荐mybatis,因为mybatis-plus的方法比较难以理解,用起来不太方便,不如自己写sql语句的逻辑那么清晰明了

    3、mybatis-plus前期准备(工程将以 H2 作为默认数据库进行演示)

    1、使用 Spring Initializer快速初始化一个 Spring Boot 工程

    2、导入mybatis-plus依赖

        org.springframework.boot    spring-boot-starter-parent    spring-latest-version                org.springframework.boot        spring-boot-starter                org.springframework.boot        spring-boot-starter-test        test                com.baomidou        mybatis-plus-boot-starter        Latest Version                com.h3database        h3        runtime    

    3、yml文件中添加相关配置

    # DataSource Configspring:  datasource:    driver-class-name: org.h3.Driver    schema: classpath:db/schema-h3.sql    data: classpath:db/data-h3.sql    url: jdbc:h3:mem:test    username: root    password: test

    4、在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹

    @MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")

    5、编写实体类和Mapper类

    //entity@Datapublic class User {    private Long id;    private String name;    private Integer age;    private String email;}//Mapperpublic interface UserMapper extends BaseMapper {}

    6、service继承IService

    public interface UserService extends IService

    7、serviceImpl继承ServiceImpl

    public class UserServiceImpl extends ServiceImpl implements UserService

    4、mybatis-plus的sql操作(Service层)

    1、Save:插入

    // 插入一条记录(选择字段,策略插入)boolean save(T entity);// 插入(批量)boolean saveBatch(Collection entityList);// 插入(批量)boolean saveBatch(Collection entityList, int batchSize);


    2、SaveOrUpdate:修改插入

    // TableId 注解存在更新记录,否插入一条记录boolean saveOrUpdate(T entity);// 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法boolean saveOrUpdate(T entity, Wrapper updateWrapper);// 批量修改插入boolean saveOrUpdateBatch(Collection entityList);// 批量修改插入boolean saveOrUpdateBatch(Collection entityList, int batchSize);


    3、Remove:删除

    // 根据 entity 条件,删除记录boolean remove(Wrapper queryWrapper);// 根据 ID 删除boolean removeById(Serializable id);// 根据 columnMap 条件,删除记录boolean removeByMap(Map columnMap);// 删除(根据ID 批量删除)boolean removeByIds(Collection idList);

    4、Update:更新

    // 根据 UpdateWrapper 条件,更新记录 需要设置sqlsetboolean update(Wrapper updateWrapper);// 根据 whereWrapper 条件,更新记录boolean update(T updateEntity, Wrapper whereWrapper);// 根据 ID 选择修改boolean updateById(T entity);// 根据ID 批量更新boolean updateBatchById(Collection entityList);// 根据ID 批量更新boolean updateBatchById(Collection entityList, int batchSize);


    5、Get:单体查询

    // 根据 ID 查询T getById(Serializable id);// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")T getOne(Wrapper queryWrapper);// 根据 Wrapper,查询一条记录T getOne(Wrapper queryWrapper, boolean throwEx);// 根据 Wrapper,查询一条记录Map getMap(Wrapper queryWrapper);// 根据 Wrapper,查询一条记录 V getObj(Wrapper queryWrapper, Function mapper)


    6、List:多条查询

    // 查询所有List list();// 查询列表List list(Wrapper queryWrapper);// 查询(根据ID 批量查询)Collection listByIds(Collection idList);// 查询(根据 columnMap 条件)Collection listByMap(Map columnMap);// 查询所有列表List> listMaps();// 查询列表List> listMaps(Wrapper queryWrapper);// 查询全部记录List listObjs();// 查询全部记录 List listObjs(Function mapper);// 根据 Wrapper 条件,查询全部记录List listObjs(Wrapper queryWrapper);// 根据 Wrapper 条件,查询全部记录 List listObjs(Wrapper queryWrapper, Function mapper);


    7、Page:分页查询(需要导入相关的配置或依赖)

    // 无条件分页查询IPage page(IPage page);// 条件分页查询IPage page(IPage page, Wrapper queryWrapper);// 无条件分页查询IPage> pageMaps(IPage page);// 条件分页查询IPage> pageMaps(IPage page, Wrapper queryWrapper);


    8、Count:记录数据个数

    // 查询总记录数int count();// 根据 Wrapper 条件,查询总记录数int count(Wrapper queryWrapper);

    以上是"Java中mybatis-plus怎么用"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

    0