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

网站建设等级定级在家做衣服的网站

网站建设等级定级,在家做衣服的网站,有关建设旅游网站的公司,长沙科技有限公司前言 看到这个标题有人就要说了#xff0c;D哥啊#xff0c;MybatisPlus不是本来就有逻辑删除的配置吗#xff0c;比如TableLogic注解#xff0c;配置文件里也能添加如下配置设置逻辑删除。 mybatis-plus:mapper-locations: classpath*:mapper/*.xmlconfiguration:mapUnd…前言 看到这个标题有人就要说了D哥啊MybatisPlus不是本来就有逻辑删除的配置吗比如TableLogic注解配置文件里也能添加如下配置设置逻辑删除。 mybatis-plus:mapper-locations: classpath*:mapper/*.xmlconfiguration:mapUnderscoreToCamelCase: trueglobal-config:db-config:logic-delete-field: dellogic-delete-value: 1logic-notDelete-value: 0 但是我想说xml中添加了逻辑删除了吗很明显这个没有MybatisPlus只在QueryWrapper中做了手脚而xml是Mybatis的功能非MybatisPlus的功能。而xml中写SQL又是我工作中最常用到的优势在于SQL可读性强结合MybatisX插件并在IDEA中连接database后能够直接跳转到方法和表且对多表join和子查询支持都比QueryWrapper来得好。而逻辑删除又是会经常漏掉的字段虽然说手动添加也不费多少时间但是麻烦的是容易漏掉特别是子查询和join的情况而且时间积少成多我觉得有必要解决这个问题。 本插件适用于绝大部分表都拥有逻辑删除字段的情况 本文使用的MybatisPlus的版本为3.5.3.1 不多说了直接上代码 /*** author DCT* version 1.0* date 2023/11/9 23:10:18* description*/ Slf4j public class DeleteMpInterceptor implements InnerInterceptor {public static final String LOGIC_DELETE LOGIC_DELETE;public static final ListString excludeFunctions new ArrayList();protected String deleteFieldName;public DeleteMpInterceptor(String deleteFieldName) {this.deleteFieldName deleteFieldName;}static {excludeFunctions.add(selectList);excludeFunctions.add(selectById);excludeFunctions.add(selectBatchIds);excludeFunctions.add(selectByMap);excludeFunctions.add(selectOne);excludeFunctions.add(selectCount);excludeFunctions.add(selectObjs);excludeFunctions.add(selectPage);excludeFunctions.add(selectMapsPage);}SneakyThrowsOverridepublic void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {if (InterceptorIgnoreHelper.willIgnoreOthersByKey(ms.getId(), LOGIC_DELETE)) {return;}if (StringUtils.isBlank(deleteFieldName)) {// INFO: Zhouwx: 2023/11/20 没有设置逻辑删除的字段名也忽略添加逻辑删除return;}PluginUtils.MPBoundSql mpBs PluginUtils.mpBoundSql(boundSql);String sql mpBs.sql();Statement statement CCJSqlParserUtil.parse(sql);if (!(statement instanceof Select)) {return;}String id ms.getId();int lastIndexOf id.lastIndexOf(.);String functionName id.substring(lastIndexOf 1);if (excludeFunctions.contains(functionName)) {// INFO: DCT: 2023/11/12 QueryWrapper的查询本来就会加逻辑删除不需要再加了return;}Select select (Select) statement;SelectBody selectBody select.getSelectBody();// INFO: DCT: 2023/11/12 处理核心业务handleSelectBody(selectBody);// INFO: DCT: 2023/11/12 将处理完的数据重新转为MPBoundSqlString sqlChange statement.toString();mpBs.sql(sqlChange);}protected void handleSelectBody(SelectBody selectBody) {if (selectBody instanceof PlainSelect) {PlainSelect plainSelect (PlainSelect) selectBody;// INFO: DCT: 2023/11/12 处理join中的内容handleJoins(plainSelect);Expression where plainSelect.getWhere();FromItem fromItem plainSelect.getFromItem();EqualsTo equalsTo getEqualTo(fromItem);if (where null) {// INFO: DCT: 2023/11/12 where条件为空增加一个where且赋值为 表名.del 0plainSelect.setWhere(equalsTo);} else {// INFO: DCT: 2023/11/12 普通where后面直接加上本表的 表名.del 0 AndExpression andExpression new AndExpression(where, equalsTo);plainSelect.setWhere(andExpression);// INFO: DCT: 2023/11/12 有子查询的处理子查询 handleWhereSubSelect(where);}}}/*** 这一段来自MybatisPlus的租户插件源码通过递归的方式加上需要的SQL** param where*/protected void handleWhereSubSelect(Expression where) {if (where null) {return;}if (where instanceof FromItem) {processOtherFromItem((FromItem) where);return;}if (where.toString().indexOf(SELECT) 0) {// 有子查询if (where instanceof BinaryExpression) {// 比较符号 , and , or , 等等BinaryExpression expression (BinaryExpression) where;handleWhereSubSelect(expression.getLeftExpression());handleWhereSubSelect(expression.getRightExpression());} else if (where instanceof InExpression) {// inInExpression expression (InExpression) where;Expression inExpression expression.getRightExpression();if (inExpression instanceof SubSelect) {handleSelectBody(((SubSelect) inExpression).getSelectBody());}} else if (where instanceof ExistsExpression) {// existsExistsExpression expression (ExistsExpression) where;handleWhereSubSelect(expression.getRightExpression());} else if (where instanceof NotExpression) {// not existsNotExpression expression (NotExpression) where;handleWhereSubSelect(expression.getExpression());} else if (where instanceof Parenthesis) {Parenthesis expression (Parenthesis) where;handleWhereSubSelect(expression.getExpression());}}}/*** 处理子查询等*/protected void processOtherFromItem(FromItem fromItem) {// 去除括号while (fromItem instanceof ParenthesisFromItem) {fromItem ((ParenthesisFromItem) fromItem).getFromItem();}if (fromItem instanceof SubSelect) {SubSelect subSelect (SubSelect) fromItem;if (subSelect.getSelectBody() ! null) {// INFO: Zhouwx: 2023/11/20 递归从select开始查找handleSelectBody(subSelect.getSelectBody());}} else if (fromItem instanceof ValuesList) {log.debug(Perform a subQuery, if you do not give us feedback);} else if (fromItem instanceof LateralSubSelect) {LateralSubSelect lateralSubSelect (LateralSubSelect) fromItem;if (lateralSubSelect.getSubSelect() ! null) {SubSelect subSelect lateralSubSelect.getSubSelect();if (subSelect.getSelectBody() ! null) {// INFO: Zhouwx: 2023/11/20 递归从select开始查找handleSelectBody(subSelect.getSelectBody());}}}}protected void handleJoins(PlainSelect plainSelect) {ListJoin joins plainSelect.getJoins();if (joins null) {return;}for (Join join : joins) {// INFO: DCT: 2023/11/12 获取表别名并获取 表.del 0FromItem rightItem join.getRightItem();EqualsTo equalsTo getEqualTo(rightItem);// INFO: DCT: 2023/11/12 获取on右边的表达式Expression onExpression join.getOnExpression();// INFO: DCT: 2023/11/12 给表达式增加 and 表.del 0AndExpression andExpression new AndExpression(onExpression, equalsTo);ArrayListExpression expressions new ArrayList();expressions.add(andExpression);// INFO: DCT: 2023/11/12 目前的这个表达式就是and后的表达式了不用再增加原来的表达式因为这个and表达式已经包含了原表达式所有的内容了join.setOnExpressions(expressions);}}protected EqualsTo getEqualTo(FromItem fromItem) {Alias alias fromItem.getAlias();String aliasName ;if (alias null) {if (fromItem instanceof Table) {Table table (Table) fromItem;aliasName table.getName();}} else {aliasName alias.getName();}EqualsTo equalsTo new EqualsTo();Column leftColumn new Column();leftColumn.setColumnName(aliasName . deleteFieldName);equalsTo.setLeftExpression(leftColumn);equalsTo.setRightExpression(new LongValue(0));return equalsTo;} } 代码说明 这代码中已经有很多注释了其实整段代码并非100%我的原创而是借鉴了MybatisPlus自己的TenantLineInnerIntercept因为两者的功能实际上非常详尽我依葫芦画瓢造了一个特别是中间的handleWhereSubSelect方法令人拍案叫绝使用大量递归通过非常精简的代码处理完了SQL查询中所有的子查询。 getEqualTo方法可以算是逻辑删除插件的核心代码了先判断表是否存在别名如果有就拿别名如果没有就拿表名防止出现多个表都有逻辑删除字段的情况下指代不清的情况出现查询ambiguous错误。通过net.sf.jsqlparser中的EqualsTo表达式拼上字段名和未被逻辑删除的值0这里可以按照自己的情况进行修改 顶上的excludeFunctions是为了排除QueryWrapper的影响因为QueryWrapper自己会加上逻辑删除而这个插件还会再添加一个逻辑删除导致出现重复打印出来的SQL不美观。 使用方法 和MybatisPlus其他的插件一样都通过Bean的方式进行配置 Value(${mybatis-plus.global-config.db-config.logic-delete-field})private String deleteFieldName;Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new DeleteMpInterceptor(deleteFieldName));return interceptor;}我这里的deleteFieldName直接借用了MybatisPlus自己的逻辑删除配置可以自己设置 排除使用这个插件 如果有几张表是没有逻辑删除字段的那么这个插件自动补的逻辑删除字段则会导致SQL出现报错可以通过添加以下注解InterceptorIgnore排除插件对于该方法的生效 Repository interface ExampleMapper : BaseMapperExampleEntity {InterceptorIgnore(others [DeleteMpInterceptor.LOGIC_DELETE true])fun findByList(query: ExampleQo): ListExampleListVo } 上面的代码是Kotlin写的但是不妨碍查看 运行效果 mapper中代码为 /*** author DCTANT* version 1.0* date 2023/11/20 17:40:41* description*/ Repository interface ExampleMapper : BaseMapperExampleEntity {fun findByList(query: ExampleQo): ListExampleListVo } xml中的代码为 select idfindByList resultTypecom.itdct.server.admin.example.vo.ExampleListVoselect t.* from test_example as twhereif testname ! null and name ! and t.name #{name}/ifif testnumber ! nulland t.number #{number}/ifif testkeyword ! null and keyword ! and t.name like concat(%,#{keyword},%)/ifif teststartTime ! nulland t.create_time gt; #{startTime}/ifif testendTime ! nulland t.create_time lt; #{endTime}/if/whereorder by t.create_time desc/select 执行效果为 t.del 0就是逻辑删除插件添加的代码当然join和子查询我也使用了目前来看没有什么问题 欢迎大家提出修改意见 目前这个代码还处于Demo阶段并没有上线使用还是处于没充分测试的状态欢迎大家提出整改意见如果有bug我也会及时修复。
http://www.lebaoying.cn/news/11065.html

相关文章:

  • 海纳企业网站建设免费奖励代码网站
  • 营销型网站首页模板wordpress+widget+开发
  • 村级网站建设系统wordpress 精简优化
  • 成都设计网站的公司神童预言新冠2023结束
  • 适合做网站服务器的主机昔阳做网站公司
  • 业余学做衣服上哪个网站长春网站建设的公司
  • 网站怎么做json数据简单网站 快速建设
  • 建行网站用户名是什么网站建设实习每天内容
  • 廊坊网站建设佛山厂商备案个人网站名称
  • 如何上传模板到网站游戏开发需要具备哪些技术
  • 技术提供微信网站开发临沧市住房和城乡建设局门户网站
  • 免费发广告的网站大全a站在线观看人数在哪
  • 品牌型网站的特点商城网站建设制作
  • 门户网站的种类html网页源代码查看
  • 巢湖路桥建设集团网站wordpress 禁用可视化
  • 网站建设运营的成本wordpress中数据库配置文件
  • dede网站源码一建 建设网站首页
  • 网站正在建设中 蓝色网站怎么做域名
  • 女人做一级a网站免费如何设计公司标志图案
  • 织梦手机网站怎么仿制免费个人网站在线制作
  • 成都注册公司核名网站农产品网站设计
  • 网站建设的途径不良网站进入窗口免费正能量
  • 天津企业免费建站网络策略
  • 网站后台怎么制作网站外链发布
  • 门户网站开发研究报告网站优化与seo
  • 网站设计确认书网站自己做推广
  • 龙华网站制作百度云网站建设视频
  • 品牌网站如何做seoapp官网入口
  • 智慧团建网站入口官网wordpress中文免费主题
  • 做标书网站增加网站关键词