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 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:136) - Opening JDBC Connection
2019-04-13 05:03:33.181 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:397) - Checked out connection 197673257 from pool.
2019-04-13 05:03:33.182 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:100) - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.235 [DEBUG] 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 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - ==> Parameters: 100(Integer)
2019-04-13 05:03:33.275 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.276 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.277 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - <==   Total: 1
2019-04-13 05:03:33.277 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.resetAutoCommit(JdbcTransaction.java:122) - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.312 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.close(JdbcTransaction.java:90) - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.312 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.pushConnection(PooledDataSource.java:362) - Returned connection 197673257 to pool.
2019-04-13 05:03:33.313 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:136) - Opening JDBC Connection
2019-04-13 05:03:33.314 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:397) - Checked out connection 197673257 from pool.
2019-04-13 05:03:33.314 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:100) - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.346 [DEBUG] 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 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - ==> Parameters: 100(Integer)
2019-04-13 05:03:33.386 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.390 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.391 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - <==   Total: 1
2019-04-13 05:03:33.395 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.resetAutoCommit(JdbcTransaction.java:122) - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.442 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.close(JdbcTransaction.java:90) - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.443 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.pushConnection(PooledDataSource.java:362) - Returned connection 197673257 to pool.
2019-04-13 05:03:33.443 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:136) - Opening JDBC Connection
2019-04-13 05:03:33.444 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:397) - Checked out connection 197673257 from pool.
2019-04-13 05:03:33.444 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:100) - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.479 [DEBUG] 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 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - ==> Parameters: 100(Integer)
2019-04-13 05:03:33.518 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.519 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.520 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - <==   Total: 1
2019-04-13 05:03:33.520 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.resetAutoCommit(JdbcTransaction.java:122) - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.556 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.close(JdbcTransaction.java:90) - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.557 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.pushConnection(PooledDataSource.java:362) - Returned connection 197673257 to pool.
2019-04-13 05:03:33.558 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:136) - Opening JDBC Connection
2019-04-13 05:03:33.558 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:397) - Checked out connection 197673257 from pool.
2019-04-13 05:03:33.558 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:100) - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.591 [DEBUG] 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 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - ==> Parameters: 100(Integer)
2019-04-13 05:03:33.638 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.639 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.640 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - <==   Total: 1
2019-04-13 05:03:33.640 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.resetAutoCommit(JdbcTransaction.java:122) - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.676 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.close(JdbcTransaction.java:90) - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.677 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.pushConnection(PooledDataSource.java:362) - Returned connection 197673257 to pool.
2019-04-13 05:03:33.677 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:136) - Opening JDBC Connection
2019-04-13 05:03:33.677 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:397) - Checked out connection 197673257 from pool.
2019-04-13 05:03:33.677 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:100) - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.711 [DEBUG] 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 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - ==> Parameters: 100(Integer)
2019-04-13 05:03:33.747 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.748 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.748 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - <==   Total: 1
2019-04-13 05:03:33.749 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.resetAutoCommit(JdbcTransaction.java:122) - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.780 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.close(JdbcTransaction.java:90) - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.780 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.pushConnection(PooledDataSource.java:362) - Returned connection 197673257 to pool.
2019-04-13 05:03:33.781 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:136) - Opening JDBC Connection
2019-04-13 05:03:33.781 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:397) - Checked out connection 197673257 from pool.
2019-04-13 05:03:33.781 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:100) - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.830 [DEBUG] 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 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - ==> Parameters: 100(Integer)
2019-04-13 05:03:33.868 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.870 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.870 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - <==   Total: 1
2019-04-13 05:03:33.872 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.resetAutoCommit(JdbcTransaction.java:122) - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.913 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.close(JdbcTransaction.java:90) - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.914 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.pushConnection(PooledDataSource.java:362) - Returned connection 197673257 to pool.
2019-04-13 05:03:33.914 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:136) - Opening JDBC Connection
2019-04-13 05:03:33.915 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:397) - Checked out connection 197673257 from pool.
2019-04-13 05:03:33.915 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:100) - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:33.956 [DEBUG] 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 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - ==> Parameters: 100(Integer)
2019-04-13 05:03:33.994 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.994 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:33.995 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - <==   Total: 1
2019-04-13 05:03:33.995 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.resetAutoCommit(JdbcTransaction.java:122) - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:34.029 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.close(JdbcTransaction.java:90) - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:34.029 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.pushConnection(PooledDataSource.java:362) - Returned connection 197673257 to pool.
2019-04-13 05:03:34.030 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:136) - Opening JDBC Connection
2019-04-13 05:03:34.030 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:397) - Checked out connection 197673257 from pool.
2019-04-13 05:03:34.030 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:100) - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:34.095 [DEBUG] 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 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - ==> Parameters: 100(Integer)
2019-04-13 05:03:34.133 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:34.133 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:34.134 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - <==   Total: 1
2019-04-13 05:03:34.134 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.resetAutoCommit(JdbcTransaction.java:122) - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:34.168 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.close(JdbcTransaction.java:90) - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:34.169 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.pushConnection(PooledDataSource.java:362) - Returned connection 197673257 to pool.
2019-04-13 05:03:34.170 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:136) - Opening JDBC Connection
2019-04-13 05:03:34.170 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:397) - Checked out connection 197673257 from pool.
2019-04-13 05:03:34.171 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:100) - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:34.214 [DEBUG] 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 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - ==> Parameters: 100(Integer)
2019-04-13 05:03:34.252 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:34.252 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:34.253 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - <==   Total: 1
2019-04-13 05:03:34.254 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.resetAutoCommit(JdbcTransaction.java:122) - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:34.291 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.close(JdbcTransaction.java:90) - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:34.291 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.pushConnection(PooledDataSource.java:362) - Returned connection 197673257 to pool.
2019-04-13 05:03:34.292 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:136) - Opening JDBC Connection
2019-04-13 05:03:34.292 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:397) - Checked out connection 197673257 from pool.
2019-04-13 05:03:34.292 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:100) - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:34.336 [DEBUG] 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 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - ==> Parameters: 100(Integer)
2019-04-13 05:03:34.379 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:34.380 [INFO ] com.ming.MyBatis.StringTypeHandler.getResult(StringTypeHandler.java:48) - 使用自定义类型,结果获取列名字符串
2019-04-13 05:03:34.380 [DEBUG] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143) - <==   Total: 1
2019-04-13 05:03:34.381 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.resetAutoCommit(JdbcTransaction.java:122) - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:34.415 [DEBUG] org.apache.ibatis.transaction.jdbc.JdbcTransaction.close(JdbcTransaction.java:90) - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@bc84129]
2019-04-13 05:03:34.415 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.pushConnection(PooledDataSource.java:362) - Returned connection 197673257 to pool.

ps 类型转换的问题

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。

(0)

相关推荐

  • Mybatis中自定义TypeHandler处理枚举详解

    在Mybatis中,处理枚举类的TypeHandler有两个: EnumTypeHandler: 用于保存枚举名 EnumOrdinalTypeHandler: 用于保存枚举的序号. 在实际项目中,以上往往不能满足我们的需求. 需求分析 枚举需要包含两个属性,label(用于显示), value(实际的枚举值).数据库保存枚举值(value). 这很明显Mybatis提供的两个枚举TypeHandler不能满足我们的需求.此时,我们可以自定义一个通用的枚举TypeHandler来满足我们的需求.

  • Mybatis实现自定义类型转换器TypeHandler的方法

    先给大家简单介绍下mybatis MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录. 此文其实是java操作Oracle类型XMLType总结二:使用Mybatis附带的一篇小结. Mybatis实现自定义的转换器,

  • Mybatis实现自定义的typehandler三步曲

    第一步实现接口TypeHandler<T> @MappedJdbcTypes(JdbcType.TIMESTAMP)//此处如果不使用该注解,在myabtis-config.xml中注册该typehandler的时候需要写明jdbctype="TIMESTAMP" @MappedTypes(DateTime.class)//此处如果不使用该注解,在myabtis-config.xml中注册该typehandler的时候需要写明javatype="com.sinos

  • MyBatis使用自定义TypeHandler转换类型的实现方法

    MyBatis虽然有很好的SQL执行性能,但毕竟不是完整的ORM框架,不同的数据库之间SQL执行还是有差异. 笔者最近在升级 Oracle 驱动至 ojdbc 7 ,就发现了处理DATE类型存在问题.还好MyBatis提供了使用自定义TypeHandler转换类型的功能. 本文介绍如下使用 TypeHandler 实现日期类型的转换. 问题背景 项目中有如下的字段,是采用的DATE类型: birthday = #{birthday, jdbcType=DATE}, 在更新 Oracle 驱动之前

  • 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; imp

  • Mybatis自定义TypeHandler解决特殊类型转换问题详解

    我们知道,Java和MySQL中的数据类型是不同的,Java中除了基本数据类型,还有对象. 有时候使用MySQL存储数据,或者从MySQL中读取数据时,会有一些特殊需求 weary ,比如: 将Integer数组直接存入MySQL,保存为BLOB形式,读取出来时又是正常的Integer数组 将Integer数组转换为String,然后存入MySQL,使用varchar类型,读取出来时又是正常的Integer数组 这也太难了叭! 解决办法有两种: Basic Method:Java在存入数据之前,

  • php自定义分页类完整实例

    本文实例讲述了php自定义分页类.分享给大家供大家参考,具体如下: <?php header("Content-type:text/html;Charset=utf-8"); class SubPages{ private $each_disNums;//每页显示的条目数 private $nums;//总条目数 private $current_page;//当前被选中的页 private $sub_pages;//每次显示的页数 private $pageNums;//总页数

  • Spring集成MyBatis完整实例(分享)

    为了梳理前面学习的<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>中的内容,准备做一个完整的示例完成一个简单的图书管理功能,主要使用到的技术包含Spring.MyBatis.Maven与MySQL等.最后的运行效果如下: 项目结构如下: 一.新建一个基于Maven的Web项目 1.1.创建一个简单的Maven项目,项目信息如下: 1.2.修改层面信息,在项目上右键选择属性,再选择"Project

  • mybatis插件实现自定义改写表名实例代码

    代码如下: @Intercepts({@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), @Signature(type = Executor.class, method = "update", args = {MappedStatement.cl

  • MyBatis Properties及别名定义实例详解

    上一篇我们介绍了mybatis的增删改查入门实例,我们发现在 mybatis-configuration.xml 的配置文件中,对数据库的配置都是硬编码在这个xml文件中,如下图,那么我们如何改进这个写法呢? 1.我们将 数据库的配置语句写在 db.properties 文件中 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm jdbc.username=root jdbc.password=ro

  • C#自定义HttpFilter模块完善实例

    本文实例讲述了C#自定义HttpFilter模块完善的方法,分享给大家供大家参考.具体实现方法如下: 一.背景 近期由于要针对项目做用户操作日志,但不想在每个方法里去增加代码,写入用户日志.因为这样具体的方法违背职责单一的原则,若后期日志内容格式发生变更,或其他什么需求,该方法代码主要一变在变,故使用HttpModule模块来完成此功能,感兴趣的朋友可以参考:关于HttpHandler与HttpModule的理解和应用方法 经过实际运用与完善,现在可以再次总结下. 二.拦截时机 现在的版本中,拦

  • MyBatis自定义类型转换器实现加解密

    需求场景:当数据库中保存的部分数据需要加密,页面需要正常显示时.这是就需要我们自定义类型转换器,在Mybatis执行SQL得到结果时,通过自定义类型转换器将CHAR或者VARCHAR2进行加解密处理,Java代码如下: /**自定义typeHandler<br/> * 1 插入数据库, 加密 * 2 查询,解密 * @author Administrator * */ public class CryptTypeHandler implements TypeHandler<CryptTy

  • Spring MVC实现mysql数据库增删改查完整实例

    最近刚学了springmvc框架,感觉确实方便了不少,减少了大量的冗余代码.就自己做了个小项目练练手,这是个初级的springmvc应用的项目,没有用到mybatis,项目功能还算完善,实现了基本的增删改查的功能. 项目环境: -系统:win10 -开发环境:eclipseOxygenReleaseCandidate3(4.7) -jdk版本:java1.8(121) -mysql:5.7 -spring:4.0 -tomcat:8.5 用到的技术: springmvcspringjspjdbc

随机推荐