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}

最新文章

  1. hadoop_elk架构图
  2. Java 学习之路 之 泛型方法
  3. IOS URL 编码和解码
  4. 准备.Net转前端开发-WPF界面框架那些事,搭建基础框架
  5. 让IE下支持Html5的placeholder属性
  6. 方法Equals和操作符==的区别
  7. c语言字符串_续
  8. Shell中特殊符号
  9. 刷新的时候jquery获取checkbox是否为选中和设置选中
  10. Java向上转型注意事项
  11. Google搜索的配置方法
  12. Robot Framework作者建议如何选择自动化测试框架
  13. Bigcommerce:安装的出错解决方案
  14. es6--(三)set和map数据结构
  15. Nagios工作原理
  16. 多进程multiprocessing
  17. echars柱状图修改每条柱的颜色
  18. 如何判断ACCESS数据库有无密码
  19. java 注解默认值
  20. QGIS(2.18.15 源码)+Qt(5/5.9.3)+VS2015(X64)编译

热门文章

  1. (Dijkstra) POJ2387 Til the Cows Come Home
  2. Docker CE 各安装方法
  3. MyBatis-注解方式整合SSM
  4. C#设计模式(7)——适配器模式
  5. PHP7 学习笔记(十四)Reids 键空间通知配合TP5 实现分布式延时任务
  6. 转载 IEnumerable和IEnumerator 详解
  7. [Android] Android读取Asset下文件的最简单的方法总结(用于MediaPlayer中)
  8. 海南医院帆软报表 最终版本SQL
  9. getservbyname和getservbyport
  10. Coursera Deep Learning 3 Structuring Machine Learning Projects, ML Strategy