一、概念
Mybatis-plus是Mybatis的增强工具,在Mybatis的基础上只做增强不做改变
二、特性
- 无侵入:只做增强不做改变
- 损耗小:启动自动注入基本CURD
- 强大的CURD操作:内置通用Mapper、通用Service
- 支持Lambda形式调用
- 支持主键自动生成
- 支持自定义全局通用操作
- 内置代码生成器
- 内置分页插件
- 分页插件支持多种数据库
- 内置全局拦截器
三、使用
1)引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
2)Mybatis-plus在application.yml中的配置
mybatis-plus: mapper-locations: classpath*:mapper/*Mapper.xml type-aliases-package: com.zs.mybatis_plus.entity
3)dao层
@Mapper
public interface UserDao extends BaseMapper<User> {
}
4)service层
public interface UserService extends IService<User> {
}
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
}
四、常用注解
@TableName:映射表明,当实体类与表名不一致时可使用该注解
@TableId:主键注解,参数type=IdType.AUTO
@TableField:字段注解,当实体类字段与表中字段不一致时可使用该注解
@TableLogic:字段逻辑删除
五、分页插件
1)添加分页插件
@Configuration
public class MybatisPlusInterceptorConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return mybatisPlusInterceptor;
}
}
2)进行分页
Controller层
@RequestMapping("/list")
@ResponseBody
public List<User> list(Integer page, Integer limit){
List<User> list = userService.selectAllByPage(page, limit);
return list;
}
Service层
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public List<User> selectAllByPage(Integer page, Integer limit) {
Page<User> userPage = new Page<>(page, limit);
//分页
Page<User> p = this.page(userPage,null);
//得到分页后数据
List<User> userList = p.getRecords();
return userList;
}
finally:遇见bug
Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.16.
解决方案:
六、代码生成器
1)引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2)自定义代码生成
public class generator {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/demo?useSSL=false";
String username = "root";
String password = "20001002qql";
FastAutoGenerator.create(url,username,password)
//全局配置
.globalConfig(builder -> {
builder.author("zhang") //作者
//.enableSwagger() //生成接口文档
.fileOverride() //覆盖
.outputDir("D://path"); //输出地址
})
//包配置
.packageConfig(builder -> {
builder.parent("com.zs") //父包名
.moduleName("mybatis_plus") //模块名
.pathInfo(Collections.singletonMap(OutputFile.xml,"D://path")); //mapper.xml生成路径
})
//策略配置
.strategyConfig(builder -> {
builder.addExclude("product") //需要生成的表名
.addTablePrefix("sys_","t_"); //过滤前缀
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板
.execute();
}
}
3)交互式生成
public class Generator01 {
public static void main(String[] args) {
FastAutoGenerator.create("jdbc:mysql://localhost:3306/demo", "root", "20001002qql")
// 全局配置
.globalConfig((scanner, builder) ->
builder.author(scanner.apply("请输入作者名称?"))
.fileOverride()
.outputDir("D://path")
)
// 包配置
.packageConfig((scanner, builder) ->
builder.parent(scanner.apply("请输入包名?"))
.moduleName(scanner.apply("请输入模块名?"))
.pathInfo(Collections.singletonMap(OutputFile.xml, "D://path"))
)
// 策略配置
.strategyConfig((scanner, builder) ->
builder.addInclude(getTables(scanner.apply("请输入表名,多个英文逗号分隔?所有输入 all")))
.controllerBuilder()
.enableRestStyle()
.enableHyphenStyle()
.entityBuilder()
.enableLombok()
.addTableFills(newColumn("create_time",FieldFill.INSERT)).build())
.templateEngine(new FreemarkerTemplateEngine())
.execute();
}
// 处理 all 情况
protected static List<String> getTables(String tables) {
return "all".equals(tables) ? Collections.emptyList() :
Arrays.asList(tables.split(","));
}
}