商丘网站建设推广公司,哪里网站备案,聊城网站建设制作开发公司,wordpress 视频类型mybatisplus 的常用CRUD方法
众所周知#xff0c;mybatisplus提供了强大的代码生成能力#xff0c;他默认生成的常用的CRUD方法#xff08;例如插入、更新、删除、查询等#xff09;的定义#xff0c;能够帮助我们节省很多体力劳动。
他的BaseMapper中定义了这些常用的C…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;Overridepublic MappedStatement injectMappedStatement(Class? mapperClass, Class? modelClass, TableInfo tableInfo) {// 获取需要查询的字段名及属性名TableFieldInfo erpFiled getErpProperty(tableInfo);// 拼接组装sqlSqlSource 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) {ListTableFieldInfo 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 { Overridepublic ListAbstractMethod getMethodList(Class? mapperClass) {ListAbstractMethod methodList super.getMethodList(mapperClass);// 增加 SelectByErp对象程序启动后自动加载methodList.add(new SelectByErp());return methodList;}
}4.定义一个基础MapperGyhBaseMapper添加selectByErp方法
/*** 自定义的通用Mapper*/
public interface GyhBaseMapperT extends BaseMapperT {ListT selectByErp(String erp);
}5.应用中需要使用该SelectByErp方法的表都继承GyhBaseMapper那么这些表将都拥有了selectByErp这个查询方法程序启动后会自动为这些表生成该sql。
public interface XXXMapper extends GyhBaseMapperXXXTable 添加一个mybatisplus已有sql
1.mybatisplus 常用CRUD方法如最上图这些方法已经默认会自动生成但mybatisplus其实提供了更多的方法如下图只要我们在启动时添加进去就可以使用了。 2.比如我想使用AlwaysUpdateSomeColumnById方法该方法可以在更新时只更新我需要的字段不进行全字段更新。添加步骤如下。
3.定义一个sql注入器 如GyhSqlInjector添加AlwaysUpdateSomeColumnById对象
Component
public class GyhSqlInjector extends DefaultSqlInjector { Overridepublic ListAbstractMethod getMethodList(Class? mapperClass) {ListAbstractMethod methodList super.getMethodList(mapperClass);// 添加 AlwaysUpdateSomeColumnById 对象methodList.add(new AlwaysUpdateSomeColumnById());return methodList;}
}4.定义一个基础Mapper 如GyhBaseMapper添加alwaysUpdateSomeColumnById方法
/*** 自定义的通用Mapper*/
public interface GyhBaseMapperT extends BaseMapperT {int alwaysUpdateSomeColumnById(Param(Constants.ENTITY) T entity);
}
5.继承GyhBaseMapper的其他Mapper将自动拥有alwaysUpdateSomeColumnById方法
/*** 自定义的通用Mapper*/
public interface GyhBaseMapperT extends BaseMapperT {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;}Overridepublic MappedStatement injectMappedStatement(Class? mapperClass, Class? modelClass, TableInfo tableInfo) {String tableName tableInfo.getTableName();// shardingsphere 分库分表配置信息MapString, YamlTableRuleConfiguration tables yamlShardingRuleConfiguration.getTables();// 判断当前表是否设置了分表字段if (tables.containsKey(tableName)) {YamlTableRuleConfiguration tableRuleConfiguration tables.get(tableName);// 获取分表字段String shardingColumn tableRuleConfiguration.getTableStrategy().getStandard().getShardingColumn();// 构建sqlboolean 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) {ListTableFieldInfo 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 配置信息*/Autowiredprivate YamlShardingRuleConfiguration yamlShardingRuleConfiguration;Overridepublic ListAbstractMethod getMethodList(Class? mapperClass) {ListAbstractMethod methodList super.getMethodList(mapperClass);// 添加 UpdateByIdWithSharding 对象并注入分库分表信息methodList.add(new UpdateByIdWithSharding(yamlShardingRuleConfiguration));return methodList;}
}
4.定义一个基础MapperGyhBaseMapper添加新的selectById方法
/*** 自定义的通用Mapper*/
public interface GyhBaseMapperT extends BaseMapperT {int updateById(Param(Constants.ENTITY) T entity);
}
5.所有参与分表的表在定义Mapper时继承GyhBaseMapper那么在使用他的updateById方法时将自动增加分库分表判断准确命中目标表减少其他分表查询的资源浪费。 以上是针对mybatisplus的一些简单改造希望能为你提供一点点帮助~ 作者京东科技 郭艳红 来源京东云开发者社区 转载请注明来源