MyBatis Plus 学习笔记(2019年7月6号)
官方地址:
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
基础配置:
替换Mybatis中sqlSessionFactoryBean,替换成MP的
通用 CRUD
单表插入操作:
1) Integer insert(T entity);
2) @TableName
3) 全局的 MP 配置:
4) @TableField
5) 全局的 MP 配置:
6) @TableId
7) 全局的 MP 配置:
8) 支持主键自增的数据库插入数据获取主键值
Mybatis: 需要通过 useGeneratedKeys 以及 keyProperty 来设置
MP: 自动将主键值回写到实体类中
9) Integer insertAllColumn(T entity)
更新操作:
1) Integer updateById(@Param("et") T entity); --更新通过id有几个字段更新几个字段
2) Integer updateAllColumnById(@Param("et") T entity) --更新通过多个条件去更新
查询操作:
1) T selectById(Serializable id); --通过id去查询
2) T selectOne(@Param("ew") T entity); --通过多个条件去查询
3) List
4) List
5) List
删除操作
1) Integer deleteById(Serializable id); --通过ID去删除
2) Integer deleteByMap(@Param("cm") Map
3) Integer deleteBatchIds(List extends Serializable> idList); --通过ids去删除
MP SQL 注入原理
xxxMapper -》继承BaseMapper -》SqlSessionFacotry -》Configuration(全局配置) -》 (所有sql构造方法)
MP 在启动就会挨个分析 xxxMapper 中的方法,并且将对应的 SQL 语句处理好,保存到 configuration 对象中的 mappedStatements 中
EntityWrapper(Wrapper) 构造器
new EntityWrapper
查询方法:List
selectList:查询条件带分页的 and操作:.eq()或者.between() (new Page
带有条件查询 or操作:.like() .or() 之后的就是执行or方法
or()或者orNew()区别:
// SQL: (gender = ? AND last_name LIKE ? OR email LIKE ?)
// SQL: (gender = ? AND last_name LIKE ?) OR (email LIKE ?)
更新操作:Integer update(@Param("et") T entity, @Param("ew") Wrapper
Condition 操作:
Condition.create() = new EntityWrapper
AR(ActiveRecord)操作:
首先在实体类中集成 :public class Employee extends Model
然后在实体类中集成指定当前实体类的主键属性:
@Override
protected Serializable pkVal() {
return id;
}
操作:实体类.insert() .update()
1) 插入操作
public boolean insert()
2) 修改操作
public boolean updateById()
3) 查询操作
public T selectById()
public T selectById(Serializable id)
public List
public List
public int selectCount(Wrapper wrapper)
4) 删除操作
public boolean deleteById()
public boolean deleteById(Serializable id)
public boolean delete(Wrapper wrapper)
5) 分页复杂操作
public Page
代码生成器:
1:添加代码生成器依赖:
测试方法:
@Test
public void testGenerator(){
//1. 全局配置
GlobalConfig config = new GlobalConfig();
config.setActiveRecord(true) // 是否支持AR模式
.setAuthor("Lixb") // 作者
.setOutputDir("D:\\workspacce_xx\\mp03\\src\\main\\java") // 生成路径
.setFileOverride(true) // 文件覆盖
.setIdType(IdType.AUTO) // 主键策略
.setServiceName("%sService") // 设置生成的service接口的名字的首字母是否为I
// IEmployeeService
.setBaseResultMap(true)
.setBaseColumnList(true);
//2. 数据源配置
DataSourceConfig dsConfig = new DataSourceConfig();
dsConfig.setDbType(DbType.MYSQL) // 设置数据库类型
.setDriverName("com.mysql.jdbc.Driver")
.setUrl("jdbc:mysql://localhost:3306/mp")
.setDbQuery(null)
.setUsername("root")
.setPassword("123456");
//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();
}
MybatisPlus插件:
配置文件:
1
2
3
4
1:分页查询:
com.baomidou.mybatisplus.plugins.PaginationInterceptor
Page
List
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);
2:执行分析插件
1) com.baomidou.mybatisplus.plugins.SqlExplainInterceptor
2) SQL 执行分析拦截器,只支持 MySQL5.6.3 以上版本
3) 该插件的作用是分析 DELETE UPDATE 语句,防止小白
或者恶意进行 DELETE UPDATE 全表操作
4) 只建议在开发环境中使用,不建议在生产环境使用
5) 在插件的底层 通过 SQL 语句分析命令:Explain 分析当前的 SQL 语句,
根据结果集中的 Extra 列来断定当前是否全表操作。
3:性能分析插件
1) com.baomidou.mybatisplus.plugins.PerformanceInterceptor
2) 性能分析拦截器,用于输出每条 SQL 语句及其执行时间
3) 只建议在开发环境中使用,不建议在生产环境使用
4:乐观锁插件
1) com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor
2) 如果想实现如下需求: 当要更新一条记录的时候,希望这条记录没有被别人更新
3) 乐观锁的实现原理:
取出记录时,获取当前 version 2
更新时,带上这个 version 2
执行更新时, set version = yourVersion+1 where version = yourVersion
如果 version 不对,就更新失败
4) @Version 用于注解实体字段,必须要有。
自动填充
1) 注解填充字段 @TableFile(fill = FieldFill.INSERT) 查看 FieldFill
全局策略:
逻辑删除