mybatis自定义类型转换器

Mybatis类型转换介绍http://haohaoxuexi.iteye.com/blog/1847854

原文http://maimode.iteye.com/blog/1488620

mybatis提供了对自定义的类型转换器(typeHandler)的支持,因此我们可以自己编写类型转换器来实现这一自动转换的功能。

注意:

1.使用的时候,resultMap也select的SQL的参数,尽量要指定类型.

2.ibatis的方法:ibatis如何自动获取自定义的handlerhttp://daizuan.iteye.com/blog/1122969.SQL使用方法:SHOW_NAV_ADMIN=#showNavAdmin,jdbcType=VARCHAR,javaType=boolean#

3.如果是boolean类型,那么bean里面应该定义为Booelean类型,而不是基础类型boolean.

实现步骤:

第一步:编写自定义类型转换器

/** 
 *  
 */  
package test.atfm.persistence.mybatis.handler;  
  
import java.sql.CallableStatement;  
import java.sql.PreparedStatement;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
  
import org.apache.ibatis.type.JdbcType;  
import org.apache.ibatis.type.TypeHandler;  
  
/** 
 * @author  
 * java中的boolean和jdbc中的char之间转换;true-Y;false-N 
 */  
public class BooleanTypeHandler implements TypeHandler {  
  
    /* (non-Javadoc) 
     * @see org.apache.ibatis.type.TypeHandler#getResult(java.sql.ResultSet, java.lang.String) 
     */  
    @Override  
    public Object getResult(ResultSet arg0, String arg1) throws SQLException {  
        String str = arg0.getString(arg1);  
        Boolean rt = Boolean.FALSE;  
        if (str.equalsIgnoreCase("Y")){  
            rt = Boolean.TRUE;  
        }  
        return rt;   
    }  
  
    /* (non-Javadoc) 
     * @see org.apache.ibatis.type.TypeHandler#getResult(java.sql.CallableStatement, int) 
     */  
    @Override  
    public Object getResult(CallableStatement arg0, int arg1)  
            throws SQLException {  
        Boolean b = arg0.getBoolean(arg1);  
        return b == true ? "Y" : "N";  
    }  
  
    /* (non-Javadoc) 
     * @see org.apache.ibatis.type.TypeHandler#setParameter(java.sql.PreparedStatement, int, java.lang.Object, org.apache.ibatis.type.JdbcType) 
     */  
    @Override  
    public void setParameter(PreparedStatement arg0, int arg1, Object arg2,  
            JdbcType arg3) throws SQLException {  
        Boolean b = (Boolean) arg2;  
        String value = (Boolean) b == true ? "Y" : "N";  
        arg0.setString(arg1, value);  
    }  
}

第二步:注册类型转换器

<?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="db.properties"></properties>  
  
    <typeHandlers>  
        <typeHandler javaType="Boolean" jdbcType="CHAR"  
        handler="test.atfm.persistence.mybatis.handler.BooleanTypeHandler" />
    </typeHandlers>  
    <environments default="development">  
        <environment id="development">  
            <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>  
</configuration>

第三步:指定类型转换

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE mapper    
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"    
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">  
  
<mapper namespace="test.atfm.persistence.mybatis.TestMapper">  
    <resultMap type="TestBean" id="TestBeanMap">  
        <id property="tname" column="tname" />  
        <result property="isCancel" column="iscancel" javaType="Boolean" jdbcType="CHAR"/>
    </resultMap>  
  
    <select id="selectTest" resultMap="TestBeanMap">  
    select * from t_test  
    </select>  
    <insert id="insertTest" parameterType="TestBean">  
    insert into t_test(tname,iscancel) values(#{tname},#{isCancel jdbcType=CHAR javaType=Boolean})  
    </insert>  
</mapper>

这样mybatis就能将数据库中的类型与java中的类型自动做转换了。其他类型转方法同上。

相关推荐