Spring Boot集成Mybatis-plus
1.为什么是MybtisPlus而不是Mybatis?
笔者在之前的项目中大部分都是使用的SSM框架,其中就大量的用到Mybatis。Mybatis在持久层框架中还是比较的流行。主要是因为Mybatis可以直接的通过xml文件中的SQL语句操作数据库,显得很灵活、方便。但也是因为这个原因,如果业务较大就必须要写大量的xml文件,这个是很麻烦的。而mybatis-plus就很好的解决了这个问题。
2.什么是Mybatis-plus。
官方给出的定义是:Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
简单的一句话,但是也很明了,Mybatis-plus就是基于mybatis的一个plus工具,目的是为了帮助我们程序猿简化开发、提高我们程序猿的效率。
2.1 Mybatis-plus特性
内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询 等等。
…
这里由于内容过多,就不一一介绍,详细内容可以通过官网查看https://mp.baomidou.com
进入正题
3.如何集成Mybatis-plus
3.1导入依赖包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.2.0</version>
</dependency>
3.2在application.yml配置文件配置mybatis-plus
mybatis-plus:
mapper-locations: classpath:mapper/**/*.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: io.yxs.modules.*.entity
global-config:
#主键类型 0:数据库ID自增, 1:用户输入ID,2:全局唯一ID (数字类型唯一ID), 3:全局唯一ID UUID;
id-type: 2
#字段策略 0:忽略判断,1:非 NULL 判断),2:非空判断
field-strategy: 1
#驼峰下划线转换
db-column-underline: true
#刷新mapper 调试神器
refresh-mapper: true
#数据库大写下划线转换
#capital-mode: true
#序列接口实现类配置
#key-generator: com.baomidou.springboot.xxx
#逻辑删除配置
logic-delete-value: -1
logic-not-delete-value: 0
#自定义填充策略接口实现
#meta-object-handler: com.baomidou.springboot.xxx
#自定义SQL注入器
#sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
call-setters-on-nulls: true
jdbc-type-for-null: 'null'
#log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql语句
这里重点注意两个路径mapper-locations、typeAliasesPackage
注意:这两个路径为你自动生成代码时候设置的路径
第一个是mapper的xml所在的路径;
第二个是你生成的实体所在的路径;
3.3添加Mybatis-plus类,设置分页
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* mybatis-plus配置
//* @author yxs
*/
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
Ok,到这里配置基本完成,接着我们开始编码看是如何提高我们的开发效率解决Mybatis大量xm的问题。
首先我们建立User实体类
@TableName("sys_user")
public class SysUserEntity {
/**
* 用户ID
*/
@TableId
private String userId;
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
}
注意,这里的特殊之处,使用到了注解@TableName,主要是用于指定数据库中的表名。
其次我们建立DAO层
@Mapper
public interface UserMapper extends BaseMapper<SysUserEntity> {
}
说明:这里使用到了@Mapper注解,其作用主要是被注解的类,将会被Spring识别并管理。
接着我们建立Service层和实现层
UserService:
public interface UserService extends IService<User> {
}
UserServiceImpl:
//由于这里继承ServiceImpl,而这个类导入了UserMapper,因此无需再导入Mapper就可以开发了
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
implements UserService {
}
最后我们建立Controller层
@RestController
@RequestMapping("/user")
public class UserController {
//注入UserService 接口
@Autowired
private UserService userService;
@RequestMapping("/getAllUsers")
public String getAllUsers() {
//获取全部列表
List<User> selectList = userService.list();
return Result.ok().put("page", selectList);
}
}
总结
从上面的流程看下来,不难发现我们获取到了数据库中的数据,但是我们从头到尾也没有创建xml文件,编写一句select 的sql语句,这就是Mybatis-Plus的方便之处了,对于基本的sql操作Mybatsi-Plus已经为我们封装了一层,在编写代码时是可以直接去调用的,这点我们可以点开IService,ServiceImpl和BaseMapper查看提供的一些基本的sql封装方法。这就避免了程序员因为业务流程过多的时候,编写大量的xml文件。当然了,一些复杂的sql语句,还是要我们建立xml手动编写调用的,毕竟不能什么帮我们程序猿写好了,咱也不能丢饭碗不是。