Hiberante Interceptor

本文介绍了一个Java拦截器类,用于在加载、刷新脏数据和保存时加密和解密Worker实体类中的电话号码属性。通过CryptoUtils工具类实现加解密操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java 代码
  1. public class CryptoInterceptor extends EmptyInterceptor {   
  2.   
  3.     public boolean onLoad(Object entity, Serializable id, Object[] state,   
  4.             String[] propertyNames, Type[] types) {   
  5.         if (entity instanceof Worker) {   
  6.             for (int i = 0; i < propertyNames.length; i++) {   
  7.                 if ("wkrPhone".equals(propertyNames[i])) {   
  8.                     String phone = (String) state[i];   
  9.                     if (phone != null && !"".equals(phone)) {   
  10.                         state[i] = CryptoUtils.decrypt(phone);   
  11.                     }   
  12.                 }   
  13.             }   
  14.         }   
  15.         return true;   
  16.     }   
  17.   
  18.     public boolean onFlushDirty(Object entity, Serializable id, Object[] state,   
  19.             String[] propertyNames, Type[] types) {   
  20.         if (entity instanceof Worker) {   
  21.             for (int i = 0; i < propertyNames.length; i++) {   
  22.                 if ("wkrPhone".equals(propertyNames[i])) {   
  23.                     String phone = (String) state[i];   
  24.                     if (phone != null && !"".equals(phone)) {   
  25.                         state[i] = CryptoUtils.encrypt(phone);   
  26.                     }   
  27.                 }   
  28.             }   
  29.         }   
  30.         return true;   
  31.     }   
  32.   
  33.     public boolean onSave(Object entity, Serializable id, Object[] state,   
  34.             String[] propertyNames, Type[] types) {   
  35.         if (entity instanceof Worker) {   
  36.             for (int i = 0; i < propertyNames.length; i++) {   
  37.                 if ("wkrPhone".equals(propertyNames[i])) {   
  38.                     String phone = (String) state[i];   
  39.                     if (phone != null && !"".equals(phone)) {   
  40.                         state[i] = CryptoUtils.encrypt(phone);   
  41.                     }   
  42.                 }   
  43.             }   
  44.         }   
  45.         return true;   
  46.     }   
  47.   
  48.     public int[] findDirty(Object entity, Serializable id,   
  49.             Object[] currentState, Object[] previousState,   
  50.             String[] propertyNames, Type[] types) {    
  51.         boolean forceUpdate = false;   
  52.   
  53.         int[] results = null;   
  54.         int count = 0;   
  55.         if (entity instanceof Worker) {   
  56.             for (int i = 0; i < types.length; i++) {   
  57.                 if (propertyNames[i].equalsIgnoreCase("wkrPhone")) {   
  58.                     String currentPhone = (String) currentState[i];   
  59.                     String previusPhone = (String) previousState[i];   
  60.                     if (currentPhone != null && !"".equals(currentPhone)) {   
  61.                         if (!currentPhone.equals(previusPhone)) {   
  62.                             forceUpdate = true;   
  63.                             currentState[i] = CryptoUtils.encrypt(currentPhone);   
  64.                         }   
  65.                     }   
  66.                 }   
  67.   
  68.                 if (results == null) {   
  69.                     results = new int[types.length];   
  70.                 }   
  71.                 results[count++] = i;   
  72.             }   
  73.         }   
  74.   
  75.         if (count == 0) {   
  76.             return null;   
  77.         } else {   
  78.             int[] trimmed = new int[count];   
  79.             System.arraycopy(results, 0, trimmed, 0, count);   
  80.             if (forceUpdate) {   
  81.                 return trimmed;   
  82.             } else {   
  83.                 return null;   
  84.             }   
  85.         }   
  86.     }   
  87. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值