Spring boot --Mybatis Plus使用笔记 (一)
前言
开发环境是 spring boot 结合 Mybatis Plus 使用!
本教程重点在快速使用crud,基本入门内容请查阅官网!
Mybatis Plus官网请移步 https://baomidou.com/
一.Mybatis Plus 配置
maven 导入jar包坐标
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
文件配置,使用的是 application.properties
mybatis-plus
# 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/repository/*Mapper.xml
# 如果是放在resource目录 classpath:/repository/*Mapper.xml
mybatis-plus.mapper-locations=classpath*:/mapper/*/*Mapper.xml
# 实体扫描,多个 package 用逗号或分号分开
#mybatis-plus.type-aliases-package=com.qingqiuyue.jrqxsysserver.domain
#主键类型,AUTO:"数据库ID自增", INPUT:"用户输入ID",id_worker:"全局唯一ID (数字类型唯一ID)", uuid:"全局唯一ID UUID";
mybatis-plus.global-config.db-config.id-type=uuid
#字段策略 ignored:"忽略判断",not_null:"非 NULL 判断"),not_empty:"非空判断"
mybatis-plus.global-config.db-config.field-strategy=not_null
# 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
mybatis-plus.configuration.map-underscore-to-camel-case=true
# 如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
mybatis-plus.configuration.call-setters-on-nulls=true
# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
引入分页插件类
该类必须在启动类平级或者平级目录下,与Swagger2配置类位置一致即可
@Configuration
//设置mapper接口的扫描包
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
二.实体类注解配置
@TableName("test")
@Data
public class Test {
private String id;
private String name;
}
@TableName 表示数据库表名,一般表名都是小写,且用下画线分割,与Java类命名规范不符,所以一般会配置。
@TableId 表示配置id字段,上面我已经配置了全局的主键类型默认值和自动驼峰命名规则映射,所以我可以不配置id名和值,如果你没有配置全局的,就要单独配置。值的类型与数据库一致,保证在使用mybatis plus 的插入id值时不报错,具体值类型可以参考官网设置
@TableField 表示字段注解,上面我配置了开启自动驼峰命名规则映射,所以只要是按照驼峰命名规则的字段可以不使用该注解
该实体类继承Model<实体类名>,直接使用单表的crud,使用之前dao层要写,不然会报空指针,dao层写法详情参考第二篇笔记,现在只给代码。
public interface TestDao extends BaseMapper<Test> {
}
@TableName("test")
@Data
public class Test extends Model<Test> {
private String id;
private String name;
//重写pkVal方法,返回主键
@Override
protected Serializable pkVal() {
return id;
}
}
创建Test对象后,可以直接利用该对象的api进行crud
Test test = new Test ();
test.setName("test_name");
//没有id直接插入
test.insert();
//根据id更新
test.setId(1);
test.setName("test_name2");
test.updateById();
//直接使用updata(),需要写条件构造器
//QueryWrapper、LambdaQueryWrapper 3.0版本以上使用的两种构造器稍后介绍
test.update()
//查询所有
List<Test> tests = test.selectAll();
//根据条件查询
List<Test> tests = test.selectList(new QueryWrapper<Test>().like("name","test"));
//根据条件的查询总数
int result = test.selectCount(new QueryWrapper<Test>().eq("name","test_name"));
//根据id删除数据
boolean result = test.deleteById(1);
//或者这样写
//test.setId(1);
boolean result = test.deleteById();
//根据条件删除
boolean result = test.delete(new QueryWrapper<Test>().like("name","test"));
//分页查询
QueryWrapper<Test> wrapper = new QueryWrapper();wrapper.like("name", "test");
//new Page(当前页码,每页显示个数)
Page<Test> page = new Page<>(1,2);
IPage<Map<String, Object>> mapIPage = test.selectPage(page, wrapper);
System.out.println("总页数"+mapIPage.getPages());
System.out.println("总记录数"+mapIPage.getTotal());
List<Map<String, Object>> records = mapIPage.getRecords();
QueryWrapper
根据实体类创建queryWrapper对象,查哪一个实体类,就创建基于该实体类的对象
QueryWrapper<Test> queryWrapper = new QueryWrapper<>();
然后根据查询条件写构造器 前面写数据库真实列名,后面是值
例如 eq ="值" ;like like"%值%"
wrapper.like("name", "test");
LambdaQueryWrapper
使用LambdaQueryWrapper的好处在于,不用去查询数据库列名,直接使用实体类的get方法,不容易发生拼写错误
LambdaQueryWrapper<Test> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(Test::getName,"test");
总结
第一篇讲解了mybatis plus的入门配置和实体类层的crud,可以帮我们进行快速简单查询,后面讲解mapper和service层的快速crud