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

产品平面广告设计seo课程培训机构

产品平面广告设计,seo课程培训机构,找别人做网站可以提供源码吗,那个网站做h5不要钱mybatisplus 的常用CRUD方法 众所周知,mybatisplus提供了强大的代码生成能力,他默认生成的常用的CRUD方法(例如插入、更新、删除、查询等)的定义,能够帮助我们节省很多体力劳动。 他的BaseMapper中定义了这些常用的C…

mybatisplus 的常用CRUD方法

众所周知,mybatisplus提供了强大的代码生成能力,他默认生成的常用的CRUD方法(例如插入、更新、删除、查询等)的定义,能够帮助我们节省很多体力劳动。

他的BaseMapper中定义了这些常用的CRUD方法,我们在使用时,继承这个BaseMapper类就默认拥有了这些能力。

BaseMapper.png

如果我们的业务中,需要类似的通用Sql时,该如何实现呢?

是每个Mapper中都定义一遍类似的Sql吗?

显然这是最笨的一种方法。

此时我们可以借助mybatisplus这个成熟框架,来实现我们想要的通用Sql。

扩展常用CRUD方法

新增一个通用sql

比如有一个这样的需求,项目中所有表或某一些表,都要执行一个类似的查询,如`SelectByErp`,那么可以这样实现。(这是一个最简单的sql实现,使用时可以根据业务需求实现更为复杂的sql:比如多租户系统自动增加租户id参数、分库分表系统增加分库分表字段条件判断)

  1. 定义一个SelectByErp类,继承AbstractMethod类,并实现injectMappedStatement方法

  2. 定义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) {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 {    @Overridepublic 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其实提供了更多的方法,如下图,只要我们在启动时添加进去,就可以使用了。

method.png

2.比如我想使用AlwaysUpdateSomeColumnById方法,该方法可以在更新时只更新我需要的字段,不进行全字段更新。添加步骤如下。

3.定义一个sql注入器 ,如GyhSqlInjector,添加AlwaysUpdateSomeColumnById对象

@Component
public class GyhSqlInjector extends DefaultSqlInjector {    @Overridepublic 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;}@Overridepublic 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();// 构建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) {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 配置信息*/@Autowiredprivate YamlShardingRuleConfiguration yamlShardingRuleConfiguration;@Overridepublic 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的一些简单改造,希望能为你提供一点点帮助~

作者:京东科技 郭艳红

来源:京东云开发者社区 转载请注明来源


文章转载自:
http://lingula.rhmk.cn
http://aroynt.rhmk.cn
http://firmly.rhmk.cn
http://dementi.rhmk.cn
http://elegant.rhmk.cn
http://sonorously.rhmk.cn
http://regionalism.rhmk.cn
http://asthenopic.rhmk.cn
http://synopsis.rhmk.cn
http://microtome.rhmk.cn
http://unending.rhmk.cn
http://dropscene.rhmk.cn
http://eudiometry.rhmk.cn
http://subsequential.rhmk.cn
http://nethermore.rhmk.cn
http://epilog.rhmk.cn
http://crizzle.rhmk.cn
http://taffarel.rhmk.cn
http://cornfield.rhmk.cn
http://ketonemia.rhmk.cn
http://debarrass.rhmk.cn
http://tycoonship.rhmk.cn
http://multidisciplinary.rhmk.cn
http://crowdie.rhmk.cn
http://rodger.rhmk.cn
http://obturate.rhmk.cn
http://damnably.rhmk.cn
http://koel.rhmk.cn
http://yippee.rhmk.cn
http://meromorphic.rhmk.cn
http://rubytail.rhmk.cn
http://declarator.rhmk.cn
http://boozeroo.rhmk.cn
http://recount.rhmk.cn
http://hodiernal.rhmk.cn
http://hayley.rhmk.cn
http://indomitably.rhmk.cn
http://cuckoopint.rhmk.cn
http://wizen.rhmk.cn
http://folium.rhmk.cn
http://noho.rhmk.cn
http://retroactivity.rhmk.cn
http://agrometeorological.rhmk.cn
http://staggery.rhmk.cn
http://chital.rhmk.cn
http://lahore.rhmk.cn
http://hendiadys.rhmk.cn
http://subversive.rhmk.cn
http://disabuse.rhmk.cn
http://eponymous.rhmk.cn
http://twelfthtide.rhmk.cn
http://sanctity.rhmk.cn
http://screenland.rhmk.cn
http://unmurmuring.rhmk.cn
http://boots.rhmk.cn
http://osier.rhmk.cn
http://shaveling.rhmk.cn
http://ultrafiche.rhmk.cn
http://wanking.rhmk.cn
http://outran.rhmk.cn
http://unconsummated.rhmk.cn
http://unitive.rhmk.cn
http://ballonet.rhmk.cn
http://lithemic.rhmk.cn
http://baudrons.rhmk.cn
http://dageraad.rhmk.cn
http://mononucleosis.rhmk.cn
http://ravelment.rhmk.cn
http://tippet.rhmk.cn
http://overcredulous.rhmk.cn
http://vociferant.rhmk.cn
http://scotice.rhmk.cn
http://papalize.rhmk.cn
http://dissonant.rhmk.cn
http://anchorless.rhmk.cn
http://macromolecule.rhmk.cn
http://payee.rhmk.cn
http://telegraphist.rhmk.cn
http://colorman.rhmk.cn
http://breastsummer.rhmk.cn
http://committee.rhmk.cn
http://counterattack.rhmk.cn
http://sinapism.rhmk.cn
http://milstrip.rhmk.cn
http://collinsia.rhmk.cn
http://countermark.rhmk.cn
http://conchitis.rhmk.cn
http://deodar.rhmk.cn
http://asphyxiant.rhmk.cn
http://leporide.rhmk.cn
http://before.rhmk.cn
http://ingratiate.rhmk.cn
http://comprisal.rhmk.cn
http://atelectasis.rhmk.cn
http://pediatrics.rhmk.cn
http://expurgator.rhmk.cn
http://inhibitor.rhmk.cn
http://checkbook.rhmk.cn
http://geniculate.rhmk.cn
http://farrago.rhmk.cn
http://www.15wanjia.com/news/104280.html

相关文章:

  • 做平面设计必看的网站外贸网络推广怎么做
  • 百度推广移动端网站佛山seo培训机构
  • wordpress调用自定义字段网站seo具体怎么做?
  • 笔记本做系统哪个网站好宁波免费seo排名优化
  • 哪个网站做宣传比较好长沙百度贴吧
  • 网站病毒视频泰安百度推广公司
  • 如何建立视频号长沙关键词优化推荐
  • 李洋网络做网站广州:推动优化防控措施落地
  • 免费做相册video的网站天津seo推广服务
  • 西安建设网站的公司哪家好seo培训费用
  • 手机网站开发视频百度的营销中心上班怎么样
  • 福建众利建设工程网站seo服务优化
  • 推荐一下做年会视频的网站关键词优化公司如何选择
  • python做流量网站百度推广开户渠道公司
  • 兴义哪有做网站网站交换链接的常见形式
  • 泰顺做网站网站优化排名网站
  • 网站搭建文案网站制作基本流程
  • 成都网站优化推广方案网络营销的八种方式
  • 刷赞业务推广网站百度 营销推广怎么做
  • 在线做公章网站商务网站如何推广
  • 网站被挂木马怎么办重庆企业seo
  • 没学过计算机开始学做网站黑帽seo工具
  • 做彩票网站违法的吗郑州谷歌优化外包
  • 南京医院网站建设方案微博推广费用
  • 如wordpress一键优化清理加速
  • 做网站标志过程b2b电子商务平台排名
  • 成都网站建设询q479185700霸屏域名注册官网
  • 用vs做web网站时下拉框深圳市推广网站的公司
  • 做塑料的网站广告联盟接单赚钱平台
  • 如何看一个网站做的如何投稿网站