一、作用及默认的处理
类型处理器的作用就是
-
查询时把数据库存储的值转换成java类型
-
修改是把java类型转换成数据库类型存储,处理
下面这个表格描述了默认的类型处理器。
类型处理器
|
Java 类型
|
JDBC 类型
|
BooleanTypeHandler
|
java.lang.Boolean, boolean
|
任何兼容的布尔值
|
ByteTypeHandler
|
java.lang.Byte, byte
|
任何兼容的数字或字节类型
|
ShortTypeHandler
|
java.lang.Short, short
|
任何兼容的数字或短整型
|
IntegerTypeHandler
|
java.lang.Integer, int
|
任何兼容的数字和整型
|
LongTypeHandler
|
java.lang.Long, long
|
任何兼容的数字或长整型
|
FloatTypeHandler
|
java.lang.Float, float
|
任何兼容的数字或单精度浮点型
|
DoubleTypeHandler
|
java.lang.Double, double
|
任何兼容的数字或双精度浮点型
|
BigDecimalTypeHandler
|
java.math.BigDecimal
|
任何兼容的数字或十进制小数类型
|
StringTypeHandler
|
java.lang.String
|
CHAR 和 VARCHAR 类型
|
ClobTypeHandler
|
java.lang.String
|
CLOB 和 LONGVARCHAR 类型
|
NStringTypeHandler
|
java.lang.String
|
NVARCHAR 和 NCHAR 类型
|
NClobTypeHandler
|
java.lang.String
|
NCLOB 类型
|
ByteArrayTypeHandler
|
byte[]
|
任何兼容的字节流类型
|
BlobTypeHandler
|
byte[]
|
BLOB 和 LONGVARBINARY 类型
|
DateTypeHandler
|
java.util.Date
|
TIMESTAMP 类型
|
DateOnlyTypeHandler
|
java.util.Date
|
DATE 类型
|
TimeOnlyTypeHandler
|
java.util.Date
|
TIME 类型
|
SqlTimestampTypeHandler
|
java.sql.Timestamp
|
TIMESTAMP 类型
|
SqlDateTypeHandler
|
java.sql.Date
|
DATE 类型
|
SqlTimeTypeHandler
|
java.sql.Time
|
TIME 类型
|
ObjectTypeHandler
|
Any
|
其他或未指定类型
|
EnumTypeHandler
|
Enumeration Type
|
VARCHAR-任何兼容的字符串类型, 作为代码存储(而不是索引)
|
EnumOrdinalTypeHandler
|
Enumeration Type
|
Any compatible NUMERIC or DOUBLE, as the position is stored (not the code itself).
|
二、自定义类型
例如我们在数据库经常把时间存储为一个14位的VARCHAR(YYYYmmddssss)。而前台java层为java.util.Date,在这里我们就简单做一个类型处理。
-
package com.elements.typehandler;
-
-
import java.sql.CallableStatement;
-
import java.sql.PreparedStatement;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import java.text.SimpleDateFormat;
-
import java.util.Date;
-
-
import org.apache.ibatis.type.BaseTypeHandler;
-
import org.apache.ibatis.type.JdbcType;
-
-
public class DataTimeTypeHandler extends BaseTypeHandler<Date> {
-
-
/**
-
*
-
* 这里应该是在保存数据的时候,把前台的日期类型转换成数据的varchar 我们使用了SimpleDateFormat来转换data类型
-
* */
-
@Override
-
public void setNonNullParameter(PreparedStatement ps, int i,
-
Date parameter, JdbcType jdbcType) throws SQLException {
-
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
-
ps.setString(i, sdf.format(parameter));
-
}
-
-
/**
-
* 下面三个函数应该都是返回到java层时调用,我们把varchar转换成Date类型
-
*/
-
@Override
-
public Date getNullableResult(ResultSet rs, String columnName)
-
throws SQLException {
-
String varchartime = rs.getString(columnName);
-
if (varchartime!=null)
-
return new Date(Long.parseLong(varchartime));
-
else return null;
-
}
-
-
@Override
-
public Date getNullableResult(ResultSet rs, int columnIndex)
-
throws SQLException {
-
String varchartime = rs.getString(columnIndex);
-
if (varchartime!=null)
-
return new Date(Long.parseLong(varchartime));
-
else return null;
-
}
-
-
@Override
-
public Date getNullableResult(CallableStatement cs, int columnIndex)
-
throws SQLException {
-
String varchartime = cs.getString(columnIndex);
-
if (varchartime!=null)
-
return new Date(Long.parseLong(varchartime));
-
else return null;
-
}
-
-
}
-
<?xml version="1.0" encoding="UTF-8"?>
-
<!DOCTYPE configuration
-
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
-
"">
-
-
<configuration>
-
-
<properties resource="jdbc.properties" />
-
-
<typeHandlers>
-
<typeHandler handler="com.elements.typehandler.DataTimeTypeHandler" javaType="java.util.Date" jdbcType="VARCHAR"/>
-
<package name="com.elements.user"/>
-
</typeHandlers>
-
-
<environments default="dev">
-
<environment id="dev">
-
<transactionManager type="JDBC" />
-
<dataSource type="POOLED">
-
<property name="driver" value="${driver}" />
-
<property name="url" value="${url}" />
-
<property name="username" value="${username}" />
-
<property name="password" value="${password}" />
-
</dataSource>
-
</environment>
-
</environments>
-
-
-
-
<mappers>
-
<mapper class="com.elements.user.dao.UserMapper" />
-
-
</mappers>
-
-
</configuration>
User类
-
-
<?xml version="1.0" encoding="UTF-8" ?>
-
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "" >
-
<mapper namespace="com.elements.user.dao.UserMapper" >
-
-
-
<resultMap id="BaseResultMap" type="com.elements.user.model.User" >
-
-
<!--
-
WARNING - @mbggenerated
-
This element is automatically generated by MyBatis Generator, do not modify.
-
This element was generated on Wed Apr 27 15:05:09 CST 2016.
-
-->
-
<id column="userId" property="userid" jdbcType="INTEGER" />
-
<result column="UserName" property="username" jdbcType="VARCHAR" />
-
<result column="UserEmail" property="useremail" jdbcType="VARCHAR" />
-
<result column="createTime" property="createtime" jdbcType="VARCHAR" />
-
<result column="updateTime" property="updatetime" jdbcType="VARCHAR" />
-
</resultMap>
-
<sql id="Base_Column_List" >
-
<!--
-
WARNING - @mbggenerated
-
This element is automatically generated by MyBatis Generator, do not modify.
-
This element was generated on Wed Apr 27 15:05:09 CST 2016.
-
-->
-
userId, UserName, UserEmail, createTime, updateTime
-
</sql>
-
-
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
-
<!--
-
WARNING - @mbggenerated
-
This element is automatically generated by MyBatis Generator, do not modify.
-
This element was generated on Wed Apr 27 15:05:09 CST 2016.
-
-->
-
select
-
<include refid="Base_Column_List" />
-
from user
-
where userId = #{userid,jdbcType=INTEGER}
-
</select>
-
-
<insert id="insert" parameterType="com.elements.user.model.User" >
-
<!--
-
WARNING - @mbggenerated
-
This element is automatically generated by MyBatis Generator, do not modify.
-
This element was generated on Wed Apr 27 15:05:09 CST 2016.
-
-->
-
insert into user (userId, UserName, UserEmail,
-
createTime, updateTime)
-
values (#{userid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{useremail,jdbcType=VARCHAR},
-
#{createtime,jdbcType=VARCHAR}, #{updatetime,jdbcType=VARCHAR})
-
</insert>
-
-
-
</mapper>
注意:我们没有在mapper文件内显示的做类型转换 ,他已经默认把这两种类型(javaType="java.util.Date" jdbcType="VARCHAR" )的互转都调用我们的类型处理了
代码下载地址:
阅读(2526) | 评论(0) | 转发(0) |