日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MyBatis-进阶2

發布時間:2025/3/8 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MyBatis-进阶2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

typeHandler

typeHandler有什么用?

你可以重寫類型處理器或創建你自己的類型處理器來處理不支持的或非標準的類型。 具體做法為:實現 org.apache.ibatis.type.TypeHandler 接口, 或繼承一個很便利的類 org.apache.ibatis.type.BaseTypeHandler, 然后可以選擇性地將它映射到一個 JDBC 類型

用法

參考官網的示例:

package com.xh.mybatisLearn.utils;import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedJdbcTypes; import org.apache.ibatis.type.MappedTypes;import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;/*** Created by root on 3/2/18.*/ @MappedJdbcTypes(JdbcType.VARCHAR) @MappedTypes(String.class) public class ExampleTypeHandler extends BaseTypeHandler<String> {public void setNonNullParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType) throws SQLException {preparedStatement.setString(i, "in>>" + s);}public String getNullableResult(ResultSet resultSet, String s) throws SQLException {return resultSet.getString(s);}public String getNullableResult(ResultSet resultSet, int i) throws SQLException {return resultSet.getString(i);}public String getNullableResult(CallableStatement callableStatement, int i) throws SQLException {return callableStatement.getString(i);} }

注冊TypeHandler

<typeHandlers><typeHandler handler="com.xh.mybatisLearn.utils.ExampleTypeHandler"/></typeHandlers>

插入時指定TypeHandler

<insert id="addUser" useGeneratedKeys="true"keyProperty="id">insert into user_tb (username,age) VALUES (#{username,typeHandler=com.xh.mybatisLearn.utils.ExampleTypeHandler},#{age})</insert>

結果:

getAll>>User{id=17, username='in>>u1', age=21}

plugin

plugin有什么用?

MyBatis 允許你在已映射語句執行過程中的某一點進行攔截調用。默認情況下,MyBatis 允許使用插件來攔截的方法調用包括:
Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
ParameterHandler (getParameterObject, setParameters)
ResultSetHandler (handleResultSets, handleOutputParameters)
StatementHandler (prepare, parameterize, batch, update, query)

用法

通過 MyBatis 提供的強大機制,使用插件是非常簡單的,只需實現 Interceptor 接口,并指定了想要攔截的方法簽名即可。
參考官網示例:

package com.xh.mybatisLearn.utils;import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.plugin.*; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.slf4j.Logger; import org.slf4j.LoggerFactory;import java.util.Properties;/*** Created by root on 3/2/18.*/ @Intercepts({@Signature(type = Executor.class,method = "query",args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})}) public class ExamplePlugin implements Interceptor {private final Logger logger = LoggerFactory.getLogger(ExamplePlugin.class);public Object intercept(Invocation invocation) throws Throwable {MappedStatement ms = (MappedStatement) invocation.getArgs()[0];BoundSql boundSql = ms.getBoundSql(invocation.getArgs()[1]);logger.info("==================== sql:{}", boundSql.getSql());return invocation.proceed();}public Object plugin(Object target) {return Plugin.wrap(target, this);}public void setProperties(Properties properties) {String some_string = properties.getProperty("some_string");logger.info("==================== some_string:{}", some_string);} }

這個插件就是打印查詢的sql,
其中type對應:
Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
ParameterHandler (getParameterObject, setParameters)
ResultSetHandler (handleResultSets, handleOutputParameters)
StatementHandler (prepare, parameterize, batch, update, query)
method對應:上面各項括號的方法
args對應:method的參數

plugin注冊

<plugins><plugin interceptor="com.xh.mybatisLearn.utils.ExamplePlugin"><property name="some_string" value="some_string_xxxxxx"/></plugin></plugins>

logger

<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>1.7.21</version></dependency>

輸出

[main] INFO com.xh.mybatisLearn.utils.ExamplePlugin - ==================== some_string:some_string_xxxxxx [main] INFO com.xh.mybatisLearn.utils.ExamplePlugin - ==================== sql:select * from user_tb where id=? getOne>>User{id=12, username='u1', age=21}

轉載于:https://www.cnblogs.com/lanqie/p/8493704.html

總結

以上是生活随笔為你收集整理的MyBatis-进阶2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。