MyBatis攔截器:給參數(shù)對象屬性賦值的實(shí)例
該攔截器的作用:在進(jìn)行增加、修改等操作時(shí),給數(shù)據(jù)模型的一些通用操作屬性(如:創(chuàng)建人、創(chuàng)建時(shí)間、修改人、修改時(shí)間等)自動賦值。
該實(shí)現(xiàn)是在DAO層攔截,即存入DB前最后一層。后經(jīng)分析,不是很合理,改為在service層攔截,用spring AOP來實(shí)現(xiàn)了,該代碼遂棄用。不過已經(jīng)測試可用,記錄備忘。
package com.development; import java.lang.reflect.InvocationTargetException; import java.util.Date; import java.util.Map; import java.util.Properties; import org.apache.commons.beanutils.BeanUtils; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlCommandType; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; /** * 攔截器作用:給各實(shí)體對象在增加、修改時(shí),自動添加操作屬性信息。 */ @Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class }) }) public class OpeInfoInterceptor implements Interceptor { public Object intercept(Invocation invocation) throws Throwable { Object[] args = invocation.getArgs(); System.out.println("-----------參數(shù)攔截---------------------------------------------------"); System.out.println("02 當(dāng)前線程ID:"+Thread.currentThread().getId()); //遍歷處理所有參數(shù),update方法有兩個(gè)參數(shù),參見Executor類中的update()方法。 for(int i=0;i<args.length;i++) { Object arg=args[i]; String className=arg.getClass().getName(); System.out.println(i + " 參數(shù)類型:"+className); //第一個(gè)參數(shù)處理。根據(jù)它判斷是否給“操作屬性”賦值。 if(arg instanceof MappedStatement) {//如果是第一個(gè)參數(shù) MappedStatement MappedStatement ms = (MappedStatement)arg; SqlCommandType sqlCommandType = ms.getSqlCommandType(); System.out.println("操作類型:"+sqlCommandType); if(sqlCommandType == SqlCommandType.INSERT || sqlCommandType==SqlCommandType.UPDATE) {//如果是“增加”或“更新”操作,則繼續(xù)進(jìn)行默認(rèn)操作信息賦值。否則,則退出 continue; } else { break; } } //第二個(gè)參數(shù)處理。(只有第二個(gè)程序才能跑到這) if (arg instanceof Map) {//如果是map,有兩種情況:(1)使用@Param多參數(shù)傳入,由Mybatis包裝成map。(2)原始傳入Map System.out.println("這是一個(gè)包裝過的類型!"); Map map=(Map)arg; for (Object obj : map.values()) { setProperty(obj); } } else {//原始參數(shù)傳入 setProperty(arg); } } return invocation.proceed(); } /** * 為對象的操作屬性賦值 * @param obj */ private void setProperty(Object obj) { try { //TODO: 根據(jù)需要,將相關(guān)屬性賦上默認(rèn)值 BeanUtils.setProperty(obj, "createrUsername", "張三"); BeanUtils.setProperty(obj, "createDT", new Date()); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } public Object plugin(Object target) { return Plugin.wrap(target, this); } public void setProperties(Properties properties) { } }
以上這篇MyBatis攔截器:給參數(shù)對象屬性賦值的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
@Autowired自動裝配,@Bean注入@Primary,@Qualifier優(yōu)先級講解
這篇文章主要介紹了@Autowired自動裝配,@Bean注入@Primary,@Qualifier優(yōu)先級,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09MyBatis-Flex+ShardingSphere-JDBC多數(shù)據(jù)源分庫分表實(shí)現(xiàn)
本文介紹了使用MyBatis-Flex和ShardingSphere-JDBC實(shí)現(xiàn)多數(shù)據(jù)源分庫分表的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10spring cloud服務(wù)連接超時(shí)問題及解決
這篇文章主要介紹了spring cloud服務(wù)連接超時(shí)問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01springboot項(xiàng)目整合注冊功能模塊開發(fā)實(shí)戰(zhàn)
這篇文章主要介紹了springboot項(xiàng)目整合注冊功能模塊開發(fā)實(shí)戰(zhàn),在用戶的注冊是首先需要查詢當(dāng)前的用戶名是否存在,如果存在則不能進(jìn)行注冊,相當(dāng)于一個(gè)查詢語句,本文通過實(shí)例代碼詳細(xì)講解,需要的朋友可以參考下2022-11-11