文章目录
01#MyBatisPlus简介
MyBatis-Plus(简称 MP),是一个 MyBatis 的增强工具包,只做增强不做改变. 为简化开发工作、提高生产率而生
官方地址:
http://mp.baomidou.com
代码发布地址:
Github: https://github.com/baomidou/mybatis-plus
Gitee: https://gitee.com/baomidou/mybatis-plus
文档发布地址:
http://mp.baomidou.com/#/?id=%E7%AE%80%E4%BB%8B
02#环境搭建
01-创建测试表
-- 创建库
CREATE DATABASE mp;
-- 使用库
USE mp;
-- 创建表
CREATE TABLE tbl_employee(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(50),
email VARCHAR(50),
gender CHAR(1),
age int
);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom','tom@atguigu.com',1,22);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Jerry','jerry@atguigu.com',0,25);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Black','black@atguigu.com',1,30);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('White','white@atguigu.com',0,35);
02-创建 javaBean
public class Employee {
private Integer id ;
private String lastName;
private String email ;
private Integer gender ;
private Integer age ;
}
03-maven依赖
<!-- mp 依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.3</version>
</dependency>
<!--junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
04-加入 MyBatis 的全局配置文件
<?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>
</configuration>
05-加入 log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd
HH:mm:ss,SSS} %m (%F:%L) \n" />
</layout>
</appender>
<logger name="java.sql">
<level value="debug" />
</logger>
<logger name="org.apache.ibatis">
<level value="info" />
</logger>
<root>
<level value="debug" />
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>
06-加入 db.properties 连接信息配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mp
jdbc.username=root
jdbc.password=1234
07-加入 spring 的配置文件 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 数据源 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 事务管理器 -->
<bean id="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 基于注解的事务管理 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
<!-- 配置 SqlSessionFactoryBean -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 别名处理 -->
<property name="typeAliasesPackage" value="com.atguigu.mp.beans"></property>
</bean>
<!--配置 mybatis 扫描 mapper 接口的路径-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.atguigu.mp.mapper"></property>
</bean>
</beans>
08-集成 MP
<!-- 把 Mybatis 自带的MybatisSqlSessionFactoryBean 替换为 MP 自带的即可 -->
<!-- 配置SqlSessionFactoryBean
Mybatis提供的: org.mybatis.spring.SqlSessionFactoryBean
MP提供的:com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean
-->
<bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 别名处理 -->
<property name="typeAliasesPackage" value="com.atguigu.mp.beans"></property>
</bean>
03#通用Mapper
01-使用
<!-- Mapper 接口, 并继承 BaseMapper 接口 -->
public interface EmployeeMapper extends BaseMapper<Employee> {
}
//配置单个MP
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
/*
* MybatisPlus会默认使用实体类的类名到数据中找对应的表.
*/
@TableName(value = "tbl_employee")
public class Employee {
/*
* @TableId: value: 指定表中的主键列的列名, 如果实体属性名与列名一致,可以省略不指定.
* type: 指定主键策略.
*/
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@TableField(value = "last_name")
private String lastName;
private String email;
private Integer gender;
private Integer age;
@TableField(exist = false)
private Double salary;
}
<!-- /配置全局MP -->
<bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 别名处理 -->
<property name="typeAliasesPackage" value="com.atguigu.mp.beans"></property>
<!-- 注入全局MP策略配置 -->
<property name="globalConfig" ref="globalConfiguration"></property>
</bean>
<!-- 定义MybatisPlus的全局策略配置-->
<bean id="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!-- 在2.3版本以后,dbColumnUnderline 默认值就是true -->
<property name="dbColumnUnderline" value="true"></property>
<!-- 全局的主键策略:0代表自增长-->
<property name="idType" value="0"></property>
<!-- 全局的表前缀策略配置 -->
<property name="tablePrefix" value="tbl_"></property>
</bean>
02-插入操作
1) Integer insert(T entity);//只有非空的属性对应的字段才会出现到SQL语句中
2) 支持主键自增的数据库插入数据获取主键值
Mybatis: 需要通过 useGeneratedKeys 以及 keyProperty 来设置
MP: 自动将主键值回写到实体类中
3) Integer insertAllColumn(T entity)//属性所对应的字段都会出现到SQL语句中
03-更新操作
1) Integer updateById(@Param("et") T entity);//只有非空的属性对应的字段才会出现到SQL语句中
2) Integer updateAllColumnById(@Param("et") T entity)//属性所对应的字段都会出现到SQL语句中
04-查询操作
1) T selectById(Serializable id);
2) T selectOne(@Param("ew") T entity);
3) List<T> selectBatchIds(List<? extends Serializable> idList);
4) List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);//数据库字段
5) List<T> selectPage(RowBounds rowBounds, @Param("ew") Wrapper<T> wrapper);//分页查询
例如:
List<Employee> emps = employeeMapper.selectPage(new Page<>(3, 2), null);
05-删除操作
1) Integer deleteById(Serializable id);
2) Integer deleteByMap(@Param("cm") Map<String, Object> columnMap);//数据库字段
3) Integer deleteBatchIds(List<? extends Serializable> idList);
04#条件构造器 EntityWrapper&Condition
01-简介
1、 EntityWrapper和Condition是MybatisPlus 封装的一个查询条件构造器,来让用户自由的构建查询条件,简单便捷,没有额外的负担, 能够有效提高开发效率。
2、实体包装器,主要用于处理 sql 拼接,排序,实体参数查询等。
3、注意: 使用的是数据库字段,不是 Java 属性。
条件参数说明:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QcjiWudH-1632920737697)(MyBatisPlus.assets/20190501161734238.png)] |
---|
02-EntityWrapper带条件的查询
List<T> selectPage(RowBounds rowBounds, @Param("ew") Wrapper<T> wrapper);//分页查询
List<T> selectList(@Param("ew") Wrapper<T> wrapper);
// 我们需要分页查询tbl_employee表中,年龄在18~50之间且性别为男且姓名为Tom的所有用户
List<Employee> emps =employeeMapper.selectPage(new Page<Employee>(1, 2),
new EntityWrapper<Employee>()
.between("age", 18, 50)
.eq("gender", 1)
.eq("last_name", "Tom")
);
System.out.println(emps);
// 查询tbl_employee表中, 性别为女并且名字中带有"老师" 或者 邮箱中带有"a"
List<Employee> emps = employeeMapper.selectList(
new EntityWrapper<Employee>()
.eq("gender", 0)
.like("last_name", "老师")
//.or() // SQL: (gender = ? AND last_name LIKE ? OR email LIKE ?)
.orNew() // SQL: (gender = ? AND last_name LIKE ?) OR (email LIKE ?)
.like("email", "a")
);
System.out.println(emps);
// 查询性别为女的, 根据age进行排序(asc/desc), 简单分页
List<Employee> emps = employeeMapper.selectList(new EntityWrapper<Employee>()
.eq("gender", 0)
.orderBy("age")
//.orderDesc(Arrays.asList(new String [] {"age"}))
.last("desc limit 1,3")
);
03-EntityWrapper带条件的修改
Integer update(@Param("et") T entity, @Param("ew") Wrapper<T> wrapper);
Employee employee = new Employee();
employee.setLastName("苍老师");
employee.setEmail("cls@sina.com");
employee.setGender(0);
employeeMapper.update(employee, new EntityWrapper<Employee>().eq("last_name", "Tom").eq("age", 44));
04-EntityWrapper带条件的删除
Integer delete(@Param("ew") Wrapper<T> wrapper);
employeeMapper.delete(new EntityWrapper<Employee>().eq("last_name", "Tom").eq("age", 22));
05-Condition使用
EntityWrapper和Condition用法一致
List<Employee> emps = employeeMapper.selectPage(new Page<Employee>(1, 2),
Condition.create()
.between("age", 18, 50)
.eq("gender", "1")
.eq("last_name", "Tom")
);
05#ActiveRecord(活动记录)
01-简介
Active Record(活动记录),是一种领域模型模式,特点是一个模型类对应关系型数据库中的一个表,而模型类的一个实例对应表中的一行记录。
ActiveRecord 一直广受动态语言( PHP 、 Ruby 等)的喜爱,而 Java 作为准静态语言,对于 ActiveRecord 往往只能感叹其优雅,所以 MP 也在 AR 道路上进行了一定的探索
02-如何使用 AR 模式
//仅仅需要让实体类继承 Model 类且实现主键指定方法,即可开启 AR 之旅.
@TableName("tbl_employee")
public class Employee extends Model<Employee> {
.......
/**
* 指定当前实体类的主键属性
*/
@Override
protected Serializable pkVal() {
return id;
}
}
03-插入操作
public boolean insert()
Employee employee = new Employee();
employee.setLastName("宋老师");
employee.setEmail("sls@atguigu.com");
employee.setGender(1);
employee.setAge(35);
boolean result = employee.insert();
System.out.println("result:" + result);
04-修改操作
public boolean updateById()
Employee employee = new Employee();
employee.setId(20);
employee.setLastName("宋老湿");
employee.setEmail("sls@atguigu.com");
employee.setGender(1);
employee.setAge(36);
boolean result = employee.updateById();
System.out.println("result:" + result);
05-查询操作
public T selectById()
public T selectById(Serializable id)
public List<T> selectAll()
public List<T> selectList(Wrapper wrapper)
public int selectCount(Wrapper wrapper)
Employee employee = new Employee();
Employee result = employee.selectById(14);
Employee employee = new Employee();
employee.setId(14);
Employee result = employee.selectById();
Employee employee = new Employee();
List<Employee> emps = employee.selectAll();
Employee employee = new Employee();
List<Employee > emps= employee.selectList(
new EntityWrapper<Employee>().like("last_name", "老师"));
Employee employee = new Employee();
Integer result = employee.selectCount(new EntityWrapper<Employee>().eq("gender", 0));
06-删除操作
//注意: 删除不存在的数据 逻辑上也是属于成功的.
public boolean deleteById()
public boolean deleteById(Serializable id)
public boolean delete(Wrapper wrapper)
Employee employee = new Employee();
boolean result = employee.deleteById(2);
Employee employee = new Employee();
employee.setId(2);
boolean result = employee.deleteById();
Employee employee = new Employee();
boolean result = employee.delete(new EntityWrapper<Employee>().like("last_name", "小"));
07-分页复杂操作
public Page<T> selectPage(Page<T> page, Wrapper<T> wrapper)
Employee employee = new Employee();
Page<Employee> page = employee.selectPage(new Page<>(1, 1),
new EntityWrapper<Employee>().like("last_name", "老"));
List<Employee> emps = page.getRecords();
06#代码生成器
01-MP和Mybatis代码生成器
MP 的代码生成器 和 Mybatis MBG 代码生成器比较:
1、MP 的代码生成器都是基于 java 代码来生成。MBG 基于 xml 文件进行代码生成
2、MyBatis 的代码生成器可生成: 实体类、Mapper 接口、Mapper 映射文件
MP 的代码生成器可生成: 实体类(可以选择是否支持 AR)、Mapper接口、Mapper映射、Service层、Controller层
02-建议
建议数据库表名和表字段名采用驼峰命名方式
如果采用下划线命名方式 请开启全局下划线开关
如果表名字段名命名方式不一致请注解指定
03-Maven
<!-- Apache velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- sfl4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
04-示例代码
package com.atguigu.mp.test;
import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;
public class MPGenerator {
/**
* 代码生成 示例代码
*/
@Test
public void testGenerator() {
//1. 全局配置
GlobalConfig config = new GlobalConfig();
config.setActiveRecord(true) // 是否支持AR模式
.setAuthor("weiyunhui") // 作者
.setOutputDir("D:\\workspace_mp\\mp03\\src\\main\\java") // 生成路径
.setFileOverride(true) // 多次生成文件覆盖
.setIdType(IdType.AUTO) // 主键策略--自增
// 设置生成的service接口的名字的首字母是否为I
// IEmployeeService
.setServiceName("%sService")
.setBaseResultMap(true)//基本的ResultMap
.setBaseColumnList(true);//基本的Column
//2. 数据源配置
DataSourceConfig dsConfig = new DataSourceConfig();
dsConfig.setDbType(DbType.MYSQL) // 设置数据库类型
.setDriverName("com.mysql.jdbc.Driver")
.setUrl("jdbc:mysql://localhost:3306/mp")
.setUsername("root")
.setPassword("1234");
//3. 策略配置
StrategyConfig stConfig = new StrategyConfig();
stConfig.setCapitalMode(true) //全局大写命名
.setDbColumnUnderline(true) // 指定表名 字段名是否使用下划线
.setNaming(NamingStrategy.underline_to_camel) // 数据库表映射到实体的命名策略
// 表名前缀
.setTablePrefix("tbl_")
// 表名// 表名// 表名// 表名// 表名// 表名// 表名
.setInclude("tbl_employee");
//4. 包名策略配置
// 代码生成的包
PackageConfig pkConfig = new PackageConfig();
pkConfig.setParent("com.atguigu.mp")
.setMapper("mapper")
.setService("service")
.setController("controller")
.setEntity("beans")
.setXml("mapper");
//5. 整合配置
AutoGenerator ag = new AutoGenerator();
ag.setGlobalConfig(config)
.setDataSource(dsConfig)
.setStrategy(stConfig)
.setPackageInfo(pkConfig);
//6. 执行
ag.execute();
}
}
07#插件
01-简介
1) 插件机制: Mybatis 通过插件(Interceptor) 可以做到拦截四大对象相关方法的执行,根据需求,完成相关数据的动态改变。
Executor
StatementHandler
ParameterHandler
ResultSetHandler
2) 插件原理
四大对象的每个对象在创建时,都会执行 interceptorChain.pluginAll(),会经过每个插件对象的 plugin()方法,目的是为当前的四大对象创建代理。代理对象就可以拦截到四大对象相关方法的执行,因为要执行四大对象的方法需要经过代理.
02-插件使用
<!--方式一:MP的全局配置文件中-->
<?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.plugins.PaginationInterceptor"></plugin>
......
</plugins>
</configuration>
<!--方式二:Spring配置文件中-->
<bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
......
<!-- 插件注册 -->
<property name="plugins">
<list>
<!-- 注册分页插件 -->
<bean class="com.baomidou.mybatisplus.plugins.PaginationInterceptor"></bean>
</list>
</property>
</bean>
03-分页插件
<bean class="com.baomidou.mybatisplus.plugins.PaginationInterceptor"></bean>
Page<Employee> page = new Page<>(1, 1);
List<Employee> emps = employeeMapper.selectPage(page, null);
System.out.println(emps);
System.out.println("===============获取分页相关的一些信息======================");
System.out.println("总条数:" + page.getTotal());
System.out.println("当前页码: " + page.getCurrent());
System.out.println("总页码:" + page.getPages());
System.out.println("每页显示的条数:" + page.getSize());
System.out.println("是否有上一页: " + page.hasPrevious());
System.out.println("是否有下一页: " + page.hasNext());
//将查询的结果封装到page对象中
page.setRecords(emps);
04-执行分析插件
1) SQL 执行分析拦截器,只支持 MySQL5.6.3 以上版本
2) 该插件的作用是防止小白或者恶意进行 DELETE UPDATE 全表操作
3) 只建议在开发环境中使用,不建议在生产环境使用
4) 在插件的底层通过SQL语句分析命令:Explain 分析当前的SQL语句,根据结果集中的Extra列来断定当前是否全表操作。
<bean class="com.baomidou.mybatisplus.plugins.SqlExplainInterceptor">
<property name="stopProceed" value="true"></property><!--检测到全表删除或者修改 中止操作-->
</bean>
@Test
public void testSQLExplain() {
employeeMapper.delete(null); // 全表删除
}
05-性能分析插件
1) 性能分析拦截器,用于输出每条 SQL 语句及其执行时间
2) SQL性能执行分析,开发环境使用,超过指定时间,停止运行。有助于发现问题
<bean class="com.baomidou.mybatisplus.plugins.PerformanceInterceptor">
<property name="format" value="true"></property><!--格式化代码-->
<property name="maxTime" value="5"></property><!--毫秒 超时时间-->
</bean>
@Test
public void testPerformance() {
Employee employee = new Employee();
employee.setLastName("玛利亚老师");
employee.setEmail("mly@sina.com");
employee.setGender("0");
employee.setAge(22);
employeeMapper.insert(employee);
}
06-乐观锁插件
1) 如果想实现如下需求: 当要更新一条记录的时候,希望这条记录没有被别人更新
2) 乐观锁的实现原理:
取出记录时,获取当前 version 2
更新时,带上这个 version 2
执行更新时, set version = yourVersion+1 where version = yourVersion
如果 version 不对,就更新失败
3) @Version 用于注解实体字段,必须要有。
<bean class="com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor">
</bean>
@Version
private Integer version;
@Test
public void testOptimisticLocker() {
Employee employee = new Employee();
employee.setId(15);
employee.setVersion(3);
employeeMapper.updateById(employee);
}
08#自定义全局操作
01-自定义全局操作
不想在 xml 中进行配置的 SQL 语句,想跟通用Mapper一样直接注入使用
1) 在 Mapper 接口中定义相关的 CRUD 方法
2) 扩展 AutoSqlInjector inject 方法,实现 Mapper 接口中方法要注入的 SQL
3) 在 MP 全局策略中,配置 自定义注入器
public interface EmployeeMapper extends BaseMapper<Employee> {
// 1、Mapper中定义方法
int deleteAll();
}
/**
* 自定义全局操作
*/
public class MySqlInjector extends AutoSqlInjector {
/**
* 扩展inject 方法,完成自定义全局操作
*/
@Override
public void inject(Configuration configuration, MapperBuilderAssistant builderAssistant, Class<?> mapperClass,Class<?> modelClass, TableInfo table) {
//将EmployeeMapper中定义的deleteAll,处理成对应的MappedStatement对象,加入到configuration对象中。
//注入的SQL语句
String sql = "delete from " + table.getTableName();
//注入的方法名 一定要与EmployeeMapper接口中的方法名一致
String method = "deleteAll";
//构造SqlSource对象
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
//构造一个删除的MappedStatement
this.addDeleteMappedStatement(mapperClass, method, sqlSource);
}
}
<!-- Spring配置文件-->
<!-- 定义MybatisPlus的全局策略配置-->
<bean id ="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!--注入自定义全局操作 -->
<property name="sqlInjector" ref="mySqlInjector"></property>
</bean>
<!-- 定义自定义注入器 -->
<bean id="mySqlInjector" class="com.atguigu.mp.injector.MySqlInjector"></bean>
/**
* 测试自定义全局操作
*/
@Test
public void testMySqlInjector() {
Integer result = employeeMapper.deleteAll();
System.out.println("result: " +result );
}
02-系统逻辑删除
@TableLogic // 逻辑删除属性
private Integer logicFlag ;
<!-- Spring配置文件-->
<!-- 定义MybatisPlus的全局策略配置-->
<bean id ="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!-- 注入逻辑删除 -->
<property name="sqlInjector" ref="logicSqlInjector"></property>
<!-- 注入逻辑删除全局值 -->
<property name="logicDeleteValue" value = "-1"></property><!--删除状态-->
<property name="logicNotDeleteValue" value="1"></property><!--非删除状态-->
</bean>
<!-- 逻辑删除 -->
<bean id="logicSqlInjector" class="com.baomidou.mybatisplus.mapper.LogicSqlInjector"></bean>
Integer result = userMapper.deleteById(1);
System.out.println("result:" +result );
User user = userMapper.selectById(1);//null
09#公共字段自动填充
01-元数据处理器接口
com.baomidou.mybatisplus.mapper.MetaObjectHandler
insertFill(MetaObject metaObject)
updateFill(MetaObject metaObject)
metaobject: 元对象. 是 Mybatis 提供的一个用于更加方便,更加优雅的访问对象的属性,给对象的属性设置值 的一个对象. 还会用于包装对象. 支持对 Object 、Map、Collection等对象进行包装
本质上 metaObject 获取对象的属性值或者是给对象的属性设置值,最终是要通过 Reflector 获取到属性的对应方法的 Invoker, 最终 invoke.
02-开发步骤
1) 注解填充字段 @TableFile(fill = FieldFill.INSERT) 查看 FieldFill
2) 自定义公共字段填充处理器
3) MP 全局注入 自定义公共字段填充处理器
@TableField(fill=FieldFill.INSERT_UPDATE)
private String name ;
import org.apache.ibatis.reflection.MetaObject;
import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
/**
* 自定义公共字段填充处理器
*/
public class MyMetaObjectHandler extends MetaObjectHandler {
/**
* 插入操作 自动填充
*/
@Override
public void insertFill(MetaObject metaObject) {
//获取到需要被填充的字段的值
Object fieldValue = getFieldValByName("name", metaObject);
if (fieldValue == null) {
System.out.println("*******插入操作 满足填充条件*********");
setFieldValByName("name", "weiyunhui", metaObject);
}
}
/**
* 修改操作 自动填充
*/
@Override
public void updateFill(MetaObject metaObject) {
Object fieldValue = getFieldValByName("name", metaObject);
if (fieldValue == null) {
System.out.println("*******修改操作 满足填充条件*********");
setFieldValByName("name", "weiyh", metaObject);
}
}
}
<!-- Spring配置文件-->
<!-- 定义MybatisPlus的全局策略配置-->
<bean id ="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!-- 注入公共字段填充处理器 -->
<property name="metaObjectHandler" ref="myMetaObjectHandler"></property>
</bean>
<!-- 公共字段填充 处理器 -->
<bean id="myMetaObjectHandler" class="com.atguigu.mp.metaObjectHandler.MyMetaObjectHandler"> </bean>
public void testMetaObjectHandler() {
User user = new User();
//user.setName("Tom");
user.setId(5);
user.setLogicFlag(1);
userMapper.updateById(user);
}
10#Oracle主键 Sequence
01-使用
MySQL: 支持主键自增。 IdType.Auto
Oracle: 序列(Sequence)
1) 实体类配置主键 Sequence @KeySequence(value=”序列名”,clazz=xxx.class 主键属性类型)
2) 全局 MP 主键生成策略为 IdType.INPUT
3) 全局 MP 中配置 Oracle 主键 Sequence
com.baomidou.mybatisplus.incrementer.OracleKeyGenerator
4) 可以将@keySequence 定义在父类中,可实现多个子类对应的多个表公用一个 Sequence
<!-- Oracle驱动: 因为Oracle授权的问题,不能从Maven的仓库中下载到Oracle驱动 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
orcl.driver=oracle.jdbc.OracleDriver
orcl.url=jdbc:oracle:thin:@localhost:1521:xe
orcl.username=system
orcl.password=1234
<!-- Spring配置文件-->
<!-- 数据源 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- Oracle -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${orcl.driver}"></property>
<property name="jdbcUrl" value="${orcl.url}"></property>
<property name="user" value="${orcl.username}"></property>
<property name="password" value="${orcl.password}"></property>
</bean>
@KeySequence(value="seq_user",clazz=Integer.class)
public class User {
//@TableId(type=IdType.INPUT)//某个类指定主键自增类型
private Integer id ;
}
<!-- Spring配置文件-->
<!-- 定义MybatisPlus的全局策略配置-->
<bean id ="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!-- Mysql 全局的主键策略 -->
<!-- <property name="idType" value="0"></property> -->
<!-- Oracle全局主键策略 -->
<property name="idType" value="1"></property>
</bean>
<!-- Spring配置文件-->
<!-- 定义MybatisPlus的全局策略配置-->
<bean id ="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!-- 注入Oracle主键Sequence -->
<property name="keyGenerator" ref="oracleKeyGenerator"></property>
</bean>
<!-- 配置Oracle主键Sequence -->
<bean id="oracleKeyGenerator" class="com.baomidou.mybatisplus.incrementer.OracleKeyGenerator"></bean>
public void testOracle() {
User user = new User();
user.setLogicFlag(1);
user.setName("OracleSEQ");
userMapper.insert(user);
}
02-抽取公共entity
@KeySequence(value="seq_user",clazz=Integer.class)
public abstract class Parent {
}
//@KeySequence(value="seq_user",clazz=Integer.class)
public class User extends Parent {
//@TableId(type=IdType.INPUT)
private Integer id ;
}