MyBatisPlus学习总结 1.入门案例 1.1开发环境 IDE:idea 2019.2 JDK:JDK8+ 构建工具:maven 3.5.4 MySQL版本:MySQL 5.7 Spring Boot:2.6.3 MyBatis-Plus:3.5.1
1.2初始化SpringBoot工程 使用 Spring Initializr 快速初始化一个 Spring Boot 工程
1.3引入依赖
<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 > 3.5.1</version > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <optional > true</optional > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <scope > runtime</scope > </dependency > </dependencies >
1.4编写代码 1.4.1配置application.yml spring: datasource: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf8&useSSL=false username: root password: 123456
1、驱动类driver-class-name spring boot 2.0(内置jdbc5驱动),驱动类使用: driver-class-name: com.mysql.jdbc.Driver spring boot 2.1及以上(内置jdbc8驱动),驱动类使用: driver-class-name: com.mysql.cj.jdbc.Driver 否则运行测试用例的时候会有 WARN 信息 2、连接地址url MySQL5.7版本的url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false MySQL8.0版本的url: jdbc:mysql://localhost:3306/mybatis_ plus?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false 否则运行测试用例报告如下错误: java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more
1.4.2SpringBoot启动类添加@MapperScan注解 @SpringBootApplication @MapperScan("com.atguigu.mybatisplus.mapper") public class MybatisplusApplication { public static void main (String[] args) { SpringApplication.run(MybatisplusApplication.class, args); } }
1.4.3测试 @SpringBootTest public class MybatisPlusTest {@Autowired private UserMapper userMapper;@Test public void testSelectList () {userMapper.selectList(null ).forEach(System.out::println); } }
IDEA在 userMapper 处报错,因为找不到注入的对象,因为类是动态创建的,但是程序可以正确 的执行。 为了避免报错,可以在mapper接口上添加 @Repository 注解 用于解决@Autowired注入时爆红,但是可以运行。mapper上添加@mapper是为了让MyBatis找到mapper实现创建数据动态代理对象,查询数据库,可以在springboot启动类上添加mapperscan来代替mapper的繁琐。
1.4.4添加日志 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
2.常用注解 2.1@TableName 经过以上的测试,在使用MyBatis-Plus实现基本的CRUD时,我们并没有指定要操作的表,只是在 Mapper接口继承BaseMapper时,设置了泛型User,而操作的表为user表 由此得出结论,MyBatis-Plus在确定操作的表时,由BaseMapper的泛型决定,即实体类型决 定,且默认操作的表名和实体类型的类名一致
2.1.1实体类的类名和操作表的表名不一致产生问题
2.1.2通过添加@TableName解决问题
2.1.3通过全局配置解决问题 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: table-prefix: t_
在开发的过程中,我们经常遇到以上的问题,即实体类所对应的表都有固定的前缀,例如t_或tbl_ 此时,可以使用MyBatis-Plus提供的全局配置,为实体类所对应的表名设置默认的前缀,那么就 不需要在每个实体类上通过@TableName标识实体类对应的表。后文:利用MyBatisX插件自动逆向工程,生成表的时候就可以指定省略前缀t_或者后缀,suffix后缀,prefix前缀。
2.2@TableId 经过以上的测试,MyBatis-Plus在实现CRUD时,会默认将id作为主键列,并在插入数据时,默认 基于雪花算法的策略生成id 若实体类和表中表示主键的不是id,而是其他字段,例如uid,MyBatis-Plus会自动识别uid为主 键列吗? 我们实体类中的属性id改为uid,将表中的字段id也改为uid,测试添加功能
2.2.1通过@TableId解决问题
2.2.2@TableId的value属性 场景:若实体类中主键对应的属性为id,而表中表示主键的字段为uid,此时若只在属性id上添加注解 @TableId,则抛出异常Unknown column 'id' in 'field list',即MyBatis-Plus仍然会将id作为表的主键操作,而表中表示主键的是字段uid 此时需要通过@TableId注解的value属性,指定表中的主键字段,@TableId("uid")或 @TableId(value="uid")
2.2.3@TableId的type属性 1.常用的主键策略
2.配置全局主键策略 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: table-prefix: t_ id-type: auto
x雪花算法: 长度共64bit(一个long型)。 首先是一个符号位,1bit标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负 数是1,所以id一般是正数,最高位是0。 41bit时间截(毫秒级),存储的是时间截的差值(当前时间截 - 开始时间截),结果约等于69.73年。 10bit作为机器的ID(5个bit是数据中心,5个bit的机器ID,可以部署在1024个节点)。 12bit作为毫秒内的流水号(意味着每个节点在每毫秒可以产生 4096 个 ID)。
2.3@TableField 经过以上的测试,我们可以发现,MyBatis-Plus在执行SQL语句时,要保证实体类中的属性名和 表中的字段名一致 如果实体类中的属性名和字段名不一致的情况,会出现什么问题呢?
2.3.1下划线和驼峰情况 若实体类中的属性使用的是驼峰命名风格,而表中的字段使用的是下划线命名风格 例如实体类属性userName,表中字段user_name 此时MyBatis-Plus会自动将下划线命名风格转化为驼峰命名风格 相当于在MyBatis中配置
2.3.2实体类属性名和表中属性名不一致(不属于第一种情况) 若实体类中的属性和表中的字段不满足情况1 例如实体类属性name,表中字段username 此时需要在实体类属性上使用@TableField("username")设置属性所对应的字段名
2.4@TableLogic 2.4.1逻辑删除 物理删除:真实删除,将对应数据从数据库中删除,之后查询不到此条被删除的数据 逻辑删除:假删除,将对应数据中代表是否被删除字段的状态修改为“被删除状态”,之后在数据库 中仍旧能看到此条数据记录 使用场景:可以进行数据恢复
2.4.2实现逻辑删除
如果这篇博客对您有帮助,可以点个赞支持一下博主,感谢!