Mybatis——typeHandlers标签自定义类型处理器

利用SpringBoot整合Mybatis并使用yaml格式文件注册类型处理器,完成java.util.Date与数据库中bigint数据类型的转换(就是在处理器中将Date类转换为long类型)

User实体类

public class User {
    private int id;
    private String name;
    private String password;
    private Date birthday;
    ......//补齐get和set函数    
}

DateTypeHandler类型处理器

public class DateTypeHandler extends BaseTypeHandler<Date> {
    /**
     * 将java类型转换成数据库需要的类型
     * @param preparedStatement
     * @param i
     * @param date
     * @param jdbcType
     * @throws SQLException
     */
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException {
        long time = date.getTime();
        preparedStatement.setLong(i,time);
    }

    /**
     * 将数据库中类型转换成java类型
     * @param resultSet 查询出的结果集
     * @param s 要转换的字段名称
     * @return
     * @throws SQLException
     */
    @Override
    public Date getNullableResult(ResultSet resultSet, String s) throws SQLException {
        //获得结果集中所需要的数据(long)转换成Date类型 返回
        long aLong = resultSet.getLong(s);
        Date date = new Date(aLong);
        return date;
    }

    /**
     * 将数据库中类型转换成java类型
     * @param resultSet
     * @param i 字段位置
     * @return
     * @throws SQLException
     */
    @Override
    public Date getNullableResult(ResultSet resultSet, int i) throws SQLException {
        //获得结果集中所需要的数据(long)转换成Date类型 返回
        long aLong = resultSet.getLong(i);
        Date date = new Date(aLong);
        return date;
    }

    /**
     *
     *
     * @param callableStatement
     * @param i
     * @return
     * @throws SQLException
     */
    @Override
    public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        long aLong = callableStatement.getLong(i);
        Date date = new Date(aLong);
        return date;
    }
}

application.yaml配置文件

写入类型处理器所在的包引用地址放入

# datasource
spring:
  datasource:
    url: jdbc:mysql:///user
    driver: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
# mybatis
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml # mapper 映射文件路径i
  type-aliases-package: com/example/springbootmybatis/domain
  # 注册类型处理器
  type-handlers-package: com/example/springbootmybatis/handler

UserMapper

与普通的mapper编写无异

<?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.example.springbootmybatis.mapper.UserXmlMapper">
    <insert id="insert_b" parameterType="com.example.springbootmybatis.domain.User">
        insert into user values(#{id}, #{name}, #{password}, #{birthday})
    </insert>

    <select id="findById_b" parameterType="int" resultType="com.example.springbootmybatis.domain.User">
        select * from user where id=#{id}
    </select>
</mapper>

UserXmlMapper代理开发方法实现接口

@Mapper
@Repository
public interface UserXmlMapper {
    public void insert_b(User user);
    public User findById_b(int id);
}

测试

@Test
    public void testInsert_b(){
        user.setId(1234567806);
        user.setName("张三");
        user.setPassWord("123456");
        user.setBirthday(new Date());
        System.out.println(user);
        userXmlMapper.insert_b(user);
        System.out.println("插入成功!");
    }

    @Test
    public void testFindById_b(){
        User user = userXmlMapper.findById_b(1234567801);
        System.out.println(user);
    }
上一篇:自定义多数据源连接池


下一篇:模板方法模式