springboot~mybatis统一处理公有字段

打印 上一主题 下一主题

主题 910|帖子 910|积分 2730

对于实体中包罗有公共字段,像create_at,create_time,update_at,update_time来说,我们没有须要在每个实体的crud操作中复制同样的代码,如许代码的味道很坏,我们应该使用mybatis的拦截器机制,将公共字段统一处理;当然mybatis-puls在实现上更加优雅,它帮我们封装了MetaObjectHandler接口,我们可以重写insertFill和updateFill来完成公共字段的统一填充,详细可看我这篇文章《springboot~MyBatisPlus中使用@TableField完成字段主动填充》。
mybatis中的实现


  • 需要实现org.apache.ibatis.plugin.Interceptor接口
  • 在intercept方法中实现业务核心逻辑
  • 在SqlSessionFactory中注册你的intercept类
  • 拦截器代码
  1. @Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) })
  2. public class FillCreateAndUpdateFieldInterceptor implements Interceptor {
  3.         static final Logger logger = LoggerFactory.getLogger(FillCreateAndUpdateFieldInterceptor.class);
  4.         @Override
  5.         public Object intercept(Invocation invocation) throws Throwable {
  6.                 Object[] args = invocation.getArgs();
  7.                 MappedStatement ms = (MappedStatement) args[0];
  8.                 Object parameter = args[1];
  9.                 if (ms.getId().contains("insert") || ms.getId().contains("update")) {
  10.                         if (SecurityUtils.getAuthentication() != null) {
  11.                                 if (parameter instanceof BaseEntity) {
  12.                                         BaseEntity entity = (BaseEntity) parameter;
  13.                                         if (ms.getId().contains("insert")) {
  14.                                                 entity.setCreateBy(SecurityUtils.getAuthentication().getName());
  15.                                                 entity.setCreateTime(new Date());
  16.                                         }
  17.                                         entity.setUpdateBy(SecurityUtils.getAuthentication().getName());
  18.                                         entity.setUpdateTime(new Date());
  19.                                 }
  20.                  }
  21.         }
  22. }
复制代码

  • 自定义SqlSessionFactory的bean
  1. @Bean
  2. public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
  3.         String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
  4.         String mapperLocations = env.getProperty("mybatis.mapperLocations");
  5.         String configLocation = env.getProperty("mybatis.configLocation");
  6.         typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
  7.         VFS.addImplClass(SpringBootVFS.class);
  8.         final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
  9.         sessionFactory.setDataSource(dataSource);
  10.         sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
  11.         sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
  12.         sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
  13.         SqlSessionFactory result = sessionFactory.getObject();
  14.         // 注册自定义拦截器
  15.         result.getConfiguration().addInterceptor(new FillCreateAndUpdateFieldInterceptor());
  16.         return result;
  17. }
复制代码
通过上面的代码,我们就完成了公共字段的统一处理。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

王海鱼

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表