SpringBoot学习总结(三)-MybatisPlus

277 阅读3分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 4 天,点击查看详情

前几期讲述了spring boot中部分基础及配置相关知识,本文将着重总结MybatisPlus相关知识;

介绍

MybatisPlus是当前市面应用较为广泛的mybatis的增强工具,特点是只在MyBatis基础上进行增强不做改变,引入它对现有项目不会产生影响,启动后便会自动注入基础增删查改操作,性能损耗小,内置通用Mapper,Service可以通过少量配置实现大部分基础操作及大部分需求;支持lambda表达式,更加方便实现各种查询条件,支持主键自增等多种主键策略,还包含了代码生成器,分页插件,性能分析插件,全局拦截插件,支持各种标准SQL数据库操作;

简单使用引入

1:使用idea快速创建springboot项目;

2:添加依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>Latest Version</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

3:在主启动类添加注解,扫描mapper文件夹下文件

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

4:编写实体类与mapper,实体类使用lombok注解,mapper需要继承BaseMapper

public interface ModelMapper extends BaseMapper<Model> {

}

5:编写测试代码进行测试

    @Autowired
    private ModelMapper modelMapper;

    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<Model> userList = modelMapper.selectList(null);
        Assert.assertEquals(5, userList.size());
        userList.forEach(System.out::println);
    }

完成这几步基本上基础的CURD都已经可以实现,还可以通过Lambda表达式完成各种筛选与较为复杂的操作;

下面介绍一些常用注解:

@TableName("table_name") 该注解标注在实体类上,用来标识BaseMapper引用的泛型从而确定当前sql语句操作的表名,mybatis-plus同时还提供了全局配置用来配置表名的默认前缀(table-prefix);

@TableId(value='UID') 该注解用来标注在实体类主键字段上,用来标识该字段为主键id,常用两种主键策略:auto(自增),ASSIGN_ID(基于雪花算法生成的id);同时可以在全局配置中配置主键策略(id-type);

@TableField(exist/'value') 该注解用来标注在实体类字段,内容为字段名称时标识实体类属性与表字段关系, 当内容为exist=false标识该字段不存在与该实体类对应的表上;

@TableLogic 该注解标注在实体类字段标识该字段为逻辑删除字段,mybatisPlus执行sql语句时会自动添加过滤逻辑删除的sql语句;

@Version 该注解标注在实体类中标识该字段为乐观锁字段,可以自动生成乐观锁,操作数据时先取出数据获取当前字段,更新时该字段+1,如果该字段不对应,更新失败;

分页实现
添加分页配置类
@Configuration
@MapperScan("com.mybatisplus.mapper")
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}
//通过以下代码可以查看是否实现分页
Page<User> page = new Page<>(2, 5);
userMapper.selectPage(page,null);
多表联查

我们在日常开发中经常遇到多表联查的功能或需求,mp中暂时没有相关可以直接应用的查询方式,最近出现了一个非mybatisPlus官方维护的MyBatisPlus-join,方法是重写于MybatisPlus的list,one,count,page,这几个方法使用起来基本于原来一致,条件构造器定义的对象为主表对象;

简单实例:

//编写配置类文件
@Configuration
public class MyBatisPlusConfig extends JoinDefaultSqlInjector {
  @Override
  public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
      return  super.getMethodList(mapperClass);
  }
}

构造器使用: 第一步new一个JoinLambdaWrapper构造参数是主表的实体对象

JoinLambdaWrapper<User> wrapper = new JoinLambdaWrapper<>(User.class);

第二步 使用leftJoin方法创建一个左连,第一个参数是join中定义的连接的表,第二个参数是随意的表,但是是要出现构造器中的表

wrapper.leftJoin(UserAge.class,UserAge::getId,User::getAgeId);

然后可以设置多表中的查询条件,这一步和mybatisPlus使用时一致,使用end方法结束

wrapper.eq(UserAge::getAgeName,"18")
       .select(UserAge::getAge)
      .end();
      

该构造器执行SQL如下:

select 
  user.user_id,
  user.user_name,
  user_age.age_name
from user user
  left join user_age user_age on user_age.id = user.age_id
where (
	user_age.age = '18'
)

在使用MybatisPlusJoin时所有参数不能为null,类似于将MybatisPlus中构造器多个维护到一起使用,使用熟练之后还是比较方便的;

代码生成模板