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

b2c网站主要功能流程东莞网站建设制作

b2c网站主要功能流程,东莞网站建设制作,南通启益建设集团有限公司网站,中国建工社微课程官网前言 看到这个标题有人就要说了,D哥啊,MybatisPlus不是本来就有逻辑删除的配置吗,比如TableLogic注解,配置文件里也能添加如下配置设置逻辑删除。 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 List<String> 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");}@SneakyThrows@Overridepublic 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) {List<Join> 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);ArrayList<Expression> 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 : BaseMapper<ExampleEntity> {@InterceptorIgnore(others = [DeleteMpInterceptor.LOGIC_DELETE + "@true"])fun findByList(query: ExampleQo): List<ExampleListVo>
}

上面的代码是Kotlin写的,但是不妨碍查看

运行效果

mapper中代码为:

/*** @author DCTANT* @version 1.0* @date 2023/11/20 17:40:41* @description*/
@Repository
interface ExampleMapper : BaseMapper<ExampleEntity> {fun findByList(query: ExampleQo): List<ExampleListVo>
}

xml中的代码为:

    <select id="findByList" resultType="com.itdct.server.admin.example.vo.ExampleListVo">select t.* from test_example as t<where><if test="name != null and name != ''">and t.name = #{name}</if><if test="number != null">and t.number = #{number}</if><if test="keyword != null and keyword != ''">and t.name like concat('%',#{keyword},'%')</if><if test="startTime != null">and t.create_time &gt; #{startTime}</if><if test="endTime != null">and t.create_time &lt; #{endTime}</if></where>order by t.create_time desc</select>

执行效果为:

t.del = 0就是逻辑删除插件添加的代码,当然join和子查询我也使用了,目前来看没有什么问题

欢迎大家提出修改意见

目前这个代码还处于Demo阶段,并没有上线使用,还是处于没充分测试的状态,欢迎大家提出整改意见!如果有bug我也会及时修复。

http://www.tj-hxxt.cn/news/102005.html

相关文章:

  • jsp企业网站源码四川网络推广推广机构
  • 营销型的网站企业网站seo外包公司有哪些
  • cms网站群管理系统seo诊断的网络问题
  • 大型搬家门户网站源码最新互联网项目平台网站
  • 重庆专业网站建设费用黑帽seo之搜索引擎
  • 蒙城做网站的公司百度推广话术全流程
  • 网站建设软件开发东莞seo优化排名
  • logo网站广州市口碑全网推广报价
  • 上海做网站的公司有哪些网站建设流程图
  • 用windows搭建手机网站哪里有学市场营销培训班
  • 正规新闻网站哪家好泰安做网站公司
  • 买卖网站建设赛雷猴是什么意思
  • 网站开发实现的功能广州seo关键词优化外包
  • wamp网站根目录配置2023年11月新冠高峰
  • 青岛住房和城乡建设委员会官方网站整合营销传播策略
  • Python电影网站开发百度收录入口在哪里
  • 网站建设投资资金济南seo优化外包服务公司
  • 网站外链分析工具网站友情链接美化代码
  • 建设网站的英语怎么说微信推广费用一般多少
  • php无版权企业网站管理系统seo免费系统
  • mac markdown 转 wordpress网站seo重庆
  • 法院门户网站建设方案推广费用一般多少钱
  • 上海网站群建设百度指数数据分析平台
  • 网站 的版面结构可以放友情链接的网站
  • 泗泾做网站公司百度下载免费安装最新版
  • 做网站需要办什么手续微信小程序开发平台官网
  • 泉州公司网站建设sem优化
  • 做网站加班多吗南昌seo顾问
  • 丹东黄页网百度seo入驻
  • 政府微网站建设目标seo代码优化有哪些方法