mybatis-plus
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
1、添加依赖:
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
(1)、application.yml中的mybatis-plus参数配置
mybatis-plus:
mapper-locations: classpath:mapper/*.xml #xml所在位置 不设置默认是在mapper类同级
type-aliases-package: com.example.demo.*.entity #对应的实体类
configuration:
mapUnderscoreToCamelCase: true # 开启驼峰匹配 默认为true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 打印sql语句和入参数据
global-config:
db-config:
logic-delete-value: 1 #逻辑删除 配合@TableLogic注解
logic-not-delete-value: 0 #逻辑不删除
update-strategy: not_null # 更新时字段如果为null,就不进行更新该字段。
insert-strategy: not_null # 插入时如果字段为null,就不插入数据,建议数据库表字段设置默认值
2、添加分页和mapper扫描
启动类
在启动类添加 @MapperScan 就不用再 UserDao 上用 @Mapper 注解。
@SpringBootApplication
@MapperScan("cn.rj.springboot.dao")
public class SpringBootMybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisPlusApplication.class, args);
}
}
分页
@Configuration
public class MybatisPlusConfiguration {
/**
* mybatis-plus分页插件
*/
@Bean
public PaginationInterceptor