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

商丘网站建设推广公司外贸移动端网站模板

商丘网站建设推广公司,外贸移动端网站模板,建设网站过程视频,东莞手机微信网站制作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.hkea.cn/news/14318137/

相关文章:

  • 江西建设银行分行网站滨州网站建设
  • 烟台网站建设多少钱贵阳建站
  • 展示型网站建设方案书企业网站建设ppt介绍
  • 网站开发去哪里找程序员百度云架设网站
  • 啪啪男女禁做视频网站营销策略从哪几个方面分析
  • 网站如何做好内链中国排建设银行悦生活网站
  • 盘龙网站建设wordpress空间购买
  • 一比一高仿手表网站整容医院网络建设公司
  • 对中国建设银行网站的评价网站统计插件
  • 网站快速排名工具做个电商平台需要哪些步骤
  • 内销常用网站嘉兴 网站建设
  • 上海网站制作科技公司营销型网站制作msgg
  • 装饰网站开发背景国内的c2c网站有哪些
  • 一家只做卫生巾的网站线上推广费用
  • 浙江住房和城乡建设厅网站揭阳住房和城乡建设厅网站
  • 建站网站那个好站外推广渠道有哪些
  • 政务服务网站建设技术因素wordpress galleria
  • 好看网站的浏览器Md5(Wordpress)解密
  • 网站主机教程wordpress5.1更新
  • 关于百度网站是多少中国菲律宾南海开战
  • 网站用哪种语言如何利用淘宝建设网站挣钱
  • 免费 成品模板网站品牌网站设计方案
  • 网站改了title 删除百度就的收录wordpress可视化函数
  • 提供网站制作公司哪家专业北京工商注册网上核名
  • 企业门户网站建设管理制度网站层次索引模板
  • 网站建设中的需求报告功能大型网站建设地址
  • 公司做网站找谁怎样通过手机建网站
  • 好的文化网站模板下载HTML建网站
  • 有人做彩票网站吗天津关键词优化平台
  • 好用的cms网站网站运营难做嘛