标签:
类型处理器的作用就是
类型处理器 | 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"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<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>
package com.elements.user.model;
import java.util.Date;
public class User {
private Integer userid;
private String username;
private String useremail;
private Date createtime;
private Date updatetime;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
public String getUseremail() {
return useremail;
}
public void setUseremail(String useremail) {
this.useremail = useremail == null ? null : useremail.trim();
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime ;
}
public Date getUpdatetime() {
return updatetime;
}
public void setUpdatetime(Date updatetime) {
this.updatetime = updatetime;
}
@Override
public String toString() {
return "User [userid=" + userid + ", username=" + username
+ ", useremail=" + useremail + ", createtime=" + createtime
+ ", updatetime=" + updatetime + "]";
}
}
<?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.elements.user.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.elements.user.model.User" >
<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" >
userId, UserName, UserEmail, createTime, updateTime
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user
where userId = #{userid,jdbcType=INTEGER}
</select>
<insert id="insert" parameterType="com.elements.user.model.User" >
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” )的互转都调用我们的类型处理了
代码下载地址:http://pan.baidu.com/s/1eRDzY6m
标签:
原文地址:http://blog.csdn.net/likewindy/article/details/51350995