唐伯虎 发表于 2021-8-17 11:58:05

MyBatis自定义typeHandler的完整实例

自定义typehandler
对于自定义typehandler来说,需要在配置文件中注册typehandlers 然后需要实现typehandler接口,
一个例子
首先编写调度的处理类


package com.ming.mybatis;


import org.apache.ibatis.type.jdbctype;
import org.apache.ibatis.type.typehandler;
import org.apache.logging.log4j.logmanager;
import org.apache.logging.log4j.logger;

import java.sql.callablestatement;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;

public class stringtypehandler implements typehandler<string> {
private static logger logger = logmanager.getlogger();

/**
* 对预编译的sql语句进行设置的时候,
* @param ps
* @param i
* @param parameter
* @param jdbctype
* @throws sqlexception
*/
@override
public void setparameter(preparedstatement ps, int i, string parameter, jdbctype jdbctype) throws sqlexception {
logger.info("使用typehandler");
// 设置预编译的sql对象,的string
ps.setstring(i, parameter);
}

/**
* 获得结果的时候,获取到列名的时候,调度此方法
* @param rs
* @param columnname
* @return
* @throws sqlexception
*/
@override
public string getresult(resultset rs, string columnname) throws sqlexception {
logger.info("使用自定义类型,结果获取列名字符串");
return rs.getstring(columnname);
}

/**
* 获取结果集中的index
* @param rs
* @param columnindex
* @return
* @throws sqlexception
*/
@override
public string getresult(resultset rs, int columnindex) throws sqlexception {
logger.info("在结果集中获取columnindex");
return rs.getstring(columnindex);
}

/**
* 获取结果集中的下标对应的字符串
* @param cs
* @param columnindex
* @return
* @throws sqlexception
*/
@override
public string getresult(callablestatement cs, int columnindex) throws sqlexception {
logger.info("结果集中,下标获取到字符串");
return cs.getstring(columnindex);
}
}
再次编写配置文件


<typehandlers>
<typehandler jdbctype="varchar" javatype="string" handler="com.ming.mybatis.stringtypehandler">
</typehandlers>
传入数据库的为的varchar类型,javatype类型为string类型,调度的类为com.ming.mybatis.stringtypehandler
修改映射文件,说明那些需要对结果进行修改


<?xml version="1.0" encoding="utf-8" ?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 定义接口类 -->
<mapper namespace="com.ming.mybatis.pojo.rolemapper">
<!-- id表示标签标识 type标识结果的返回类型,其中role为在配置文件中使用的别名 role类 -->
<resultmap type="role" id="rolemap">
<!-- id为主键映射关系 其中数据库中的id为主键 -->
<id column="id" property="id" javatype="int" jdbctype="integer"/>
<!-- result为其他基本数据类型和实体类之间的映射 映射关系为role_name 到 rolename之间的映射 数据类型为string到varchar之间的映射关系 -->
<result column="role_name" property="rolename" javatype="string" jdbctype="varchar"/>
<!-- 这里使用typehandler表示遇到此处理集的时候,将会执行com.ming.mybatis.stringtypehandler -->
<result column="note" property="note" typehandler="com.ming.mybatis.stringtypehandler"/>
</resultmap>

<!-- 书写sql语句 定义使用resultmap为rolemap-->
<select id="getrole" parametertype="int" resultmap="rolemap">
select id, role_name, note from t_role4 where id = #{id}
</select>

</mapper>
其中传入的参数为int类型,由于结果中mybatis没有int类型,对应的是integer类型,在mysql中两者类型相同,所以修改如上所示,表类型

书写index.jsp


<%@ page import="org.apache.ibatis.session.sqlsession" %>
<%@ page import="com.ming.util.sqlsessionfactoryutil" %>
<%@ page import="com.ming.mybatis.pojo.rolemapper" %>
<%@ page import="com.ming.mybatis.pojo.role" %>
<html>
<body>
<h2>hello world!</h2>

<%
long starttime = system.currenttimemillis(); //获取开始时间
sqlsession sqlsession = null;
role role = null;
for(int i = 0; i < 10; i++) {
    try {
      sqlsession = sqlsessionfactoryutil.opensqlsesion();
      rolemapper rolemapper = sqlsession.getmapper(rolemapper.class);
      role = rolemapper.getrole(100);
      sqlsession.commit();
    } catch (exception e) {
      e.printstacktrace();
      sqlsession.rollback();
    } finally {
      if (sqlsession != null) {
      sqlsession.close();
      }
    }
}
long endtime = system.currenttimemillis(); //获取结束时间

%>
<%=role.getnote()%>
</body>
</html>
已经查询成功

查看日志文件,日志文件已经输出相关信息


2019-04-13 05:03:33.181 org.apache.ibatis.transaction.jdbc.jdbctransaction.openconnection(jdbctransaction.java:136) - opening jdbc connection
2019-04-13 05:03:33.181 org.apache.ibatis.datasource.pooled.pooleddatasource.popconnection(pooleddatasource.java:397) - checked out connection 197673257 from pool.
2019-04-13 05:03:33.182 org.apache.ibatis.transaction.jdbc.jdbctransaction.setdesiredautocommit(jdbctransaction.java:100) - setting autocommit to false on jdbc connection
2019-04-13 05:03:33.235 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> preparing: select id, role_name, note from t_role4 where id = ?
2019-04-13 05:03:33.236 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> parameters: 100(integer)
2019-04-13 05:03:33.275 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.276 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.277 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - <==   total: 1
2019-04-13 05:03:33.277 org.apache.ibatis.transaction.jdbc.jdbctransaction.resetautocommit(jdbctransaction.java:122) - resetting autocommit to true on jdbc connection
2019-04-13 05:03:33.312 org.apache.ibatis.transaction.jdbc.jdbctransaction.close(jdbctransaction.java:90) - closing jdbc connection
2019-04-13 05:03:33.312 org.apache.ibatis.datasource.pooled.pooleddatasource.pushconnection(pooleddatasource.java:362) - returned connection 197673257 to pool.
2019-04-13 05:03:33.313 org.apache.ibatis.transaction.jdbc.jdbctransaction.openconnection(jdbctransaction.java:136) - opening jdbc connection
2019-04-13 05:03:33.314 org.apache.ibatis.datasource.pooled.pooleddatasource.popconnection(pooleddatasource.java:397) - checked out connection 197673257 from pool.
2019-04-13 05:03:33.314 org.apache.ibatis.transaction.jdbc.jdbctransaction.setdesiredautocommit(jdbctransaction.java:100) - setting autocommit to false on jdbc connection
2019-04-13 05:03:33.346 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> preparing: select id, role_name, note from t_role4 where id = ?
2019-04-13 05:03:33.348 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> parameters: 100(integer)
2019-04-13 05:03:33.386 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.390 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.391 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - <==   total: 1
2019-04-13 05:03:33.395 org.apache.ibatis.transaction.jdbc.jdbctransaction.resetautocommit(jdbctransaction.java:122) - resetting autocommit to true on jdbc connection
2019-04-13 05:03:33.442 org.apache.ibatis.transaction.jdbc.jdbctransaction.close(jdbctransaction.java:90) - closing jdbc connection
2019-04-13 05:03:33.443 org.apache.ibatis.datasource.pooled.pooleddatasource.pushconnection(pooleddatasource.java:362) - returned connection 197673257 to pool.
2019-04-13 05:03:33.443 org.apache.ibatis.transaction.jdbc.jdbctransaction.openconnection(jdbctransaction.java:136) - opening jdbc connection
2019-04-13 05:03:33.444 org.apache.ibatis.datasource.pooled.pooleddatasource.popconnection(pooleddatasource.java:397) - checked out connection 197673257 from pool.
2019-04-13 05:03:33.444 org.apache.ibatis.transaction.jdbc.jdbctransaction.setdesiredautocommit(jdbctransaction.java:100) - setting autocommit to false on jdbc connection
2019-04-13 05:03:33.479 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> preparing: select id, role_name, note from t_role4 where id = ?
2019-04-13 05:03:33.480 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> parameters: 100(integer)
2019-04-13 05:03:33.518 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.519 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.520 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - <==   total: 1
2019-04-13 05:03:33.520 org.apache.ibatis.transaction.jdbc.jdbctransaction.resetautocommit(jdbctransaction.java:122) - resetting autocommit to true on jdbc connection
2019-04-13 05:03:33.556 org.apache.ibatis.transaction.jdbc.jdbctransaction.close(jdbctransaction.java:90) - closing jdbc connection
2019-04-13 05:03:33.557 org.apache.ibatis.datasource.pooled.pooleddatasource.pushconnection(pooleddatasource.java:362) - returned connection 197673257 to pool.
2019-04-13 05:03:33.558 org.apache.ibatis.transaction.jdbc.jdbctransaction.openconnection(jdbctransaction.java:136) - opening jdbc connection
2019-04-13 05:03:33.558 org.apache.ibatis.datasource.pooled.pooleddatasource.popconnection(pooleddatasource.java:397) - checked out connection 197673257 from pool.
2019-04-13 05:03:33.558 org.apache.ibatis.transaction.jdbc.jdbctransaction.setdesiredautocommit(jdbctransaction.java:100) - setting autocommit to false on jdbc connection
2019-04-13 05:03:33.591 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> preparing: select id, role_name, note from t_role4 where id = ?
2019-04-13 05:03:33.592 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> parameters: 100(integer)
2019-04-13 05:03:33.638 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.639 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.640 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - <==   total: 1
2019-04-13 05:03:33.640 org.apache.ibatis.transaction.jdbc.jdbctransaction.resetautocommit(jdbctransaction.java:122) - resetting autocommit to true on jdbc connection
2019-04-13 05:03:33.676 org.apache.ibatis.transaction.jdbc.jdbctransaction.close(jdbctransaction.java:90) - closing jdbc connection
2019-04-13 05:03:33.677 org.apache.ibatis.datasource.pooled.pooleddatasource.pushconnection(pooleddatasource.java:362) - returned connection 197673257 to pool.
2019-04-13 05:03:33.677 org.apache.ibatis.transaction.jdbc.jdbctransaction.openconnection(jdbctransaction.java:136) - opening jdbc connection
2019-04-13 05:03:33.677 org.apache.ibatis.datasource.pooled.pooleddatasource.popconnection(pooleddatasource.java:397) - checked out connection 197673257 from pool.
2019-04-13 05:03:33.677 org.apache.ibatis.transaction.jdbc.jdbctransaction.setdesiredautocommit(jdbctransaction.java:100) - setting autocommit to false on jdbc connection
2019-04-13 05:03:33.711 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> preparing: select id, role_name, note from t_role4 where id = ?
2019-04-13 05:03:33.712 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> parameters: 100(integer)
2019-04-13 05:03:33.747 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.748 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.748 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - <==   total: 1
2019-04-13 05:03:33.749 org.apache.ibatis.transaction.jdbc.jdbctransaction.resetautocommit(jdbctransaction.java:122) - resetting autocommit to true on jdbc connection
2019-04-13 05:03:33.780 org.apache.ibatis.transaction.jdbc.jdbctransaction.close(jdbctransaction.java:90) - closing jdbc connection
2019-04-13 05:03:33.780 org.apache.ibatis.datasource.pooled.pooleddatasource.pushconnection(pooleddatasource.java:362) - returned connection 197673257 to pool.
2019-04-13 05:03:33.781 org.apache.ibatis.transaction.jdbc.jdbctransaction.openconnection(jdbctransaction.java:136) - opening jdbc connection
2019-04-13 05:03:33.781 org.apache.ibatis.datasource.pooled.pooleddatasource.popconnection(pooleddatasource.java:397) - checked out connection 197673257 from pool.
2019-04-13 05:03:33.781 org.apache.ibatis.transaction.jdbc.jdbctransaction.setdesiredautocommit(jdbctransaction.java:100) - setting autocommit to false on jdbc connection
2019-04-13 05:03:33.830 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> preparing: select id, role_name, note from t_role4 where id = ?
2019-04-13 05:03:33.831 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> parameters: 100(integer)
2019-04-13 05:03:33.868 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.870 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.870 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - <==   total: 1
2019-04-13 05:03:33.872 org.apache.ibatis.transaction.jdbc.jdbctransaction.resetautocommit(jdbctransaction.java:122) - resetting autocommit to true on jdbc connection
2019-04-13 05:03:33.913 org.apache.ibatis.transaction.jdbc.jdbctransaction.close(jdbctransaction.java:90) - closing jdbc connection
2019-04-13 05:03:33.914 org.apache.ibatis.datasource.pooled.pooleddatasource.pushconnection(pooleddatasource.java:362) - returned connection 197673257 to pool.
2019-04-13 05:03:33.914 org.apache.ibatis.transaction.jdbc.jdbctransaction.openconnection(jdbctransaction.java:136) - opening jdbc connection
2019-04-13 05:03:33.915 org.apache.ibatis.datasource.pooled.pooleddatasource.popconnection(pooleddatasource.java:397) - checked out connection 197673257 from pool.
2019-04-13 05:03:33.915 org.apache.ibatis.transaction.jdbc.jdbctransaction.setdesiredautocommit(jdbctransaction.java:100) - setting autocommit to false on jdbc connection
2019-04-13 05:03:33.956 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> preparing: select id, role_name, note from t_role4 where id = ?
2019-04-13 05:03:33.957 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> parameters: 100(integer)
2019-04-13 05:03:33.994 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.994 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.995 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - <==   total: 1
2019-04-13 05:03:33.995 org.apache.ibatis.transaction.jdbc.jdbctransaction.resetautocommit(jdbctransaction.java:122) - resetting autocommit to true on jdbc connection
2019-04-13 05:03:34.029 org.apache.ibatis.transaction.jdbc.jdbctransaction.close(jdbctransaction.java:90) - closing jdbc connection
2019-04-13 05:03:34.029 org.apache.ibatis.datasource.pooled.pooleddatasource.pushconnection(pooleddatasource.java:362) - returned connection 197673257 to pool.
2019-04-13 05:03:34.030 org.apache.ibatis.transaction.jdbc.jdbctransaction.openconnection(jdbctransaction.java:136) - opening jdbc connection
2019-04-13 05:03:34.030 org.apache.ibatis.datasource.pooled.pooleddatasource.popconnection(pooleddatasource.java:397) - checked out connection 197673257 from pool.
2019-04-13 05:03:34.030 org.apache.ibatis.transaction.jdbc.jdbctransaction.setdesiredautocommit(jdbctransaction.java:100) - setting autocommit to false on jdbc connection
2019-04-13 05:03:34.095 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> preparing: select id, role_name, note from t_role4 where id = ?
2019-04-13 05:03:34.097 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> parameters: 100(integer)
2019-04-13 05:03:34.133 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:34.133 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:34.134 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - <==   total: 1
2019-04-13 05:03:34.134 org.apache.ibatis.transaction.jdbc.jdbctransaction.resetautocommit(jdbctransaction.java:122) - resetting autocommit to true on jdbc connection
2019-04-13 05:03:34.168 org.apache.ibatis.transaction.jdbc.jdbctransaction.close(jdbctransaction.java:90) - closing jdbc connection
2019-04-13 05:03:34.169 org.apache.ibatis.datasource.pooled.pooleddatasource.pushconnection(pooleddatasource.java:362) - returned connection 197673257 to pool.
2019-04-13 05:03:34.170 org.apache.ibatis.transaction.jdbc.jdbctransaction.openconnection(jdbctransaction.java:136) - opening jdbc connection
2019-04-13 05:03:34.170 org.apache.ibatis.datasource.pooled.pooleddatasource.popconnection(pooleddatasource.java:397) - checked out connection 197673257 from pool.
2019-04-13 05:03:34.171 org.apache.ibatis.transaction.jdbc.jdbctransaction.setdesiredautocommit(jdbctransaction.java:100) - setting autocommit to false on jdbc connection
2019-04-13 05:03:34.214 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> preparing: select id, role_name, note from t_role4 where id = ?
2019-04-13 05:03:34.215 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> parameters: 100(integer)
2019-04-13 05:03:34.252 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:34.252 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:34.253 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - <==   total: 1
2019-04-13 05:03:34.254 org.apache.ibatis.transaction.jdbc.jdbctransaction.resetautocommit(jdbctransaction.java:122) - resetting autocommit to true on jdbc connection
2019-04-13 05:03:34.291 org.apache.ibatis.transaction.jdbc.jdbctransaction.close(jdbctransaction.java:90) - closing jdbc connection
2019-04-13 05:03:34.291 org.apache.ibatis.datasource.pooled.pooleddatasource.pushconnection(pooleddatasource.java:362) - returned connection 197673257 to pool.
2019-04-13 05:03:34.292 org.apache.ibatis.transaction.jdbc.jdbctransaction.openconnection(jdbctransaction.java:136) - opening jdbc connection
2019-04-13 05:03:34.292 org.apache.ibatis.datasource.pooled.pooleddatasource.popconnection(pooleddatasource.java:397) - checked out connection 197673257 from pool.
2019-04-13 05:03:34.292 org.apache.ibatis.transaction.jdbc.jdbctransaction.setdesiredautocommit(jdbctransaction.java:100) - setting autocommit to false on jdbc connection
2019-04-13 05:03:34.336 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> preparing: select id, role_name, note from t_role4 where id = ?
2019-04-13 05:03:34.337 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - ==> parameters: 100(integer)
2019-04-13 05:03:34.379 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:34.380 com.ming.mybatis.stringtypehandler.getresult(stringtypehandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:34.380 org.apache.ibatis.logging.jdbc.basejdbclogger.debug(basejdbclogger.java:143) - <==   total: 1
2019-04-13 05:03:34.381 org.apache.ibatis.transaction.jdbc.jdbctransaction.resetautocommit(jdbctransaction.java:122) - resetting autocommit to true on jdbc connection
2019-04-13 05:03:34.415 org.apache.ibatis.transaction.jdbc.jdbctransaction.close(jdbctransaction.java:90) - closing jdbc connection
2019-04-13 05:03:34.415 org.apache.ibatis.datasource.pooled.pooleddatasource.pushconnection(pooleddatasource.java:362) - returned connection 197673257 to pool.
ps 类型转换的问题
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对CodeAE代码之家的支持。
原文链接:https://www.iming.info/mybatis-zi-ding-yi-typehandler/

文档来源:http://www.zzvips.com/article/180139.html
页: [1]
查看完整版本: MyBatis自定义typeHandler的完整实例