一劳永逸!!利用拦截器全局实现Mybatisplus去除字符串空格
实现 利用 字符串 全局 去除 空格 拦截器 MyBatisPlus
2023-09-14 09:06:26 时间
对于前端页面提交的数据信息,后台程序要去掉字符串前后面的空格,即TRIM,绝大多数企业应用都需要。
一个一个改?
太麻烦了!
用户在网页输入条件查询数据,同样要去掉空格,否则可能导致查不出来数据。并且,通常,我们在排查这样的问题的时候,也许只有眼明心细的人,才能发现原来是有空格字符导致的。
一个一个改?
太麻烦了!
本文介绍使用拦截器(Interceptor)来实现全局处理。mybatis自带Interceptor接口,我们只需实现接口即可。
mybatisplus或mybatis对于数据库CRUD操作,主要体现在mybatis jar包里 org.apache.ibatis.executor.Executor 接口的两个方法——#update 和 #query,见下面mybatis-3.5.1.jar截图。
我们所需要的拦截的,也正是这两个方法。
mybatis的拦截器是 org.apache.ibatis.plugin.Interceptor ,同样定义在mybatis jar中。
查询数据拦截器处理
查询数据拦截Executor.query方法。对于mybatisplus,请求参数一般是lambda表达式,所以我们对lambda表达式携带的数据进行改造。
代码如下:
package com.emaxcard.car.config; import com.baomidou.mybatisplus.core.conditions.AbstractWrapper; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.binding.MapperMethod.ParamMap; import org.apache.ibatis.cache.CacheKey; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlCommandType; import org.apache.ibatis.plugin.*; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.springframework.stereotype.Component; import java.util.Iterator; import java.util.Map; import java.util.Properties; /** * mybatis拦截器,去掉字符串参数的前后空格 * * @Author gz.zhang * @Date 2020-09-29 * @see {https://www.cnblogs.com/buguge/p/13749101.html} */ @Slf4j @Component @Intercepts({ @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class})}) public class MybatisQueryInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; String sqlId = mappedStatement.getId(); log.debug("------sqlId------" + sqlId); SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType(); log.debug("------sqlCommandType------" + sqlCommandType); Object parameter = invocation.getArgs()[1]; if (parameter == null || SqlCommandType.SELECT != sqlCommandType) { return invocation.proceed(); } if (parameter instanceof ParamMap && ((ParamMap) parameter).get("ew") instanceof AbstractWrapper) { Map nameValuePairs = ((AbstractWrapper) ((ParamMap) parameter).get("ew")).getParamNameValuePairs(); Iterator<Map.Entry<String, Object>> iterator = nameValuePairs.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, Object> next = iterator.next(); if (next.getValue() instanceof String) { next.setValue(((String) next.getValue()).trim()); } } } return invocation.proceed(); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { // TODO Auto-generated method stub } }
插入数据的拦截器处理
插入数据\修改数据统一拦截Executor.update方法。请求参数一般是pojo对象,利用反射来修改属性的值。
关键代码如下:
@Slf4j @Component @Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})}) public class MybatisInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; String sqlId = mappedStatement.getId(); log.debug("------sqlId------" + sqlId); SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType(); Object parameter = invocation.getArgs()[1]; log.debug("------sqlCommandType------" + sqlCommandType); if (parameter == null) { return invocation.proceed(); } if (SqlCommandType.INSERT == sqlCommandType || SqlCommandType.UPDATE == sqlCommandType) { Field[] fields = oConvertUtils.getAllFields(parameter); for (Field field : fields) { if (field.getType().equals(String.class)) { field.setAccessible(true); Object o = field.get(parameter); field.setAccessible(false); String newVal = o == null ? "" : String.valueOf(o).trim(); field.setAccessible(true); field.set(parameter, newVal); field.setAccessible(false); } } } return invocation.proceed(); } }
测试
下面testcase用来查询数据。同样插入也OK。
相关文章
- Java+MyEclipse+Tomcat (二)配置Servlet及简单实现表单提交
- Java实现LeetCode 839. 相似字符串组 (深度优先搜索,并查集,图)
- Java实现第八届蓝桥杯正则问题
- Java实现背包问题
- nginx利用lua实现nginx反向代理proxy_store缓存文件自删除
- 利用动态图层实现数据的实时显示
- 利用动态图层实现数据的实时显示(arcEngine IDynamiclayer)
- SAP CRM 产品主数据搜索时的权限检查实现 - Product search authorization check
- NHibernate利用Mindscape.NHibernateModelDesigner实现数据库与实体之间的转换及操作
- 分享一个开源的JavaScript统计图表库,40行代码实现专业统计图表
- XAI之ALE:基于titanic泰坦尼克数据集对RF算法利用ALE累积局部效应图可视化算法进而实现模型可解释性案例
- CV之IS:基于pixellib库和coco数据集且加载.h5文件利用deeplabv3和xception算法实现图像语义分割/图像分割简单代码全实现(以热播电视剧《庆余年》视频片段为例)案例应用
- NLP:利用count函数或正则表达式compile、findall、finditer实现匹配统计(包括模糊匹配的贪婪匹配、懒惰匹配)、对多个字符串组成的列表进行多个模糊关键词进行模糊匹配案例
- Wps:wps中excel表格数据整理常用技巧—利用wps表格实现自动填充等差数列然后预测数值、将python语言中的列表格式数据存为excel内
- JavaSE进阶 | 深入理解多线程(进程与线程、实现线程的方式、线程生命周期)
- Qt利用ffmpeg实现音视频同步
- 灰狼优化算法训练多层感知神经网络研究(Matlab代码实现)
- 使用带外空间信息选择毫米波波束(Matlab代码实现)
- 【AI写作】《如何利用 RPA 实现自动化获客?》
- ArrayList实现线程的几种方法
- Python+Appium实现APP自动化测试
- 利用Powershell和ceye.io实现Windows账户密码回传
- 利用MCU实现制作一台蓝牙控制小车方法
- ML之RL:基于MovieLens电影评分数据集利用强化学习算法(多臂老虎机+EpsilonGreedy策略)实现对用户进行Top电影推荐案例