手把手教你如何扩展(破解)mybatisplus的sql生成
mybatisplus 的常用CRUD方法
众所周知,mybatisplus提供了强大的代码生成能力,他默认生成的常用的CRUD方法(例如插入、更新、删除、查询等)的定义,能够帮助我们节省很多体力劳动。
他的BaseMapper
中定义了这些常用的CRUD方法,我们在使用时,继承这个BaseMapper
类就默认拥有了这些能力。
如果我们的业务中,需要类似的通用Sql时,该如何实现呢?
是每个Mapper中都定义一遍类似的Sql吗?
显然这是最笨的一种方法。
此时我们可以借助mybatisplus
这个成熟框架,来实现我们想要的通用Sql。
扩展常用CRUD方法
新增一个通用sql
比如有一个这样的需求,项目中所有表或某一些表,都要执行一个类似的查询,如`SelectByErp`,那么可以这样实现。(这是一个最简单的sql实现,使用时可以根据业务需求实现更为复杂的sql:比如多租户系统自动增加租户id参数、分库分表系统增加分库分表字段条件判断)
定义一个
SelectByErp
类,继承AbstractMethod
类,并实现injectMappedStatement
方法定义sql方法名、sql模板、实现sql的拼接组装
/**
* 新增一个通用sql
*/
public class SelectByErp extends AbstractMethod {
// 需要查询的列名
private final String erpColumn = "erp";
// sql方法名
private final String method = "selectByErp";
// sql模板
private final String sqlTemplate = "SELECT %s FROM %s WHERE %s=#{%s} %s";
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
// 获取需要查询的字段名及属性名
TableFieldInfo erpFiled = getErpProperty(tableInfo);
// 拼接组装sql
SqlSource sqlSource = new RawSqlSource(configuration, String.format(sqlTemplate,
sqlSelectColumns(tableInfo, false),
tableInfo.getTableName(),
erpFiled.getColumn(), erpFiled.getProperty(),
tableInfo.getLogicDeleteSql(true, false)), Object.class);
return this.addSelectMappedStatementForTable(mapperClass, method, sqlSource, tableInfo);
}
/**
* 查询erp列信息
*/
private TableFieldInfo getErpProperty(TableInfo tableInfo) {
List<TableFieldInfo> fieldList = tableInfo.getFieldList();
TableFieldInfo erpField = fieldList.stream().filter(filed -> filed.getColumn().equals(erpColumn)).findFirst().get();
return erpField;
}
3.定义一个sql注入器GyhSqlInjector
,添加SelectByErp
对象
// 需注入到spring容器中
@Component
public class GyhSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass);
// 增加 SelectByErp对象,程序启动后自动加载
methodList.add(new SelectByErp());
return methodList;
}
}
4.定义一个基础MapperGyhBaseMapper
,添加selectByErp
方法
/**
* 自定义的通用Mapper
*/
public interface GyhBaseMapper<T> extends BaseMapper<T> {
List<T> selectByErp(String erp);
}
5.应用中需要使用该SelectByErp
方法的表,都继承GyhBaseMapper
,那么这些表将都拥有了selectByErp
这个查询方法,程序启动后会自动为这些表生成该sql。
public interface XXXMapper extends GyhBaseMapper<XXXTable>
添加一个mybatisplus已有sql
1.mybatisplus 常用CRUD方法如最上图,这些方法已经默认会自动生成,但mybatisplus其实提供了更多的方法,如下图,只要我们在启动时添加进去,就可以使用了。
2.比如我想使用AlwaysUpdateSomeColumnById
方法,该方法可以在更新时只更新我需要的字段,不进行全字段更新。添加步骤如下。
3.定义一个sql注入器 ,如GyhSqlInjector
,添加AlwaysUpdateSomeColumnById
对象
@Component
public class GyhSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass);
// 添加 AlwaysUpdateSomeColumnById 对象
methodList.add(new AlwaysUpdateSomeColumnById());
return methodList;
}
}
4.定义一个基础Mapper 如GyhBaseMapper
,添加alwaysUpdateSomeColumnById
方法
/**
* 自定义的通用Mapper
*/
public interface GyhBaseMapper<T> extends BaseMapper<T> {
int alwaysUpdateSomeColumnById(@Param(Constants.ENTITY) T entity);
}
5.继承GyhBaseMapper
的其他Mapper,将自动拥有alwaysUpdateSomeColumnById
方法
/**
* 自定义的通用Mapper
*/
public interface GyhBaseMapper<T> extends BaseMapper<T> {
int alwaysUpdateSomeColumnById(@Param(Constants.ENTITY) T entity);
}
6.继承GyhBaseMapper
的其他Mapper,将自动拥有alwaysUpdateSomeColumnById
方法
编辑一个mybatisplus已有sql
1.如果想编辑一个mybatisplus已有sql,比如分库分表系统,执行updateById
操作时,虽然主键Id已确定,但目标表不确定,此时可能导致该sql在多张表上执行,造成资源浪费,并且分库分表字段不可修改,默认的updateById
不能用,需要改造。以下以shardingsphere
分库分表为例。
2.定义一个UpdateByIdWithSharding
类,继承UpdateById
类
public class UpdateByIdWithSharding extends UpdateById {
private String columnDot = "`";
private YamlShardingRuleConfiguration yamlShardingRuleConfiguration;
// 注入shardingsphere的分库分表配置信息
public UpdateByIdWithSharding(YamlShardingRuleConfiguration yamlShardingRuleConfiguration) {
this.yamlShardingRuleConfiguration = yamlShardingRuleConfiguration;
}
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
String tableName = tableInfo.getTableName();
// shardingsphere 分库分表配置信息
Map<String, YamlTableRuleConfiguration> tables = yamlShardingRuleConfiguration.getTables();
// 判断当前表是否设置了分表字段
if (tables.containsKey(tableName)) {
YamlTableRuleConfiguration tableRuleConfiguration = tables.get(tableName);
// 获取分表字段
String shardingColumn = tableRuleConfiguration.getTableStrategy().getStandard().getShardingColumn();
// 构建sql
boolean logicDelete = tableInfo.isLogicDelete();
SqlMethod sqlMethod = SqlMethod.UPDATE_BY_ID;
// 增加分表字段判断
String shardingAdditional = getShardingColumnWhere(tableInfo, shardingColumn);
// 是否判断逻辑删除字段
final String additional = optlockVersion() + tableInfo.getLogicDeleteSql(true, false);
shardingAdditional = shardingAdditional + additional;
String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(),
getSqlSet(logicDelete, tableInfo, shardingColumn),
tableInfo.getKeyColumn(), ENTITY_DOT + tableInfo.getKeyProperty(),
shardingAdditional);
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
} else {
return super.injectMappedStatement(mapperClass, modelClass, tableInfo);
}
}
/**
* where条件增加分表字段
*/
private String getShardingColumnWhere(TableInfo tableInfo, String shardingColumn) {
StringBuilder shardingWhere = new StringBuilder();
shardingWhere.append(" AND ").append(shardingColumn).append("=#{");
shardingWhere.append(ENTITY_DOT);
TableFieldInfo fieldInfo = tableInfo.getFieldList().stream()
.filter(f -> f.getColumn().replaceAll(columnDot, StringUtils.EMPTY).equals(shardingColumn))
.findFirst().get();
shardingWhere.append(fieldInfo.getEl());
shardingWhere.append("}");
return shardingWhere.toString();
}
/**
* set模块去掉分表字段
*/
public String getSqlSet(boolean ignoreLogicDelFiled, TableInfo tableInfo, String shardingColumn) {
List<TableFieldInfo> fieldList = tableInfo.getFieldList();
// 去掉分表字段的set设置,即不修改分表字段
String rmShardingColumnSet = fieldList.stream()
.filter(i -> ignoreLogicDelFiled ? !(tableInfo.isLogicDelete() && i.isLogicDelete()) : true)
.filter(i -> !i.getColumn().equals(shardingColumn))
.map(i -> i.getSqlSet(ENTITY_DOT))
.filter(Objects::nonNull).collect(joining(NEWLINE));
return rmShardingColumnSet;
}
}
3.定义一个sql注入器GyhSqlInjector
,添加UpdateByIdWithSharding
对象
// 需注入到spring容器中
@Component
public class GyhSqlInjector extends DefaultSqlInjector {
/**
* shardingsphere 配置信息
*/
@Autowired
private YamlShardingRuleConfiguration yamlShardingRuleConfiguration;
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass);
// 添加 UpdateByIdWithSharding 对象,并注入分库分表信息
methodList.add(new UpdateByIdWithSharding(yamlShardingRuleConfiguration));
return methodList;
}
}
4.定义一个基础MapperGyhBaseMapper
,添加新的selectById
方法
/**
* 自定义的通用Mapper
*/
public interface GyhBaseMapper<T> extends BaseMapper<T> {
int updateById(@Param(Constants.ENTITY) T entity);
}
5.所有参与分表的表,在定义Mapper时继承GyhBaseMapper
,那么在使用他的updateById
方法时,将自动增加分库分表判断,准确命中目标表,减少其他分表查询的资源浪费。
以上是针对mybatisplus
的一些简单改造,希望能为你提供一点点帮助~
作者:京东科技 郭艳红
来源:京东云开发者社区 转载请注明来源
手把手教你如何扩展(破解)mybatisplus的sql生成的更多相关文章
- 手把手教你Chrome扩展开发:本地存储篇
手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单 手把手教你开发Chrome扩展二:为html添加行为 手把手教你开发Chrome扩展三:关于本地存储数据 HTML5 ...
- 【前端工具】Chrome 扩展程序的开发与发布 -- 手把手教你开发扩展程序
关于 chrome 扩展的文章,很久之前也写过一篇.清除页面广告?身为前端,自己做一款简易的chrome扩展吧. 本篇文章重在分享一些制作扩展的过程中比较重要的知识及难点. 什么是 chrome 扩展 ...
- ----转载----【前端工具】Chrome 扩展程序的开发与发布 -- 手把手教你开发扩展程序
关于 chrome 扩展的文章,很久之前也写过一篇.清除页面广告?身为前端,自己做一款简易的chrome扩展吧. 本篇文章重在分享一些制作扩展的过程中比较重要的知识及难点. 什么是 chrome 扩展 ...
- 手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单
手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单 手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单 手把手教你开发Chrome扩 ...
- 菜鸟-手把手教你把Acegi应用到实际项目中(8)-扩展UserDetailsService接口
一个能为DaoAuthenticationProvider提供存取认证库的的类,它必须要实现UserDetailsService接口: public UserDetails loadUserByUse ...
- 手把手教你开发chrome扩展
转载:http://www.cnblogs.com/walkingp/archive/2011/04/04/2003875.html 手把手教你开发chrome扩展一:开发Chrome Extenst ...
- 手把手教你开发Chrome扩展三:关于本地存储数据
手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单 手把手教你开发Chrome扩展二:为html添加行为 手把手教你开发Chrome扩展三:关于本地存储数据 HTML5 ...
- 手把手教你开发Chrome扩展二:为html添加行为
手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单 手把手教你开发Chrome扩展二:为html添加行为 手把手教你开发Chrome扩展三:关于本地存储数据 上一节我们 ...
- 手把手教你破解文件密码、wifi密码、网页密码
手把手教你破解文件密码.wifi密码.网页密码 1.破解文件密码: 有时候我们在网上下载一个压缩包后,必须要关注或者支付一定费用才给你解压密码,实属比较恶心.在这里手把手叫你实现破解文件解压密码. 1 ...
- 手把手教从零开始在GitHub上使用Hexo搭建博客教程(二)-Hexo参数设置
前言 前文手把手教从零开始在GitHub上使用Hexo搭建博客教程(一)-附GitHub注册及配置介绍了github注册.git相关设置以及hexo基本操作. 本文主要介绍一下hexo的常用参数设置. ...
随机推荐
- 长连接:chatgpt流式响应背后的逻辑
一.前言: 提起长连接,我们并不陌生,最常见的长连接非websocket莫属了.即使没有在项目中实际用过,至少也应该有所接触.长连接指在一次网络通信中,客户端与服务器之间建立一条持久的连接,可以在多次 ...
- Java中的线程池使用及原理
开篇-为什么要使用线程池? Java 中的线程池是运用场景最多的并发框架,几乎所有需要异步或并发执行任务的程序都可以使用线程池.在开发过程中,合理地使用线程池能够带来 3 个好处. 第一:降低 ...
- Visual Studio常用快捷键(附带免费PDF)
前言 对于开发者而言,熟悉快捷键的使用,能够起到事半功倍的作用,提高工作效率.以下是我整理的一份Visual Studio常用快捷键清单,希望能够帮助到你. 常用快捷方式 快捷键 功能 Ctrl + ...
- 华为ensp配置静态路由,三路由,三pc
华为ensp配置静态路由 目的:使pc1,pc2,pc3能相互ping通 1,tuop图的搭建 1,如图所示:先搭建好设备的通讯关系,在标记好每台设备对应的,ip地址和网关. 2,pc的网关,与ip地 ...
- G-channel 实现低光图像增强
G-channel 之前研究低光图像增强时,看到一篇博客,里面介绍了一种方法,没有说明出处,也没有说明方法的名字,这里暂时叫做 G-channel 算法. 博客地址:低照度图像增强(附步骤及源码)_低 ...
- Linux下apt与dpkg的详解
apt是一个包管理工具,用于管理Debian和Ubuntu等基于Debian的Linux发行版中的软件包.它是"Advanced Packaging Tool"的缩写,允许用户在系 ...
- 银河麒麟等 Linux系统 安装 .net 5,net 6及更高版本的方法
最近项目上用到 银河麒麟的操作系统,需要搭建 .net 跨平台方案.一开始使用各种命令都安装不上,很多提示命令找不到,或者下载包时候网络无法下载. 网上教程很多,但没有一个是成功的,多数使用 apt ...
- [ABC129E] Sum Equals Xor
2023-01-15 题目 题目传送门 翻译 翻译 难度&重要性(1~10):4 题目来源 AtCoder 题目算法 dp/模拟 解题思路 我们都知道,异或是一种不进位的加法,而要想 $ a ...
- 产品代码都给你看了,可别再说不会DDD(四):代码工程结构
这是一个讲解DDD落地的文章系列,作者是<实现领域驱动设计>的译者滕云.本文章系列以一个真实的并已成功上线的软件项目--码如云(https://www.mryqr.com)为例,系统性地讲 ...
- C++ ASIO 实现异步套接字管理
Boost ASIO(Asynchronous I/O)是一个用于异步I/O操作的C++库,该框架提供了一种方便的方式来处理网络通信.多线程编程和异步操作.特别适用于网络应用程序的开发,从基本的网络通 ...