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

1网站免费建站/牛推网

1网站免费建站,牛推网,科技杭州网站建设,云南创网科技有限公司前两天了解了一下mybatis插件这个东西,为了加深一下印象,突然想给当前代码做一个简化操作,通过插件的形式统一处理createTime,createBy,updateBy。 首先定义一个BaseEntity,里面放入这些参数,需…

前两天了解了一下mybatis插件这个东西,为了加深一下印象,突然想给当前代码做一个简化操作,通过插件的形式统一处理createTime,createBy,updateBy。
首先定义一个BaseEntity,里面放入这些参数,需要处理的实体类只需要继承即可

import java.io.Serializable;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;@Data
public class BaseEntity implements Serializable{private static final long serialVersionUID = 1L;@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private Date updateTime;@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private Date createTime;private String createBy;private String updateBy;}

处理逻辑代码

import java.util.Date;
import java.util.Properties;import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.springframework.stereotype.Component;import com.spring.framework.entity.BaseEntity;
import com.spring.framework.utils.UserUtil;/***  Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)   拦截执行器的方法ParameterHandler (getParameterObject, setParameters)	拦截参数的处理ResultSetHandler (handleResultSets, handleOutputParameters)	拦截结果集的处理StatementHandler (prepare, parameterize, batch, update, query) 拦截Sql语法构建的处理* @author Administrator**/
@Intercepts({@Signature(type = Executor.class,method = "update",args = {MappedStatement.class,Object.class})})
@Component
public class HandleTimeInterceptor implements Interceptor {@Overridepublic Object intercept(Invocation invocation) throws Throwable {Object[] args = invocation.getArgs();MappedStatement mappedStatement = (MappedStatement) args[0];SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();if(sqlCommandType== SqlCommandType.UPDATE) {if(args[1] instanceof BaseEntity) {BaseEntity baseEntity = (BaseEntity) args[1];baseEntity.setUpdateTime(new Date());baseEntity.setUpdateBy(UserUtil.getUsername());}}else if(sqlCommandType==SqlCommandType.INSERT) {if(args[1] instanceof BaseEntity) {BaseEntity baseEntity = (BaseEntity) args[1];baseEntity.setCreateTime(new Date());baseEntity.setCreateBy(UserUtil.getUsername());}}return invocation.proceed();}@Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}@Overridepublic void setProperties(Properties properties) {}
}

简单测试了一下,是没有问题的,具体还存在哪些bug暂时还不知道,先这样吧
参考地址:https://blog.csdn.net/weixin_43850799/article/details/106745494

2020-06-21,代码出bug了,项目中集成了mybatis-plus后,更新数据的时候发现updateBy和updateTime不会被赋值了,debug看了一下,发现由于使用的时候mp自带的修改方法,他把参数结构修改了,所以代码也要随之改变一下,增加个判断。(手写xml和使用tk的时候没有发现这个问题的出现)

import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.utils.SecurityUtils;/*** 公共字段统一处理* @author ljw* 2021年6月19日14:56:21**/
@Intercepts({@Signature(type = Executor.class,method = "update",args = {MappedStatement.class,Object.class})})
public class HandleCommonInterceptor implements Interceptor {@SuppressWarnings("unchecked")@Overridepublic Object intercept(Invocation invocation) throws Throwable {Object[] args = invocation.getArgs();MappedStatement mappedStatement = (MappedStatement) args[0];SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();if(sqlCommandType== SqlCommandType.UPDATE) {System.err.println(args[1]);if(args[1] instanceof BaseEntity) {BaseEntity baseEntity = (BaseEntity) args[1];baseEntity.setUpdateTime(new Date());if(SecurityUtils.getAuthentication()!=null) {//主要处理在执行插入日志的时候会获取不到用户信息,问题暂时不清楚为啥baseEntity.setUpdateBy(SecurityUtils.getUsername());}//调用mp的更新方法时,参数会变成如下形式:{param1=Receive(id=16, dh=1244, qzh=null, postId=1)} 正常的应该是Receive(id=16, dh=1244, qzh=null, postId=1)}else if(args[1] instanceof Map){Map<String,Object> map = (Map<String, Object>) args[1];Object object = getFirstOrNull(map);if(object instanceof BaseEntity) {BaseEntity baseEntity = (BaseEntity) object;baseEntity.setUpdateTime(new Date());if(SecurityUtils.getAuthentication()!=null) {//主要处理在执行插入日志的时候会获取不到用户信息,问题暂时不清楚为啥baseEntity.setUpdateBy(SecurityUtils.getUsername());}}}}else if(sqlCommandType==SqlCommandType.INSERT) {if(args[1] instanceof BaseEntity) {BaseEntity baseEntity = (BaseEntity) args[1];baseEntity.setCreateTime(new Date());if(SecurityUtils.getAuthentication()!=null) {baseEntity.setCreateBy(SecurityUtils.getUsername());}}}return invocation.proceed();}@Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}@Overridepublic void setProperties(Properties properties) {}/*** 获取第一个key的值* @param map* @return*/private static Object getFirstOrNull(Map<String, Object> map) {Object obj = null;for (Entry<String, Object> entry : map.entrySet()) {obj = entry.getValue();if (obj != null) {break;}}return  obj;}
}
http://www.jmfq.cn/news/4772395.html

相关文章:

  • wordpress 后台登录/优化设计英语
  • 公司网站设计案例/盐城seo排名
  • wordpress手机不能显示字体/朝阳区seo技术
  • 网上做网页网站任务赚钱/杭州做百度推广的公司
  • 一个服务器可以做几个网站/seo快速排名软件品牌
  • 小型旅游网站/网站推广系统方案
  • discuz 做企业网站/无锡网络公司
  • 建做一个av网站好/网站搭建的流程
  • wordpress 调用站外api/长尾词seo排名
  • 做口碑都有哪些网站/seo网络优化前景怎么样
  • 湖南长沙网站建设公司电话/大型seo公司
  • 的网站制作/如何提升网站seo排名
  • 做网页的it网站/怎么做推广网站
  • 孝感新闻门户网站/热点新闻事件素材
  • 用织梦做的学校网站/深圳设计公司
  • 长春网站制作专业/沈阳seo
  • 网站建设有关要求/电商平台怎么搭建
  • 武威做网站的/seo优化实训总结
  • 扬州网站建设电话/网站快照优化公司
  • 淘宝网站网页图片怎么做/商丘搜索引擎优化
  • 武汉做网站最好的公司/管理人员需要培训哪些课程
  • 个人网站模板素材/域名注册服务机构
  • 免费做的网站怎么设置域名/经典品牌推广文案
  • 那种漂亮的网站怎么做的/搜索热度查询
  • 房卡app游戏开发/厦门seo优化
  • 西安本地十家做网站建设的公司/流量精灵app
  • 微信公众号微网站怎么做的/今天发生的重大新闻
  • 网站设计做哪些的/链接检测工具
  • 域名解析到网站需要怎么做/优化排名seo
  • 为啥做网站/武汉seo网站