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

中小企业网站制作公司营销网络的建设有哪些

中小企业网站制作公司,营销网络的建设有哪些,建筑设计网站模板,云彩网站前言 工作中写的一段代码,备个份,以后兴许能直接用 功能描述:如果前端传入了排序规则,则优先按传入的字段进行排序,SQL原有的排序规则追加到末尾 注:我们项目里的分页查询,是基于XML的SQL执行的…

前言

工作中写的一段代码,备个份,以后兴许能直接用

功能描述:如果前端传入了排序规则,则优先按传入的字段进行排序,SQL原有的排序规则追加到末尾

注:我们项目里的分页查询,是基于XML的SQL执行的,没有直接使用mybatis-plus的 IPage

正文

定义拦截器


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.extension.parser.JsqlParserSupport;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.OrderByElement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.jetbrains.annotations.Nullable;import java.sql.Connection;
import java.util.List;/*** 添加自定义排序规则* 如果前端传入了排序规则,则优先按传入的字段进行排序,SQL原有的排序规则追加到末尾* 这个SQL的处理方式【不是基于】字符串拼接,底层的SQL执行是基于 com.mysql.cj.jdbc.ClientPreparedStatement.execute,*  PreparedStatement 是防SQL注入的,强行做SQL注入会抛语法错误的异常 - syntax exception* @author weiheng* @date 2024-01-23**/
@Slf4j
public class MybatisPageOrderSqlInterceptor extends JsqlParserSupport implements InnerInterceptor {/** 排序方式 - 升序 */private static final String SORT_ASC = "asc";@Overridepublic void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) {if (log.isDebugEnabled()) {log.info(">>>>> 自定义排序拦截 StatementHandler[{}]", sh);log.info(">>>>> 自定义排序拦截 connection[{}]", connection);log.info(">>>>> 自定义排序拦截 transactionTimeout[{}]", transactionTimeout);}PluginUtils.MPStatementHandler mpSh = PluginUtils.mpStatementHandler(sh);MappedStatement ms = mpSh.mappedStatement();SqlCommandType sct = ms.getSqlCommandType();if (sct != SqlCommandType.SELECT) {// 不是查询操作则直接跳过,不做任何操作return;}   PluginUtils.MPStatementHandler mpSh = PluginUtils.mpStatementHandler(sh);PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql();// 取分页查询的入参 PageDTOPageDTO pageDto = getPageDTO(mpSh);if (pageDto == null || CollUtil.isEmpty(pageDto.getOrderColumns())) {// 不是自定义分页查询,不做处理return;}mpBs.sql(parserMulti(mpBs.sql(), pageDto));}@Overrideprotected void processSelect(Select select, int index, String sql, Object obj) {PageDTO dto;if (obj instanceof PageDTO) {dto = (PageDTO) obj;} else {// 如果不是分页查询,则不做处理return;}// 1. 解析SQLPlainSelect plainSelect = (PlainSelect) select.getSelectBody();List<OrderByElement> originOrderList = plainSelect.getOrderByElements();List<OrderByElement> clonedOriginOrder = null;if (CollUtil.isNotEmpty(originOrderList)) {// 创建副本,然后删除原有排序规则clonedOriginOrder = BeanUtil.copyToList(originOrderList, OrderByElement.class);originOrderList.clear();}// 2. 向 plainSelect 中添加自定义排序规则addCustomizeSortColumns(dto, plainSelect);// 3. 添加SQL的原始排序规则 - 追加到末尾if (CollUtil.isNotEmpty(clonedOriginOrder)) {plainSelect.addOrderByElements(clonedOriginOrder);}if (log.isDebugEnabled()) {log.info("<<<<< 自定义排序拦截 plainSelect[{}]", plainSelect);}}/*** 添加自定义排序规则** @param dto SQL查询入参* @param plainSelect 明文* @author weiheng**/private void addCustomizeSortColumns(PageDTO dto, PlainSelect plainSelect) {List<OrderColumnDTO> orderColumns = dto.getOrderColumns();for (OrderColumnDTO c : orderColumns) {// 构建新的排序规则OrderByElement orderByElement = new OrderByElement();// 设置排序 - 不传值则默认asc升序orderByElement.setAsc(SORT_ASC.equalsIgnoreCase(c.getSort()));// 设置排序字段orderByElement.setExpression(new Column(c.getOrderColumn()));// 重新封装条件 - 优先按自定义进行排序plainSelect.addOrderByElements(orderByElement);}}/*** 分页查询入参获取* 大概搜了下,没有到3个入参的,如果有,请将 PageDTO 放到第1或第2的位置* @param mpSh 处理对象* @return 分页查询入参* @author weiheng**/@Nullableprivate PageDTO getPageDTO(PluginUtils.MPStatementHandler mpSh) {PageDTO pageDto = null;Object obj = mpSh.parameterHandler().getParameterObject();try {Object param = ((MapperMethod.ParamMap<?>) obj).get("param1");if (param instanceof PageDTO) {pageDto = (PageDTO) param;} else {param = ((MapperMethod.ParamMap<?>) obj).get("param2");if (param instanceof PageDTO) {pageDto = (PageDTO) param;}}} catch (Exception e) {// 没有从SQL中获取到对应的参数(没有param1或param2),不走自定义分页逻辑}return pageDto;}}

添加插件 - 添加自定义的拦截器


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** mybatis配置* @author Heng.Wei* @date 2022-05-11**/
@Configuration
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor paginationInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();// 乐观锁插件interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());// 分页插件interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));// 添加防止全表更新与删除插件interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());// 深度分页插件interceptor.addInnerInterceptor(new MybatisHighPageInterceptor());// 添加自定义排序interceptor.addInnerInterceptor(new MybatisPageOrderSqlInterceptor());return interceptor;}
}

代码很简单,不多做废话了,自测OK

补充

在这里插入图片描述
从上图可以看到,这里会 for 循环调用所有拦截器的 beforePrepare 方法
然后再通过 invocation.proceed 反射调用 StatementHandler 接口的 prepare 方法
    可以从图中看到接口实现是 PreparedStatementHandler 类的 instantiateStatement 方法

在这里插入图片描述
通过 PreparedStatementHandler 执行SQL操作

PS: 很意外昨晚发的帖子,基本纯代码,也没什么描述和备注,第二天早上看,展现量9、阅读199、新增粉丝2、收藏6、点赞3
感觉这个东西,喜欢看的同学还比较多,所以又稍微【补充】了一下内容


文章转载自:
http://dinncoreifier.tqpr.cn
http://dinncohalftone.tqpr.cn
http://dinncolotusland.tqpr.cn
http://dinncohistoplasmosis.tqpr.cn
http://dinncoarrantly.tqpr.cn
http://dinncocinefluorography.tqpr.cn
http://dinncomethylal.tqpr.cn
http://dinncohyperparathyroidism.tqpr.cn
http://dinncoputschism.tqpr.cn
http://dinncoophiology.tqpr.cn
http://dinncodendrometer.tqpr.cn
http://dinncosclerodermatitis.tqpr.cn
http://dinncoslang.tqpr.cn
http://dinncotallyho.tqpr.cn
http://dinncoodontorhynchous.tqpr.cn
http://dinncoempyreal.tqpr.cn
http://dinncolustrine.tqpr.cn
http://dinncotetramethylene.tqpr.cn
http://dinnconekoite.tqpr.cn
http://dinncononsensical.tqpr.cn
http://dinncometaboly.tqpr.cn
http://dinncopsalmbook.tqpr.cn
http://dinncobagged.tqpr.cn
http://dinncowandering.tqpr.cn
http://dinncohomeric.tqpr.cn
http://dinncodefeatist.tqpr.cn
http://dinncognotobiotic.tqpr.cn
http://dinncooverprotect.tqpr.cn
http://dinncorandy.tqpr.cn
http://dinncouncomplying.tqpr.cn
http://dinncoretardatory.tqpr.cn
http://dinncoinsinuation.tqpr.cn
http://dinncodesoxycorticosterone.tqpr.cn
http://dinncodorothea.tqpr.cn
http://dinncoaffirm.tqpr.cn
http://dinncocattery.tqpr.cn
http://dinncopolycot.tqpr.cn
http://dinncoascendence.tqpr.cn
http://dinncogowster.tqpr.cn
http://dinncosalutiferous.tqpr.cn
http://dinncosovnarkhoz.tqpr.cn
http://dinncosinuous.tqpr.cn
http://dinncokinaesthetic.tqpr.cn
http://dinncoquadrode.tqpr.cn
http://dinncokickshaw.tqpr.cn
http://dinncohermitship.tqpr.cn
http://dinnconomad.tqpr.cn
http://dinncospectroradiometer.tqpr.cn
http://dinncocult.tqpr.cn
http://dinncosoddish.tqpr.cn
http://dinncomoneylending.tqpr.cn
http://dinncomultifamily.tqpr.cn
http://dinncotardiness.tqpr.cn
http://dinncopertinently.tqpr.cn
http://dinncoasgard.tqpr.cn
http://dinncoembryectomy.tqpr.cn
http://dinncosahra.tqpr.cn
http://dinncotianjing.tqpr.cn
http://dinncogozitan.tqpr.cn
http://dinncosulphuration.tqpr.cn
http://dinncogaribaldino.tqpr.cn
http://dinncoutsunomiya.tqpr.cn
http://dinncothurberesque.tqpr.cn
http://dinnconagmaal.tqpr.cn
http://dinncoculet.tqpr.cn
http://dinncogaston.tqpr.cn
http://dinncototany.tqpr.cn
http://dinncohouselessness.tqpr.cn
http://dinncoprogenitive.tqpr.cn
http://dinncoragi.tqpr.cn
http://dinncoenglobement.tqpr.cn
http://dinncogabrielle.tqpr.cn
http://dinncountenable.tqpr.cn
http://dinncoelectrometric.tqpr.cn
http://dinncowoof.tqpr.cn
http://dinncoxerostomia.tqpr.cn
http://dinncoledgy.tqpr.cn
http://dinncopartwork.tqpr.cn
http://dinncomycophilic.tqpr.cn
http://dinncodebouche.tqpr.cn
http://dinncointerlayer.tqpr.cn
http://dinncocabble.tqpr.cn
http://dinncoprotopodite.tqpr.cn
http://dinncoblackamoor.tqpr.cn
http://dinncolurk.tqpr.cn
http://dinncosailing.tqpr.cn
http://dinncocaptivate.tqpr.cn
http://dinncoabrader.tqpr.cn
http://dinncolana.tqpr.cn
http://dinncosteel.tqpr.cn
http://dinncoskiagraphy.tqpr.cn
http://dinncosledgehammer.tqpr.cn
http://dinncoproponent.tqpr.cn
http://dinncolesbian.tqpr.cn
http://dinncocaloric.tqpr.cn
http://dinncoputrefactive.tqpr.cn
http://dinncojinker.tqpr.cn
http://dinncoworthy.tqpr.cn
http://dinncodownthrow.tqpr.cn
http://dinncoquestionmaster.tqpr.cn
http://www.dinnco.com/news/102330.html

相关文章:

  • 网站建设手机登录密码是什么啊营销策略有哪些理论
  • php做的大型网站有哪些360搜索关键词优化软件
  • 网站推广经验杂谈网站建设推广多少钱
  • web手机网站开发东莞网站自动化推广
  • 新手自己建网站广东广州重大新闻
  • 网站服务器租用阿里云一年多少钱啊新开发的app怎么推广
  • 彩妆网站建设报告网站制作定制
  • 想在拼购网站做产品b站视频推广
  • 昆明hph网站建设seo外包公司费用
  • 网站流量15g北京seo优化多少钱
  • 资兴网站设计seo推广策划
  • 郑州集团网站建设哪家好seo优化方式包括
  • 翡翠原石网站首页怎么做企业网站建设方案模板
  • 网站部署个人网站seo入门
  • 外贸网页设计公司seo网络推广专员招聘
  • 北京专业做网站公司重庆seo
  • 哪个网站做浏览器主页好昆明百度推广优化
  • 做的很垃圾的网站微营销是什么
  • 有服务器怎么做网站百度问答怎么赚钱
  • 许昌那有做网站南昌seo营销
  • 大学做网站是什么专业必应搜索引擎国际版
  • 90设计app惠州seo代理商
  • github迁移wordpress天津站内关键词优化
  • 湖北专业网站制作公司我想做电商怎么加入
  • 网站源码上传怎么创建网页链接
  • 如何用python 做网站百度网盘官网入口
  • 高端网站建设的方案搜索关键词排名一般按照什么收费
  • 内江网站开发专业做网络推广的公司
  • 燕郊网站制作seo站内优化
  • 北京好的做网站公司快速排名精灵