hibernate EmptyInterceptor审计日志实现

2 篇文章 0 订阅
2 篇文章 0 订阅
@MappedSuperclass
public abstract class BaseAuditModel extends BaseCrmModel{
	/**
	 * UID
	 */
	private static final long serialVersionUID = 8886136517524378574L;

	public abstract String[][] getAuditPropertys(); //需要审计的字段名称
	
}

 

@Entity
@Table(name = "crm_user")
public class UserModel extends BaseAuditModel{

	/**
	 * UID
	 */
	private static final long serialVersionUID = 1L;
	
	private String userName; //用户名
	private String mobile;	//手机号
	private String address; //住址

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Override
	public String[][] getAuditPropertys() {
		//需要审计的字段/解释
		return new String[][]{{"userName", "用户名"}, {"mobile","手机号"}, {"address", "住址"}};
	}

	@Override
	public String getModelName() {
		return "用户信息";
	}

}

 

public class LogEntityInterceptor extends EmptyInterceptor {
	/**
	 * UID
	 */
	private static final long serialVersionUID = 1L;
	
	private Logger log = Logger.getLogger(LogEntityInterceptor.class);
	
	@Override
	public void onDelete(Object entity, Serializable id, Object[] state,
			String[] propertyNames, Type[] types) {

		log.info("删除数据 onDelete...");
		
	}
	
	@Override
	public boolean onSave(Object entity, Serializable id, Object[] state,
			String[] propertyNames, Type[] types) {
		log.info("保存数据 onSave...");
		return false;
	}
	
	
	@Override
	public boolean onFlushDirty(Object entity, Serializable id,
			Object[] currentState, Object[] previousState,
			String[] propertyNames, Type[] types) {
		log.info("修改数据 onFlushDirty..." + entity.getClass());
		if(entity instanceof BaseAuditModel){
			BaseAuditModel audit = (BaseAuditModel) entity;
			if(audit.getAuditPropertys() != null && previousState != null){
				log.info(audit.getModelName());
				log.info("id: "+audit.getId());
				Map<String, String> map = MapUtils.putAll(new HashMap(), audit.getAuditPropertys());
				for(int i=0, len=previousState.length; i<len; i++){
					if(previousState[i]!=null 
							&& map.get(propertyNames[i]) != null
							&& !previousState[i].equals(currentState[i])){
						log.info(propertyNames[i]+"/"+map.get(propertyNames[i])+":原值="+previousState[i]+",新值="+currentState[i]);
					}
				}
			}
		}
		return false;
	}
	
}

 

<!-- Hibernate审计日志拦截器 -->
<bean id="logEntityInterceptor" class="com.hzwealth.app.crm.LogEntityInterceptor" />

<bean id="sessionFactory"  ............. >
   ................
   <property name="entityInterceptor" ref="logEntityInterceptor"/>
   ................
</bean>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值