『壹』 mybatis自定義插件要實現什麼介面
竟然Mybatis是對四大介面進行攔截的,那我們葯先要知道Mybatis的四大介面對象 Executor, StatementHandler, ResultSetHandler, ParameterHandler。
上圖Mybatis框架的整個執行過程。Mybatis插件能夠對則四大對象進行攔截,可以包含到了Mybatis一次會議的所有操作。可見Mybatis的的插件很強大。
Executor是 Mybatis的內部執行器,它負責調用StatementHandler操作資料庫,並把結果集通過 ResultSetHandler進行自動映射,另外,他還處理了二級緩存的操作。從這里可以看出,我們也是可以通過插件來實現自定義的二級緩存的。
StatementHandler是Mybatis直接和資料庫執行sql腳本的對象。另外它也實現了Mybatis的一級緩存。這里,我們可以使用插件來實現對一級緩存的操作(禁用等等)。
ParameterHandler是Mybatis實現Sql入參設置的對象。插件可以改變我們Sql的參數默認設置。
ResultSetHandler是Mybatis把ResultSet集合映射成POJO的介面對象。我們可以定義插件對Mybatis的結果集自動映射進行修改。
插件Interceptor
Mybatis的插件實現要實現Interceptor介面,我們看下這個介面定義的方法。
public interface Interceptor {
Object intercept(Invocation invocation) throws Throwable;
Object plugin(Object target);
void setProperties(Properties properties);
}
這個介面只聲明了三個方法。
setProperties方法是在Mybatis進行配置插件的時候可以配置自定義相關屬性,即:介面實現對象的參數配置
plugin方法是插件用於封裝目標對象的,通過該方法我們可以返回目標對象本身,也可以返回一個它的代理,可以決定是否要進行攔截進而決定要返回一個什麼樣的目標對象,官方提供了示例:return Plugin.wrap(target, this);
intercept方法就是要進行攔截的時候要執行的方法
理解這個介面的定義,先要知道java動態代理機制。plugin介面即返回參數target對象(Executor/ParameterHandler/ResultSetHander/StatementHandler)的代理對象。在調用對應對象的介面的時候,可以進行攔截並處理。
Mybatis四大介面對象創建方法
Mybatis的插件是採用對四大介面的對象生成動態代理對象的方法來實現的。那麼現在我們看下Mybatis是怎麼創建這四大介面對象的。
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
//確保ExecutorType不為空(defaultExecutorType有可能為空)
executorType = executorType == null ? defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor executor; if (ExecutorType.BATCH == executorType) {
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this, transaction);
} else {
executor = new SimpleExecutor(this, transaction);
} if (cacheEnabled) {
executor = new CachingExecutor(executor);
}
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}
public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);
statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);
return statementHandler;
}
public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
return parameterHandler;
}
public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler, ResultHandler resultHandler, BoundSql boundSql) {
ResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);
resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);
return resultSetHandler;
}
查看源碼可以發現, Mybatis框架在創建好這四大介面對象的實例後,都會調用InterceptorChain.pluginAll()方法。InterceptorChain對象是插件執行鏈對象,看源碼就知道裡面維護了Mybatis配置的所有插件(Interceptor)對象。
// target --> Executor/ParameterHandler/ResultSetHander/StatementHandler
public Object pluginAll(Object target) {
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target;
}
其實就是安順序執行我們插件的plugin方法,一層一層返回我們原對象(Executor/ParameterHandler/ResultSetHander/StatementHandler)的代理對象。當我們調用四大介面對象的方法時候,實際上是調用代理對象的響應方法,代理對象又會調用十大介面對象的實例。
Plugin對象
我們知道,官方推薦插件實現plugin方法為:Plugin.wrap(target, this);
public static Object wrap(Object target, Interceptor interceptor) {
// 獲取插件的Intercepts註解
Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
Class<?> type = target.getClass();
Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
if (interfaces.length > 0) {
return Proxy.newProxyInstance(type.getClassLoader(), interfaces, new Plugin(target, interceptor, signatureMap));
}
return target;
}
這個方法其實是Mybatis簡化我們插件實現的工具方法。其實就是根據當前攔截的對象創建了一個動態代理對象。代理對象的InvocationHandler處理器為新建的Plugin對象。
插件配置註解@Intercepts
Mybatis的插件都要有Intercepts註解來指定要攔截哪個對象的哪個方法。我們知道,Plugin.warp方法會返回四大介面對象的代理對象(通過new Plugin()創建的IvocationHandler處理器),會攔截所有的執行方法。在代理對象執行對應方法的時候,會調用InvocationHandler處理器的invoke方法。Mybatis中利用了註解的方式配置指定攔截哪些方法。具體如下:
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
Set<Method> methods = signatureMap.get(method.getDeclaringClass());
if (methods != null && methods.contains(method)) {
return interceptor.intercept(new Invocation(target, method, args));
}
return method.invoke(target, args);
} catch (Exception e) {
throw ExceptionUtil.unwrapThrowable(e);
}
}
可以看到,只有通過Intercepts註解指定的方法才會執行我們自定義插件的intercept方法。未通過Intercepts註解指定的將不會執行我們的intercept方法。
官方插件開發方式
@Intercepts({@Signature(type = Executor.class, method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
public class TestInterceptor implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
Object target = invocation.getTarget(); //被代理對象
Method method = invocation.getMethod(); //代理方法
Object[] args = invocation.getArgs(); //方法參數
// do something ...... 方法攔截前執行代碼塊
Object result = invocation.proceed();
// do something .......方法攔截後執行代碼塊
return result;
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
}
以上就是Mybatis官方推薦的插件實現的方法,通過Plugin對象創建被代理對象的動態代理對象。可以發現,Mybatis的插件開發還是很簡單的。
自定義開發方式
Mybatis的插件開發通過內部提供的Plugin對象可以很簡單的開發。只有理解了插件實現原理,對應不採用Plugin對象我們一樣可以自己實現插件的開發。下面是我個人理解之後的自己實現的一種方式。
public class TestInterceptor implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
Object target = invocation.getTarget(); //被代理對象
Method method = invocation.getMethod(); //代理方法
Object[] args = invocation.getArgs(); //方法參數
// do something ...... 方法攔截前執行代碼塊
Object result = invocation.proceed();
// do something .......方法攔截後執行代碼塊
return result;
}
public Object plugin(final Object target) {
return Proxy.newProxyInstance(Interceptor.class.getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return intercept(new Invocation(target, method, args));
}
});
}
public void setProperties(Properties properties) {
}
}
當然,Mybatis插件的那這個時候Intercepts的註解起不到作用了。
作者:曹金桂
鏈接:http://www.jianshu.com/p/7c7b8c2c985d
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請註明出處。
『貳』 JAVA使用mybatis執行sql腳本,怎麼獲取sql腳本的查詢結果
<selectid="DAO介面方法名稱"parameterType="參數類型"resultType="返回結果類型">
select*from表where。。。
</select>
resultType 可以是任意Object對象,如果多條數據,這這個方法返回的是List<Object?>,
如果確認是單條數據,可以直接 Object? ***(**); 。
沒有封裝成對象時,默認返回的是List<Map<欄位名稱String,列值Object>>這樣的數據。
Dao介面:
List<Map<String,Object>>list(Integerid);
SQL:
<selectid="list"parameterType="Integer"resultType="Map">
select*fromaaa
<where>
<iftest="null!=id">
id>#{id}
</if>
</where>
</select>
以上示例中表示查詢id>某個數值的所有結果,返回類型為MAP
執行腳本後沒有返回結果的吧,看ScriptRunner源碼,沒有提供任何返回結果的。
privatevoidexecuteStatement(Stringcommand)throwsSQLException,UnsupportedEncodingException{
booleanhasResults=false;
Statementstatement=connection.createStatement();
statement.setEscapeProcessing(escapeProcessing);
Stringsql=command;
if(removeCRs)
sql=sql.replaceAll(" "," ");
if(stopOnError){
hasResults=statement.execute(sql);
}else{
try{
hasResults=statement.execute(sql);
}catch(SQLExceptione){
Stringmessage="Errorexecuting:"+command+".Cause:"+e;
printlnError(message);
}
}
printResults(statement,hasResults);
try{
statement.close();
}catch(Exceptione){
//
}
}
...
有結果時,最後調用了這個方法列印出來而已。
privatevoidprint(Objecto){
if(logWriter!=null){
logWriter.print(o);
logWriter.flush();
}
}
你可以調用
publicvoidsetLogWriter(PrintWriterlogWriter){
this.logWriter=logWriter;
}
傳入你自己的Writer。
『叄』 如何用java的mybatis執行一連串sql語句
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;importjava.sql.Statement;publicclassxxxx{publicstaticvoidmain(String[]args){Connectioncon=null;Statementstmt=null;tr
『肆』 mybatis運行原理
你好,很高興回答你的問題。
mybatis其實就是針對jdbc做了封裝。可以根據設置的欄位映射,以及配置或者腳本等拼接sql語句並執行。獲取到結果集後再按照欄位映射進行封裝成返回值。
如果有幫助到你,請點擊採納。
『伍』 mybatis 怎麼寫sql語句
mybatis的sql和你在資料庫客戶端執行的sql是一樣的,但是在mybatis中調用的sql一般都是動態的,所以用到了參數傳遞。這個mybatis有對應的標簽以及相應的變數來實現。你可以搜索下mybatis標簽。同時給你一個參考的你看看,這個是一個查詢用戶的
<select id="queryUsers" parameterType="map" resultType="xx.xx.xx.bean.UserBean">
<![CDATA[
select
ID,
LOGIN_NAME AS loginName,
PASSWORD,
REAL_NAME AS realName,
POSITION,
(SELECT D.POSITION_NAME FROM UNIT_POSITION D WHERE D.POSITION_CODE=T.POSITION) POSITIONNAME,
USER_TYPE AS userType,
SEX,
PID,
TO_CHAR(T.BIRTHDAY,'YYYY-MM-DD') BIRTHDAY,
EMAIL,
CONTACT_TEL AS contactTel,
CONTACT_MOBILE AS contactMobile,
CONTACT_FAX AS contactFax,
CONTACT_ZIP AS contactZip,
CONTACT_ADDR AS contactAddr,
STATUS,
EDUCATION,
(SELECT D.EDUCATION_NAME FROM UNIT_EDUCATION D WHERE D.EDUCATION_CODE=T.EDUCATION AND D.STATUS=0) EDUCATIONNAME,
NATION,
POLITICAL,
REMARK,
TO_CHAR(T.CREATE_DATE,'YYYY-MM-DD HH24:MI:SS') createDate,
(SELECT D.REAL_NAME FROM UNIT_USER D WHERE D.ID= T.CREATE_USER_ID) createUserId,
TO_CHAR(T.UPDATE_DATE,'YYYY-MM-DD HH24:MI:SS') updateDate,
(SELECT D.REAL_NAME FROM UNIT_USER D WHERE D.ID= T.UPDATE_USER_ID) updateUserId
from UNIT_USER T
]]>
<where>
T.STATUS='1'
<if test="realName !=null and realName !=''">
and T.REAL_NAME like '%${realName}%'
</if>
<if test="nexusDpartment !=null">
AND T.ID IN (SELECT DISTINCT D.USER_ID FROM UNIT_USER_DEPT D WHERE D.DEPT_CODE IN (${nexusDpartment}))
</if>
<if test="deptCode !=null and deptCode !=''">
AND T.ID IN (SELECT DISTINCT D.USER_ID FROM UNIT_USER_DEPT D WHERE D.DEPT_CODE = #{deptCode})
</if>
</where>
<if test="sort != null and sort != ''">
order by ${sort}
<if test="direction != null and direction != ''">
${direction}
</if>
</if>
</select>
『陸』 如何通過url訪問執行sql (MyBatis)
在項目中,使用的是mybatis3.0.5,但沒有採用其提供的DAO層介面映射的策略,而且在進行多種屬性聯合查找時,需要底層提供通用的解決方案,所以需要mybatis直接執行sql語句,各個Impl均可調用,減少了在每個mybatis文件中配置符合當前對象的select查詢。。
(在mybatis中,需要通過傳遞對象,在select中判斷對象屬性是否為空進行where語句的拼湊,對後期的維護工作帶來不小的考驗,所以採用直接執行sql策略)
『柒』 java使用mybatis執行sql腳本,怎麼獲取sql腳本的結果
<select id="DAO介面方法名稱" parameterType="參數類型" resultType="返回結果類型">
select * from 表 where 。。。
</select>
resultType 可以是任意Object對象,如果多條數據,這這個方法返回的是List<Object?>,
如果確認是單條數據,可以直接 Object? ***(**); 。
沒有封裝成對象時,默認返回的是List<Map<欄位名稱String,列值Object>>這樣的數據。
Dao介面:
List<Map<String,Object>> list(Integer id);
SQL:
<select id="list" parameterType="Integer" resultType="Map">
select * from aaa
<where>
<if test="null!=id">
id >#{id}
『捌』 mybatis 怎麼執行sql語句
只要在一個sqlSession里就可以執行多個SQL語句,不知道你具體想要什麼效果?
『玖』 mybatis怎麼執行sql語句值
例如在一個 XXMapper.xml 中:
?
1
2
3
<select id="executeSql" resultType="map">
${_parameter}
</select>
你可以如下調用:
?
1
sqlSession.selectList("executeSql", "select * from sysuser where enabled = 1");
或者你可以在 XXMapper.java 介面中定義如下方法:
?
1
List<Map> executeSql(String sql);
然後使用介面調用方法:
?
1
xxMapper.executeSql("select * from sysuser where enabled = 1");
上面這些內容可能都會,下面在此基礎上再復雜一點。
假如像上面SQL中的enabled = 1我想使用參數方式傳值,也就是寫成 enabled = #{enabled},如果你沒有遇到過類似這種需求,可能不明白為什麼要這么寫,舉個例子,要實現一種動態查詢,可以在前台通過配置 SQL,提供一些查詢條件就能實現一個查詢的功能(為了安全,這些配置肯定是開發或者實施做的,不可能讓用戶直接操作資料庫)。
針對這個功能,使用 MyBatis 實現起來相當容易。配置 SQL 肯定要執行,用上面講的這種方式肯定可以執行 SQL,如何提供參數呢?參數就是enabled = #{enabled}中的#{enabled}部分。如果再多一些條件,一個配置好的 SQL 如下:
?
1
2
3
select * from sysuser
where enabled = #{enabled}
and userName like concat('%',#{userName},'%')
這種情況下,該怎麼用 MyBatis 實現呢?
首先 XML 中修改如下:
?
1
2
3
<select id="executeSql" resultType="map">
${sql}
</select>
介面中的方法修改為:
?
1
List<Map> executeSql(Map map);
然後調用方法:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
Map map = new HashMap();
//這里的 sql 對應 XML 中的 ${sql}
map.put("sql", "select * from sysuser "
+ " where enabled = #{enabled} "
+ " and userName like concat('%',#{userName},'%')");
//#{enabled}
map.put("enabled", 1);
//#{userName}
map.put("userName", "admin");
//介面方式調用
List<Map> list = xxMapper.executeSql(map);
//sqlSession方式調用