/**
* 功能描述:Hibernate共通字段拦截(直接通过sql语句来新增、修改表)
*
* @author :
*
* 修改历史:(修改人,修改时间,修改原因/内容)
*/
@Component("publicFieldInterceptor")
public class PublicFieldInterceptor extends EmptyInterceptor {
/**
* serialVersionUID
*/
private static final long serialVersionUID = -611066897476314368L;
/**
* HttpServletRequest
*/
@Autowired(required = false)
private HttpServletRequest request;
@Override
public 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);
}
// 当前用户ID
String userId = LoginUserConstant.getLoginUser().getSysUserId();
// 程序ID
String 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);
}
// 当前用户ID
String userId = LoginUserConstant.getLoginUser().getSysUserId();
// 程序ID
String 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;
}
}