spring-boot整合mybatis-plus
- 导包(项目中同时需要使用mybatis和mybatis-plus只需要导这一个包就可以了)
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
配置yml文件(这里有一个sql日志打印需要注意一下)
# mybatis-plus
mybatis-plus:
mapper-locations: classpath*:/mappers/**/*.xml #扫描xml文件
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.example.demo.model
global-config:
#主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
id-type: 2
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
field-strategy: 2
#驼峰下划线转换
db-column-underline: true
#刷新mapper 调试神器
refresh-mapper: true
#数据库大写下划线转换
capital-mode: true
#序列接口实现类配置
#key-generator: com.baomidou.springboot.xxx
#逻辑删除配置(下面3个配置)
logic-delete-value: 0
logic-not-delete-value: 1
#自定义SQL注入器
#sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector
#自定义填充策略接口实现
#meta-object-handler: com.baomidou.springboot.xxx
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
# 日志打印 prod 要關閉
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
启动类扫描
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
@EnableScheduling
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
mybatis-plus的分页需要一个分页的配置
@Configuration
@MapperScan("com.baomidou.cloud.service.*.mapper*")
public class MybatisPlusConfig {
// 最新版
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
}
使用示例:
Page<SysUser> page = this.page(new Page<>(dto.getPage(),dto.getSize()), new LambdaQueryWrapper<SysUser>()
.eq(StringUtils.isNotEmpty(dto.getId()), SysUser::getId, dto.getId()));
控制台日志
备注:持续更新