TypeHandler转换失败 都配置了结果 数据没有转换

我的天空
  • 1
新手上路,请多包涵

1.继承BaseTypeHandler 类 改写四个方法

public class DateTypeHandler extends BaseTypeHandler<Date> {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException {
        long time = date.getTime();
        preparedStatement.setLong(i,time);
    }
    @Override
    public Date getNullableResult(ResultSet resultSet, String s) throws SQLException {
        long aLong = resultSet.getLong(s);
        Date date = new Date(aLong);
        return date;
    }
    @Override
    public Date getNullableResult(ResultSet resultSet, int i) throws SQLException {
        long aLong = resultSet.getLong(i);
        Date date = new Date(aLong);
        return date;
    }
    @Override
    public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        long aLong = callableStatement.getLong(i);
        Date date = new Date(aLong);
        return date;
    }
}

2.Mybatis核心配置文件注册

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--    引入配置文件-->
    <properties resource="jdbc.properties"></properties>
<!--    起别名-->
    <typeAliases>
        <typeAlias type="com.vx202158.User" alias="user"/>
    </typeAliases>
<!--    注册类型处理器-->
    <typeHandlers>
        <typeHandler handler="com.vx202158.TypeHandler.DateTypeHandler"></typeHandler>
    </typeHandlers>
<!--    数据源环境-->
    <environments default="developement">
        <environment id="developement">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
<!--    加载映射文件-->
    <mappers>
        <mapper resource="com.vx202158.mapper/UserMapper.xml"></mapper>
    </mappers>
</configuration>

3.测试类

 @Test
    public void test4() throws IOException {
        User user = new User();
        user.setUser("张三");
        user.setPassword("123");
        user.setBirthday(new Date());
 
        //加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapperConfig.xml");
        //获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获取sqlsession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        mapper.saveUser(user);
        sqlSession.commit();
        sqlSession.close();
    }

4.日志
数据没有转换
image.png

5.报错

Data truncated for column 'birthday' at row 1

6.数据库列类型

image.png

题目描述

题目来源及自己的思路

相关代码

粘贴代码文本(请勿用截图)

你期待的结果是什么?实际看到的错误信息又是什么?

回复
阅读 328
1 个回答

mapper.xml 也要单独指定handleType
image.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏