<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.6.6version>
<relativePath/>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.28version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.2.8version>
dependency>
dependencies>
spring:
datasource:
username: 账户
password: 密码
url: jdbc:mysql://连接地址:3306/数据库名称?useUnicode=true&characterEncoding=utf8
driver-class-name: com.mysql.cj.jdbc.Driver
1、配置分页
2、配置乐观锁
@Configuration //配置类
@EnableTransactionManagement //开启事务管理器
@MapperScan(basePackages = "扫描的Mapper包名")
public class MybatisConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
//设置Mybatis拦截器
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//设置分页拦截器类型---(数据库类型)
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
//配置溢出总页数后是否进行处理
paginationInnerInterceptor.setOverflow(true);
//单页分页条数限制
paginationInnerInterceptor.setMaxLimit(1000L);
//其他配置参照分页插件拦截器源码
interceptor.addInnerInterceptor(paginationInnerInterceptor);
//设置乐观锁
/*
1、支持的数据类型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime
2、整数类型下 newVersion = oldVersion + 1 newVersion 会回写到 entity 中
3、仅支持 updateById(id) 与 update(entity, wrapper) 方法
4、在 update(entity, wrapper) 方法下, wrapper 不能复用!!!
*/
OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor = new OptimisticLockerInnerInterceptor();
interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor);
//防止全表更新插件
BlockAttackInnerInterceptor blockAttackInnerInterceptor = new BlockAttackInnerInterceptor();
interceptor.addInnerInterceptor(blockAttackInnerInterceptor);
return interceptor;
}
}
//Dao/Mapper
@Mapper
public interface 实体类Entity extends BaseMapper<你的数据Entity> {
//实体类属性
}
//Service
//Interface
public interface Service名称 extends IService<实体类Entity> {
//Service提供的方法接口定义
}
//ServiceImpl
/*
源码IService
IService 实现类( 泛型:M 是 mapper 对象,T 是实体 )
public class ServiceImpl, T> implements IService {
}
*/
@Service("注入的ServiceBean名称")
public class ServiceImpl名称 extends ServiceImpl<Mapper对象, 实体类Entity> implements Service名称 {
//Service实现类
}
@TableName
@TableId
@Version
@Verison 在字段上@EnumValue
@TableLogic
| 属性 | 类型 | 必须指定 | 默认值 | 描述 |
|---|---|---|---|---|
| value | String | 否 | “” | 逻辑未删除值 |
| delval | String | 否 | “” | 逻辑删除值 |
@KeySequence
oracle| 属性 | 类型 | 必须指定 | 默认值 | 描述 |
|---|---|---|---|---|
| value | String | 否 | “” | 序列名 |
| clazz | Class | 否 | Long.class | id 的类型, 可以指定 String.class,这样返回的 Sequence 值是字符串"1" |
| 属性 | 类型 | 必须指定 | 默认值 | 描述 |
|---|---|---|---|---|
| isDesc | boolean | 否 | true | 是否倒序查询 |
| sort | short | 否 | Short.MAX_VALUE | 数字越小越靠前 |