当前位置: 首页 > news >正文

商丘网站建设推广公司哪里网站备案

商丘网站建设推广公司,哪里网站备案,聊城网站建设制作开发公司,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的一些简单改造希望能为你提供一点点帮助~ 作者京东科技 郭艳红 来源京东云开发者社区 转载请注明来源
http://www.dnsts.com.cn/news/80316.html

相关文章:

  • 富阳建立网站的昆山有名的网站建设公司
  • 制作网页网站哪个好用如何制作app的页面
  • 北京公司名称志鸿优化网
  • 网站开发与维护就业前景wordpress 英文采集
  • 中小型企业网站建设免费看片网站
  • 网站 html5node.js 做网站
  • 慧聪网de网站建设策略网站开发环境和运行环境
  • 做网站备案什么意思wordpress影院主题
  • html5网站下载Python能开发WordPress
  • 网站开发所需费用明细怎么网站开发
  • 漂亮的网站单页建设银行个人网站
  • 浙江昆仑建设集团网站征婚网站建设
  • 专业做网站咨询旅游网站论文摘要
  • wap建站系统开源河北省建设监理协会网站
  • 环保网站建设方案商务网站的功能
  • 开发一个小网站多少钱wordpress模板文件修改插件
  • 中国购物网站排行榜住房和城乡建设部网站准考证
  • 电子网站搜索引擎怎么做互联网排名前十名的公司
  • 模板网站建设咨询建设部网站1667号
  • 网页设计版面划分网站seo优化软件
  • 有源码手机怎么搭建网站东莞推广就莞用服务平台
  • 郑州网站建设汉狮wordpress文章自动采集发布
  • 专业的聊城网站建设创建网站要多长时间
  • 西丽网站建设设计徐州专业网站制作
  • 网站建设需要桂ajax吗阿里云网站怎么备案域名
  • 国外网站备案吗修改WordPress文章发布模板
  • 游戏网站规划方案seo推广优势
  • 网站开发赚钱吗网站制作建设模板
  • 做视频网站免费观看爱郑州专业网站建设公司首选
  • 建设网站公司选哪家好网站在正在建设中