1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 3 4 <!--The content of element type "configuration" must match 5 "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?, 6 objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)".--> 7 8 <configuration> 9 10 <!--加载外部properties文件--> 11 <properties resource="jdbc.properties"></properties> 12 13 <!--自定义别名--> 14 <typeAliases> 15 <typeAlias type="com.haifei.domain.User" alias="user" /> 16 <typeAlias type="com.haifei.domain.Order" alias="order"/> 17 <typeAlias type="com.haifei.domain.Role" alias="role"/> 18 </typeAliases> 19 20 <!--注册类型处理器--> 21 <typeHandlers> 22 <typeHandler handler="com.haifei.handler.DateTypeHandler"/> 23 </typeHandlers> 24 25 <environments default="development"> 26 <!--配置数据源环境--> 27 <environment id="development"> 28 <transactionManager type="JDBC"></transactionManager> 29 <dataSource type="POOLED"> 30 <property name="driver" value="${jdbc.driver}"/> 31 <property name="url" value="${jdbc.url}"/> 32 <property name="username" value="${jdbc.username}"/> 33 <property name="password" value="${jdbc.password}"/> 34 </dataSource> 35 </environment> 36 </environments> 37 38 <!--加载映射文件--> 39 <mappers> 40 <mapper resource="com/haifei/mapper/UserMapper.xml"/> 41 <mapper resource="com/haifei/mapper/OrderMapper.xml"/> 42 </mappers> 43 44 </configuration>
1 package com.haifei.handler; 2 3 import org.apache.ibatis.type.BaseTypeHandler; 4 import org.apache.ibatis.type.JdbcType; 5 6 import java.sql.CallableStatement; 7 import java.sql.PreparedStatement; 8 import java.sql.ResultSet; 9 import java.sql.SQLException; 10 import java.util.Date; 11 12 public class DateTypeHandler extends BaseTypeHandler<Date> { 13 14 /** 15 * 将java数据类型 转换为 数据库所需的数据类型 16 * @param preparedStatement 17 * @param i 18 * @param date 19 * @param jdbcType 20 * @throws SQLException 21 */ 22 @Override 23 public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException { 24 long time = date.getTime(); 25 preparedStatement.setLong(i, time); 26 } 27 28 /** 29 * 将数据库中的数据类型 转换为 所需的java数据类型 30 * @param resultSet 查询出的结果集 31 * @param s 要转换的字段名称 32 * @return 33 * @throws SQLException 34 */ 35 @Override 36 public Date getNullableResult(ResultSet resultSet, String s) throws SQLException { 37 //获取结果集中需要的数据类型(long)转换为Date类型,并返回 38 long aLong = resultSet.getLong(s); 39 Date date = new Date(aLong); 40 return date; 41 } 42 43 /** 44 * 将数据库中的数据类型 转换为 所需的java数据类型 45 * @param resultSet 46 * @param i 47 * @return 48 * @throws SQLException 49 */ 50 @Override 51 public Date getNullableResult(ResultSet resultSet, int i) throws SQLException { 52 long aLong = resultSet.getLong(i); 53 Date date = new Date(aLong); 54 return date; 55 } 56 57 /** 58 * 将数据库中的数据类型 转换为 所需的java数据类型 59 * @param callableStatement 60 * @param i 61 * @return 62 * @throws SQLException 63 */ 64 @Override 65 public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException { 66 long aLong = callableStatement.getLong(i); 67 Date date = new Date(aLong); 68 return date; 69 } 70 }