增强插件流行的两种
- tk.mybatis
- mybatis plus
注意:侵入性强的代码,劲量少用,不然后期维护会很麻烦(一旦增强插件不维护了)
Mybatis-Plsh
官网: https://mp.baomidou.com/guide/#%E7%89%B9%E6%80%A7
优点
- 侵入性小,损耗小
- 通过CRUD操作,支持分页
- 预防SQL注入,支持多种数据区
- 支持ActiveRecord支持代码生产等等
导入包
<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>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--主要包mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
配置:
yml 配置
# DataSource Config
spring:
datasource:
driver-class-name: org.h2.Driver
schema: classpath:db/schema-h2.sql
data: classpath:db/data-h2.sql
url: jdbc:h2:mem:test
username: root
password: test
在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:
@SpringBootApplication
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(QuickStartApplication.class, args);
}
}
编写Mapper类 UserMapper.java继承BaseMapper<对应的泛型>
public interface UserMapper extends BaseMapper<User> {
}
Mybatis-Plsh 分页查询
https://mybatis.plus/guide/page.html
Mybatis-Plsh 乐观锁
Mybatis-Plsh Mapper层接口
https://mp.baomidou.com/guide/crud-interface.html#mapper-crud-%E6%8E%A5%E5%8F%A3