interceptor拦截器典型应用实例----数据稽核

Interceptor拦截器的使用小结,程序实例是典型的 “数据稽核”过程,即在对数据库中的数据进行修改后会自动添加对应的日志记录在数据库中对应的日志表中,用于记录对数据库中关键数据的修改过程和追踪,最重要的应用主要是日志记录功能,将业务层的逻辑处理和数据访问层的处理分离开来。功能类似于spring 中的aop功能。

Pojo类代码:Tuser类

package com.inspur.po;

import java.util.HashSet;

import java.util.Set;

/**

* TUser entity. @author MyEclipse Persistence Tools

*/

public class TUser implements java.io.Serializable {

// Fields

private Integer id;

private String name;

private Integer age;

private Set TAddresses = new HashSet(0);

// Constructors

/** default constructor */

public TUser() {

}

/** full constructor */

public TUser(String name, Integer age, Set TAddresses) {

this.name = name;

this.age = age;

this.TAddresses = TAddresses;

}

// Property accessors

public Integer getId() {

return this.id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return this.name;

}

public void setName(String name) {

this.name = name;

}

public Integer getAge() {

return this.age;

}

public void setAge(Integer age) {

this.age = age;

}

public Set getTAddresses() {

return this.TAddresses;

}

public void setTAddresses(Set TAddresses) {

this.TAddresses = TAddresses;

}

}

Taddress类代码:

package com.inspur.po;

/**

* TAddress entity. @author MyEclipse Persistence Tools

*/

public class TAddress implements java.io.Serializable {

// Fields

private Integer id;

private TUser TUser;

private String address;

private String zipcode;

private String street;

// Constructors

/** default constructor */

public TAddress() {

}

/** full constructor */

public TAddress(TUser TUser, String address, String zipcode, String street) {

this.TUser = TUser;

this.address = address;

this.zipcode = zipcode;

this.street = street;

}

// Property accessors

public Integer getId() {

return this.id;

}

public void setId(Integer id) {

this.id = id;

}

public TUser getTUser() {

return this.TUser;

}

public void setTUser(TUser TUser) {

this.TUser = TUser;

}

public String getAddress() {

return this.address;

}

public void setAddress(String address) {

this.address = address;

}

public String getZipcode() {

return this.zipcode;

}

public void setZipcode(String zipcode) {

this.zipcode = zipcode;

}

public String getStreet() {

return this.street;

}

public void setStreet(String street) {

this.street = street;

}

}

Tauditlog类代码:

package com.inspur.po;

/**

* TAuditlog entity. @author MyEclipse Persistence Tools

*/

public class TAuditlog implements java.io.Serializable {

// Fields

private Integer id;

private String user;

private String action;

private String entityname;

private String commit;

private Integer actiontflag;

// Constructors

/** default constructor */

public TAuditlog() {

}

/** full constructor */

public TAuditlog(String user, String action, String entityname,

String commit, Integer actiontflag) {

this.user = user;

this.action = action;

this.entityname = entityname;

this.commit = commit;

this.actiontflag = actiontflag;

}

// Property accessors

public Integer getId() {

return this.id;

}

public void setId(Integer id) {

this.id = id;

}

public String getUser() {

return this.user;

}

public void setUser(String user) {

this.user = user;

}

public String getAction() {

return this.action;

}

public void setAction(String action) {

this.action = action;

}

public String getEntityname() {

return this.entityname;

}

public void setEntityname(String entityname) {

this.entityname = entityname;

}

public String getCommit() {

return this.commit;

}

public void setCommit(String commit) {

this.commit = commit;

}

public Integer getActiontflag() {

return this.actiontflag;

}

public void setActiontflag(Integer actiontflag) {

this.actiontflag = actiontflag;

}

}

日志记录类代码:

package com.inspur.dao;

import java.sql.Connection;

import java.util.Iterator;

import java.util.Set;

import org.hibernate.CallbackException;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

import com.inspur.po.TAuditlog;

import com.inspur.po.TUser;

public class AuditLogDaoImpl {

public static void doLog(String action,String userId,Set modifyset,Connection connection){

Configuration config=new Configuration().configure();

SessionFactory sessionFactory=config.buildSessionFactory();

//重用interceptor中的session的jdbc Connection创建临时session

Session session=sessionFactory.openSession(connection);

Iterator it=modifyset.iterator();

TAuditlog auditLog=new TAuditlog();

TUser user=null;

try{

while(it.hasNext()){

user=(TUser)it.next();

auditLog.setAction(action);

auditLog.setCommit(user.getName());

auditLog.setUser(userId);

auditLog.setActiontflag(user.getId());

//此次不用开启transaction的原因就是临时session重用了当前session的jdbc connection,而

//外围已经开启了当前session的 jdbc connection

session.save(auditLog);

//此次的临时session是不关联到拦截器的,所以不会触发拦截器中的 postflush方法。

session.flush();

}

}catch(Exception e){

throw new CallbackException(e);

}finally{

try{

if(session!=null){

session.close();

}

}catch(Exception e1){

throw new CallbackException(e1);

}

}

}

}

拦截器类代码:

package com.inspur.interceptor;

import java.io.Serializable;

import java.util.HashSet;

import java.util.Iterator;

import java.util.Set;

import org.hibernate.CallbackException;

import org.hibernate.EntityMode;

import org.hibernate.HibernateException;

import org.hibernate.Interceptor;

import org.hibernate.Session;

import org.hibernate.Transaction;

import org.hibernate.type.Type;

import com.inspur.dao.AuditLogDaoImpl;

import com.inspur.po.TUser;

public class MyInterceptor implements Interceptor{

private Session session;

private String userId;

private Set insertSet=new HashSet();

private Set updateSet=new HashSet();

public Session getSession() {

return session;

}

public void setSession(Session session) {

this.session = session;

}

public String getUserId() {

return userId;

}

public void setUserId(String userId) {

this.userId = userId;

}

public Set getInsertSet() {

return insertSet;

}

public void setInsertSet(Set insertSet) {

this.insertSet = insertSet;

}

public Set getUpdateSet() {

return updateSet;

}

public void setUpdateSet(Set updateSet) {

this.updateSet = updateSet;

}

@Override

public void afterTransactionBegin(Transaction arg0) {

// TODO Auto-generated method stub

}

@Override

public void afterTransactionCompletion(Transaction arg0) {

// TODO Auto-generated method stub

}

@Override

public void beforeTransactionCompletion(Transaction arg0) {

// TODO Auto-generated method stub

}

@Override

public int[] findDirty(Object arg0, Serializable arg1, Object[] arg2,

Object[] arg3, String[] arg4, Type[] arg5) {

// TODO Auto-generated method stub

return null;

}

@Override

public Object getEntity(String arg0, Serializable arg1)

throws CallbackException {

// TODO Auto-generated method stub

return null;

}

@Override

public String getEntityName(Object arg0) throws CallbackException {

// TODO Auto-generated method stub

return null;

}

@Override

public Object instantiate(String arg0, EntityMode arg1, Serializable arg2)

throws CallbackException {

// TODO Auto-generated method stub

return null;

}

@Override

public Boolean isTransient(Object arg0) {

// TODO Auto-generated method stub

return null;

}

@Override

public void onCollectionRecreate(Object arg0, Serializable arg1)

throws CallbackException {

// TODO Auto-generated method stub

}

@Override

public void onCollectionRemove(Object arg0, Serializable arg1)

throws CallbackException {

// TODO Auto-generated method stub

}

@Override

public void onCollectionUpdate(Object arg0, Serializable arg1)

throws CallbackException {

// TODO Auto-generated method stub

}

@Override

public void onDelete(Object arg0, Serializable arg1, Object[] arg2,

String[] arg3, Type[] arg4) throws CallbackException {

// TODO Auto-generated method stub

}

@Override

public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState,

Object[]previousState , String[] propertyNames, Type[] types) throws CallbackException {

if(entity instanceof TUser){

updateSet.add(entity);

}

return false;

}

@Override

public boolean onLoad(Object arg0, Serializable arg1, Object[] arg2,

String[] arg3, Type[] arg4) throws CallbackException {

// TODO Auto-generated method stub

return false;

}

@Override

public String onPrepareStatement(String arg0) {

// TODO Auto-generated method stub

return arg0;

}

@Override

public boolean onSave(Object entity, Serializable id, Object[] state,

String[] propertyNames, Type[] types) throws CallbackException {

if(entity instanceof TUser){

insertSet.add(entity);

}

return false;

}

/*

* 所有的sql都执行完成以后,即事物的提交完成以后再执行一次postflush方法,不与session的动作相关联

* 而是作为整个session生命周期结束的最终执行方法*/

@Override

public void postFlush(Iterator entity) throws CallbackException {

try{

if(insertSet.size()>0){

AuditLogDaoImpl.doLog("insert", userId, insertSet, session.connection());

System.out.println(insertSet.size());

}

if(updateSet.size()>0){

AuditLogDaoImpl.doLog("update", userId, updateSet, session.connection());

}

}catch(HibernateException e){

e.printStackTrace();

}

}

@Override

public void preFlush(Iterator arg0) throws CallbackException {

// TODO Auto-generated method stub

}

}

Test类代码如下:

package com.inspur.test;

import java.util.List;

import org.hibernate.HibernateException;

/*

* 程序的总体执行过程是:利用当前session的jdbc connection创建一个临时的session用于将日志实体对象

* 保存到数据库中,在临时session中不需要开启和关闭事物,因为当前session的jdbc connection已经在

* 之前开启了,重用connection可以是一个事物只占用一个数据库连接,提供数据访问性能,在interceptor中

* 是不能调用当前session的,因为interceptor中的所有方法都是由当前session负责调用的,如果在方法中调用

* 当前session进行数据持久化操作,将导致session内部状态混乱。*/

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import com.inspur.interceptor.MyInterceptor;

import com.inspur.po.TUser;

import junit.framework.TestCase;

public class InterceptorTest extends TestCase {

Session session=null;

@Override

protected void setUp() throws Exception {

Configuration config=new Configuration().configure();

SessionFactory sessionFactory=config.buildSessionFactory();

MyInterceptor interceptor_cus=new MyInterceptor();

session=sessionFactory.openSession(interceptor_cus);

interceptor_cus.setSession(session);

interceptor_cus.setUserId("current user");

}

@Override

protected void tearDown() throws Exception {

//     session.close();

}

public void testInterceptor(){

TUser user=new TUser();

user.setName("uuu_sky");

Transaction tran=null;

try{

tran=session.beginTransaction();

session.save(user);

//如果此处使用session.flush()方法那么实际上是调用了两次dolog方法,在对应的日志数据库中会

//插入两条记录,因为tran.commit()实际上包含了执行一次session.flush方法

//         session.flush();

tran.commit();

}catch(HibernateException e){

e.printStackTrace();

tran.rollback();

}finally {

if(session!=null){

session.close();

}

}

}

public void testUpdate(){

String hql="from TUser user where user.name=? ";

Query query=session.createQuery(hql);

query.setString(0, "uuu_sky");

List list=query.list();

TUser user=(TUser)list.get(0);

System.out.println(user.getName());

user.setName("liuzhiqiang");

Transaction tran=session.beginTransaction();

try{

session.update(user);

tran.commit();

}catch(HibernateException e){

e.printStackTrace();

tran.rollback();

}finally{

if(session!=null){

try{

session.close();

}catch(HibernateException e){

e.printStackTrace();

}

}

}

}

}

最终的执行效果是向数据库中插入数据 和修改数据库中的现有数据都会将每次操作记录进数据库日志表中,保存当前操作的日志记录。

posted on 2013-03-20 22:19 moonfans 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/moonfans/archive/2013/03/20/2972188.html

interceptor拦截器典型应用实例----数据稽核相关推荐

  1. Mybatis Interceptor 拦截器

    拦截器(Interceptor)在 Mybatis 中被当做插件(plugin)对待,官方文档提供了 Executor(拦截执行器的方法),ParameterHandler(拦截参数的处理),Resu ...

  2. struts 2——拦截器的实现实例

    struts 2--拦截器的实现实例 目录 说明 配置文件web.xml 配置文件struts.xml action类login.java 实体类user.java 拦截器myinterceptor. ...

  3. 框架:SpringMVC中Interceptor拦截器的两种实现

    Spring中使用Interceptor拦截器 SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证, ...

  4. spring拦截器覆盖_Spring中使用Interceptor拦截器

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

  5. SpringMVC中使用Interceptor拦截器

    2019独角兽企业重金招聘Python工程师标准>>> SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理. ...

  6. springMVC之Interceptor拦截器

    转自:https://blog.csdn.net/qq_25673113/article/details/79153547 Interceptor拦截器用于拦截Controller层接口,表现形式有点 ...

  7. springboot Interceptor拦截器excludePathPatterns失效

    springboot Interceptor拦截器excludePathPatterns失效 使用jwt和HandlerInterceptorAdapter做登录的拦截放行失效,其实更多时候不是配置文 ...

  8. 【Java代码】使用 org.apache.ibatis.plugin.Interceptor 拦截器实现全局 mapper.xml 参数注入(可用于切换数据库实例schema+Demo举例源码)

    1. why   项目没有使用MyBatis,进行数据操作时使用的是jdbc中默认的schema,现在项目要加入多租户,同一个数据库下不同租户使用不同的实例schema,这就要在mapper文件内所有 ...

  9. Struts2 拦截器中实现对数据进行数据库操作

    功能需求说明: 用拦截器的方式,拦截以.action结尾的类,同时记录访问的action名称.action的包路径.action后面带的参数.操作者的ID.操作者的姓名.等其他数据. 吐槽点: 1.当 ...

最新文章

  1. 【mysql学习笔记整理】
  2. 大型软件公司.net面试题!一定得看(附答案)
  3. 八句经典座右铭必有一句适合你
  4. BOOST_CONSTANTS_GENERATE宏相关用法的测试程序
  5. linux编程之pipe()函数
  6. Java并发教程–线程池
  7. .net多层结构 sql注入
  8. file_get_contents请求失败处理_SpringCloud Gateway网关处理请求过程中遇到400Bad Request问题解决方案...
  9. impala的substr从第几位截到最后一位_冰雪奇缘2彩蛋:片名内含深意,艾莎是第13位公主象征着背叛...
  10. 高性能数据库集群:读写分离
  11. 如何使用iMazing为iPad创建配置文件
  12. 任正非:HR面试不深刻导致大规模进人、走人,耽误人家几年对得起人家吗?...
  13. LOJ2874 JOISC2014 历史研究 分块、莫队
  14. gitkraken点击Glo出现白屏的情况,回退回去的解决办法
  15. 基于Android的上位软件,基于Android的电子套结机上位机软件设计
  16. eplan p8详细安装步骤文库_EPLAN P8 2.1.6 安装指南
  17. 性能测试案例模板 性能测试用例模板
  18. 精读《Excel JS API》
  19. sv中program和module区别
  20. 九、Appium测试入门

热门文章

  1. autofac文档:服务类型,名称和键
  2. TCP三次握手及四次挥手详细图解
  3. Redis数据库入门基础,及优缺点介绍
  4. go中 -strconv包的使用
  5. SystemTap工具的使用基础
  6. centos创建vsftpd虚拟用户
  7. Appium环境搭建简介
  8. JAVA String 相加编译器发生了什么?
  9. iOS网络 把数据存入钥匙串
  10. iOS开发网络篇—使用ASI框架进行文件下载