目录
编写MySqlInjector继承DefaultSqlInjector
Mybatis与MybatisPlus整合
Mapper继承BaseMapper,就拥有了BaseMapper中的所有方法
相关配置
SpringBoot配置文件参考
spring.application.name=itcast-mp-springboot
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mp?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=123
# 指定全局配置文件
mybatis-plus.config-location=classpath:mybatis-config.xml
# 指定mapper.xml文件的路径
mybatis-plus.mapper-locations= classpath*:mybatis/*.xml
# 实体对象扫描包
mybatis-plus.type-aliases-package= cn.itcast.mp.pojo
# 关闭自动驼峰映射,该参数不能和mybatis-plus.config-location同时存在 可以配置到mybatis-config.xml里边
#mybatis-plus.configuration.map-underscore-to-camel-case=false
# 是否禁用缓存
mybatis-plus.configuration.cache-enabled=false
# 全局id策略
mybatis-plus.global-config.db-config.id-type=auto
# 全局的表明的前缀
mybatis-plus.global-config.db-config.table-prefix=tb_
SpringBoot配置类配置分页器
@Configuration
@MapperScan("cn.itcast.mp.mapper") //设置mapper接口的扫描包
public class MybatisPlusConfig {
@Bean //配置分页插件
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
在Mybatis-config.xml文件中配置分页器
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"></plugin>
</plugins>
</configuration>
POJO类相关注释
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String userName;
@TableField(select = false) //查询时不反回该字段的值
private String password;
private String name;
private Integer age;
@TableField(value = "email") //指定数据库字段名映射
private String mail;
@TableField(exist = false)
private String address; //在数据库表中不存在
}
配置相关插件
SpringBoot配置类配置
package cn.itcast.mp;
import cn.itcast.mp.plugins.MyInterceptor;
import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.SqlExplainInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
@Configuration
@MapperScan("cn.itcast.mp.mapper") //设置mapper接口的扫描包
public class MybatisPlusConfig {
@Bean //配置分页插件
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
@Bean //注入自定义拦截器(插件)
public MyInterceptor myInterceptor(){
return new MyInterceptor();
}
@Bean //sql执行分析插件
public SqlExplainInterceptor sqlExplainInterceptor(){
SqlExplainInterceptor sqlExplainInterceptor = new SqlExplainInterceptor();
List<ISqlParser> list = new ArrayList<>();
//全表更新、删除的阻断器
list.add(new BlockAttackSqlParser());
sqlExplainInterceptor.setSqlParserList(list);
return sqlExplainInterceptor;
}
@Bean //乐观锁插件 需要为数据库添加Version字段 为User实体对象添加version字段,并且添加@Version注解
public OptimisticLockerInterceptor optimisticLockerInterceptor(){
return new OptimisticLockerInterceptor();
}
}
配置在mybatis-config
<configuration>
<plugins>
<plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"></plugin>
<!--性能分析插件-->
<plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor">
<!--最大执行时间,单位毫秒 若超过则抛出异常-->
<property name="maxTime" value="100"/>
<!--是否对输出sql格式化 默认false-->
<property name="format" value="true"/>
</plugin>
</plugins>
</configuration>