Mybatis自定义类型处理器

无论是 MyBatis 在预处理语句(PreparedStatement)中设置一个参数时,还是从结果集中取出一个值时, 都会用类型处理器将获取的值以合适的方式转换成 Java 类型。

你可以重写类型处理器或创建你自己的类型处理器来处理不支持的或非标准的类型。
具体做法为:实现 org.apache.ibatis.type.TypeHandler 接口, 或继承一个很便利的类 org.apache.ibatis.type.BaseTypeHandler,然后可以选择性地将它映射到一个JDBC类型。
例如需求:一个Java中的Date数据类型,我想将之存到数据库的时候存成一个1970年至今的毫秒数,取出来时转换成java的Date,即java的Date与数据库的bigint毫秒值之间转换。

开发步骤:
1. 定义转换类继承类BaseTypeHandler<T>
2. 覆盖4个未实现的方法,其中setNonNullParameter为java程序设置数据到数据库的回调方法,getNullableResult为查询时 mysql的字符串类型转换成 java的Type类型的方法
3. 在MyBatis核心配置文件中进行注册

自定义类型转换器

package com.code_g.handle;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class DateTypeHandler extends BaseTypeHandler<Date> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException {
        //java => sql
        long time = parameter.getTime();
        ps.setLong(i,time);
    }

    @Override
    public Date getNullableResult(ResultSet rs, String columnName) throws SQLException {
        //sql => java
        long aLong = rs.getLong(columnName);
        return new Date(aLong);
    }

    @Override
    public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        //sql => java
        long aLong = rs.getLong(columnIndex);
        return new Date(aLong);
    }

    @Override
    public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        //sql => java
        long aLong = cs.getLong(columnIndex);
        return new Date(aLong);
    }
}

RoleMapper.xml

<?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.code_g.dao.RoleMapper">
    <insert id="save" parameterType="Role">
        insert into role values (#{id},#{username},#{password},#{date})
    </insert>

    <select id="findById" parameterType="int" resultType="Role">
        select * from role where id = #{id}
    </select>
</mapper>

RoleMapper.java

package com.code_g.dao;

import com.code_g.domain.Role;

import java.util.List;


public interface RoleMapper {

    void save(Role role);

    Role findById(int id);

}

配置自定义类型转换器

  <typeHandlers>
        <typeHandler handler="com.code_g.handle.DateTypeHandler"/>
    </typeHandlers>

测试方法

 @Test
    //自定义类型处理器
    public void test8() throws IOException {
        InputStream resource = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resource);
        SqlSession sqlSession = sessionFactory.openSession();
        RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
        Role role = new Role();
        role.setUsername("zhangsan");
        role.setPassword("123");
        role.setDate(new Date());
        mapper.save(role);
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test9() throws IOException {
        InputStream resource = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resource);
        SqlSession sqlSession = build.openSession();
        RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
        Role role = mapper.findById(1);
        System.out.println(role);
        sqlSession.close();
    }
posted @ 2021-05-28 10:53  code-G  阅读(484)  评论(0编辑  收藏  举报