为什么80%的码农都做不了架构师?>>>   

/*** 功能描述:Hibernate共通字段拦截(直接通过sql语句来新增、修改表)** @author :** 修改历史:(修改人,修改时间,修改原因/内容)*/
@Component("publicFieldInterceptor")public class PublicFieldInterceptor extends EmptyInterceptor {/*** serialVersionUID*/private static final long serialVersionUID = -611066897476314368L;/*** HttpServletRequest*/@Autowired(required = false)private HttpServletRequest request;@Overridepublic String onPrepareStatement(String sql) {if(InterceptorUtil.isExcludePublicFieldTables(sql)){return super.onPrepareStatement(sql);}//更新Pattern pattern = Pattern.compile("UPDATE\\s+", Pattern.CASE_INSENSITIVE); Matcher mathcer = pattern.matcher(sql);if(mathcer.find()){if(InterceptorUtil.isOnlyInsertPublicFieldTables(sql)){return super.onPrepareStatement(sql);}// 当前用户IDString userId = LoginUserConstant.getLoginUser().getSysUserId();// 程序IDString progId = StringUtils.substringBetween(request.getServletPath(), "/").toUpperCase();// SQL语句后面增加共通字段Pattern p = Pattern.compile("WHERE",Pattern.CASE_INSENSITIVE);String[] sqls = p.split(sql);StringBuffer sqlStrBuffer = new StringBuffer(sqls[0]);// 判断是否已经有共通字段if(!InterceptorUtil.isHavePublicField(sql, "UPDATECOUNT", true)){sqlStrBuffer.append(",UPDATECOUNT=NVL(UPDATECOUNT,0)+1");}if(!InterceptorUtil.isHavePublicField(sql, "UPDATEDATE", true)){sqlStrBuffer.append(",UPDATEDATE=SYSDATE");}if(!InterceptorUtil.isHavePublicField(sql, "UPDATEUSERID", true)){sqlStrBuffer.append(",UPDATEUSERID=").append("'").append(userId).append("'");}if(!InterceptorUtil.isHavePublicField(sql, "UPDATEPROGRAMID", true)){sqlStrBuffer.append(",UPDATEPROGRAMID=").append("'").append(progId).append("'");}if(sqls.length > 1 && !StringUtil.isEmpty(sqls[1])){sqlStrBuffer.append(" WHERE ");sqlStrBuffer.append(sqls[1]);}sql = sqlStrBuffer.toString();}else{//插入pattern = Pattern.compile("INSERT\\s+", Pattern.CASE_INSENSITIVE);mathcer = pattern.matcher(sql);if(mathcer.find()){if(InterceptorUtil.isOnlyUpdatePublicFieldTables(sql)){return super.onPrepareStatement(sql);}// 当前用户IDString userId = LoginUserConstant.getLoginUser().getSysUserId();// 程序IDString progId = StringUtils.substringBetween(request.getServletPath(), "/").toUpperCase();//SQL语句后面增加共通字段Pattern p = Pattern.compile("values",Pattern.CASE_INSENSITIVE);String[] sqls = p.split(sql);StringBuffer sqlStrBuffer = new StringBuffer();int bracesLastIndex0 = sqls[0].lastIndexOf(")");int bracesLastIndex1 = sqls[1].lastIndexOf(")");sqlStrBuffer.append(sqls[0].substring(0, bracesLastIndex0));if(!InterceptorUtil.isHavePublicField(sql, "UPDATECOUNT", false)){sqlStrBuffer.append(",UPDATECOUNT");}if(!InterceptorUtil.isHavePublicField(sql, "INSERTDATE", false)){sqlStrBuffer.append(",INSERTDATE");}if(!InterceptorUtil.isHavePublicField(sql, "INSERTUSERID", false)){sqlStrBuffer.append(",INSERTUSERID");}if(!InterceptorUtil.isHavePublicField(sql, "INSERTPROGRAMID", false)){sqlStrBuffer.append(",INSERTPROGRAMID");}if(!InterceptorUtil.isHavePublicField(sql, "UPDATEDATE", false)){sqlStrBuffer.append(",UPDATEDATE");}if(!InterceptorUtil.isHavePublicField(sql, "UPDATEUSERID", false)){sqlStrBuffer.append(",UPDATEUSERID");}if(!InterceptorUtil.isHavePublicField(sql, "UPDATEPROGRAMID", false)){sqlStrBuffer.append(",UPDATEPROGRAMID");}sqlStrBuffer.append(") values");sqlStrBuffer.append(sqls[1].substring(0, bracesLastIndex1));if(!InterceptorUtil.isHavePublicField(sql, "UPDATECOUNT", false)){sqlStrBuffer.append(",0");}if(!InterceptorUtil.isHavePublicField(sql, "INSERTDATE", false)){sqlStrBuffer.append(",SYSDATE");}if(!InterceptorUtil.isHavePublicField(sql, "INSERTUSERID", false)){sqlStrBuffer.append(",").append("'").append(userId).append("'");}if(!InterceptorUtil.isHavePublicField(sql, "INSERTPROGRAMID", false)){sqlStrBuffer.append(",").append("'").append(progId).append("'");}if(!InterceptorUtil.isHavePublicField(sql, "UPDATEDATE", false)){sqlStrBuffer.append(",SYSDATE");}if(!InterceptorUtil.isHavePublicField(sql, "UPDATEUSERID", false)){sqlStrBuffer.append(",").append("'").append(userId).append("'");}if(!InterceptorUtil.isHavePublicField(sql, "UPDATEPROGRAMID", false)){sqlStrBuffer.append(",").append("'").append(progId).append("'");}sqlStrBuffer.append(")");sql = sqlStrBuffer.toString();}}return super.onPrepareStatement(sql);}}

其中的具体代码应对应具体的业务及数据库表的设计。

/*** 功能描述:拦截工具类** @author :** 修改历史:(修改人,修改时间,修改原因/内容)*/
public class InterceptorUtil {/*** * 功能描述:判断是否没有共通字段** @author :* 创建日期 :2014年1月6日 上午11:01:32** @param str SQL或Entity类名* @return** 修改历史 :(修改人,修改时间,修改原因/内容)*/public static boolean isExcludePublicFieldTables(String str){if(Constant.EXCLUDE_PUBLIC_FIELD_TABLES == null || Constant.EXCLUDE_PUBLIC_FIELD_TABLES.length == 0){return false;}for(String exludeTable :Constant.EXCLUDE_PUBLIC_FIELD_TABLES){Pattern pattern = Pattern.compile(exludeTable, Pattern.CASE_INSENSITIVE); Matcher mathcer = pattern.matcher(str);if(mathcer.find()){return true;}}return false;}/*** * 功能描述:判断是否只有新增共通字段** @author :* 创建日期 :2014年1月6日 上午11:01:32** @param str SQL或Entity类名* @return** 修改历史 :(修改人,修改时间,修改原因/内容)*/public static boolean isOnlyInsertPublicFieldTables(String str){if(Constant.ONLY_INSERT_PUBLIC_FIELD_TABLES == null || Constant.ONLY_INSERT_PUBLIC_FIELD_TABLES.length == 0){return false;}for(String onlyTable :Constant.ONLY_INSERT_PUBLIC_FIELD_TABLES){Pattern pattern = Pattern.compile(onlyTable, Pattern.CASE_INSENSITIVE); Matcher mathcer = pattern.matcher(str);if(mathcer.find()){return true;}}return false;}/*** * 功能描述:判断是否只有修改共通字段** @author :* 创建日期 :2014年1月6日 上午11:01:32** @param str SQL或Entity类名* @return** 修改历史 :(修改人,修改时间,修改原因/内容)*/public static boolean isOnlyUpdatePublicFieldTables(String str){if(Constant.ONLY_UPDATE_PUBLIC_FIELD_TABLES == null || Constant.ONLY_UPDATE_PUBLIC_FIELD_TABLES.length == 0){return false;}for(String onlyTable :Constant.ONLY_UPDATE_PUBLIC_FIELD_TABLES){Pattern pattern = Pattern.compile(onlyTable, Pattern.CASE_INSENSITIVE); Matcher mathcer = pattern.matcher(str);if(mathcer.find()){return true;}}return false;}/*** * 功能描述:判断是否已经有该共通字段** @author :* 创建日期 :2014年1月6日 上午11:01:32** @param sql SQL* @param field 字段名称* @param isUpdate true-更新操作 false-插入操作* @return** 修改历史 :(修改人,修改时间,修改原因/内容)*/public static boolean isHavePublicField(String sql, String field, boolean isUpdate){String patternStr = field;if(isUpdate){patternStr = patternStr + "\\s*=";}else{patternStr = ",?\\s*" + patternStr;}Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE); Matcher mathcer = pattern.matcher(sql);if(mathcer.find()){return true;}return false;}}

转载于:https://my.oschina.net/pingdy/blog/192469

Hibernate 拦截器 SQL语句提交前 更新表公共字段相关推荐

  1. SQL语句判断数据库、表、字段是否存在

    --判断[TestDB]是否存在 if exists(select 1 from master..sysdatabases where name='TestDB')     print 'TestDB ...

  2. mysql中通过sql语句查询指定数据表的字段信息

      mysql数据库在安装完成时,自动创建了information_schema.mysql.test这三个数据库.其中,information_schema记录了创建的所有数据库的相关信息,因此可以 ...

  3. Hibernate 拦截器 Hibernate 监听器

    Hibernate拦截器(Interceptor)与事件监听器(Listener) 拦截器(Intercept):与Struts2的拦截器机制基本一样,都是一个操作穿过一层层拦截器,每穿过一个拦截器就 ...

  4. 根据hibernate拦截器实现可配置日志的记录

    对于日志和事件的记录在每个项目中都会用到,如果在每个manager层中触发时间记录的话,会比较难以扩展和维护,所以可配置的日志和事件记录在项目中会用到! 首先在spring的配置文件中加入hibern ...

  5. hibernate oracle驱动,出错场景是升级oracle驱动,将版本从ojdbc14升级到ojdbc6,hibernate执行原生态sql语句会报如下错误...

    出错场景是升级oracle驱动,将版本从ojdbc14升级到ojdbc6,hibernate执行原生态sql语句会报如下错误: org.hibernate.MappingException: No D ...

  6. Hibernate 拦截器实例

    Hibernate 在操作数据库的时候要执行很多操作,这些动作对用户是透明的.这些操作主要是有拦截器和时间组成 hibernate拦截器可以拦截大多数动作,比如事务开始之后(afterTransact ...

  7. Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例

    如何用一条sql语句实现批量更新?mysql并没有提供直接的方法来实现批量更新,但是可以用点小技巧来实现. 代码如下: UPDATE mytable SET myfield = CASE id WHE ...

  8. Hibernate使用原生SQL语句(left join左连接查询)

    Hibernate使用原生SQL语句 以下是本人对Hibernate使用原生SQL语句的理解: 在项目开发当中使用Hibernate提供的HQL有时候不能满足需求,尤其是多表查询或者是多表中没创建主外 ...

  9. MySQL基础教程 包含SQL语句、约束、表关系、设计范式、多表、事务等

    简介 数据库 ​ 数据库(Database):是按照数据结构来组织.存储和管理数据的仓库.每个数据库都有一个或多个不同的 API 用于创建,访问,管理,搜索和复制所保存的数据. ​ 我们也可以将数据存 ...

  10. sql语句用变量替换表名_使用内存优化表替换SQL临时表和表变量

    sql语句用变量替换表名 TempDB usage can be considered as a performance bottleneck for workloads that use SQL t ...

最新文章

  1. SAP外币评估 fagl_fc_val 多评估与少评估问题
  2. linux_mint语言卡住,使用linux mint 16的容易死机怎么处理?
  3. 德标螺纹规格对照表_抚顺船用外螺纹铜减压阀带NK船检证书
  4. Hadoop伪分布式配置和搭建,hadoop单机安装,wordcount实例测试,hadoop安装java目录怎么找,问题及问题解决方法
  5. json string 格式_GO小知识之如何做JSON美化
  6. 【设计模式】工厂模式 Factory Pattern
  7. 推理集 —— 特殊的空间
  8. 华为鸿蒙系统英语报纸_“鸿蒙”系统的英文名叫这个!华为注册的这些《山海经》神兽都该怎么翻译?...
  9. 最牛逼android上的图表库MpChart(三) 条形图
  10. 超简单!利用查看源代码+正则表达式复制百度文库无法复制内容
  11. 调用情迁机器人发送微信或者QQ消息等即时通讯消息
  12. 期货日内交易小经验-开仓篇
  13. Electron安装报错解决办法
  14. Flutter InkWell Ink组件
  15. Graccvs文件正文提取开发组件--文件内容搜索的利器
  16. strcmp( )函数
  17. Apad Qzone项目总结(一)---发布!!!
  18. 21个微信快速加好友方法
  19. 功能安全标准ISO26262-2翻译
  20. 农历与西历对照、万年历

热门文章

  1. Matplotlib 入门(详看注释)
  2. 树分类、线性回归和树回归的感性认知
  3. 机器学习笔记--基本概念
  4. 页面置换算法(FIFO、LRU、LFU)c++实现
  5. oracle学习笔记汇总
  6. java课时,java学习笔记_课时一
  7. 3dmax导出fbx没有贴图_实例讲解ArcGIS 与 3DMax 结合建模
  8. Vue.use 写多个_西双版纳能写立项报告收费公司
  9. java动作触发声音_关于鼠标动作的声音如何添加
  10. python编程从入门到实践pdf_【送书PDF】Python编程从入门到实践