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

新手上路,请多包涵

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

题目描述

题目来源及自己的思路

相关代码

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

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

阅读 1.8k
推荐问题
宣传栏