类型转换器:mybatis中有一些常用的类型转换器,比如把Java中的short类型转换为mysql中的short类型;但是如果现在是Java中的Date类型,但是我想要存储到数据库中转换为Long类型的毫秒值(默认1970-00-00-00至今),就需要自己定义转换器
1.创建实体类User
public class User {
private int id;
private String username;
private String password;
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", birthday=" + birthday +
'}';
}
}
2.编写UserMapper.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.hao.mapper.UserMapper">
<insert id="save" parameterType="user">
insert into user values(#{id},#{username},#{password},#{birthday})
</insert>
</mapper>
3.编写SqlMapConfig.xml文件
<?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>
<!-- 加载jdbc.properties配置文件-->
<properties resource="jdbc.properties"/>
<!-- 定义别名-->
<typeAliases>
<typeAlias type="com.hao.domain.User" alias="user"/>
</typeAliases>
<!-- 注册类型处理器-->
<typeHandlers>
<typeHandler handler="com.hao.handler.DateTypeHandler"/>
</typeHandlers>
<!-- 配置数据源环境-->
<environments default="development"><!-- 表示默认情况下使用id为development的环境-->
<environment id="development">
<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/hao/mapper/UserMapper.xml"/>
</mappers>
</configuration>
4.自定义类型处理器(必须实现BaseTypeHandler类,泛型中的Date就是要转换的Java类型),实现其中的四个方法
public class DateTypeHandler extends BaseTypeHandler<Date> {
//将java类型转换为数据库需要的类型
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException {
long time = date.getTime();
preparedStatement.setLong(i,time);
}
//将数据库中的类型转化为Java类型
//String:要转换的字段的名称
//ResultSet:查询的结果集
@Override
public Date getNullableResult(ResultSet resultSet, String s) throws SQLException {
//获取结果集中需要的数据(long)转换成Date类型
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;
}
}
5.测试
public class MapperTest {
@Test
public void test1() throws IOException {
InputStream stream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSession sqlSession = new SqlSessionFactoryBuilder().build(stream).openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setUsername("ceshi");
user.setPassword("abc");
user.setDate(new Date());
//执行保存
mapper.save(user);
sqlSession.commit();
}
}
6.结果
7.从数据库中读取出来
在UserMapper中添加查询sql语句
<?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.hao.mapper.UserMapper">
<insert id="save" parameterType="user">
insert into user values(#{id},#{username},#{password},#{date})
</insert>
<select id="findById" parameterType="int" resultType="user">
select * from user where id=#{id}
</select>
</mapper>
8.测试
public class MapperTest {
@Test
public void test1() throws IOException {
InputStream stream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSession sqlSession = new SqlSessionFactoryBuilder().build(stream).openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.findById(8);
System.out.println(user.getBirthday());
}
}
9.结果