Mybatis自定义typehandler

Mybatis自定义typehandler

  1. 自定义typehandler
    官方的建议是继承BaseTypeHandler,其中Typehandler可以受一个type参数,Mybatis会自动传进来实际的class类型。

    /**
     * 用来处理我们自定义的枚举 
     * @author sut
     * @version $Revision:$
     */
    public class GenericEnumUserType<E extends StringEnumTypeImp> extends BaseTypeHandler<E>{
    
        //mybatis will pass actual class when constructing TypeHandler
        private Class type;
    
        private static final String fromStringCode = "fromStringCode";
    
        public GenericEnumUserType(Class type){
            Preconditions.checkNotNull(type, "Type argument cannot be null");
            this.type = type;
        }
    
        /**
         * 设置非空参数
         * @see org.apache.ibatis.type.BaseTypeHandler#setNonNullParameter(java.sql.PreparedStatement, int, java.lang.Object, org.apache.ibatis.type.JdbcType)
         */
        @Override
        public void setNonNullParameter(PreparedStatement ps, int i, StringEnumTypeImp parameter, JdbcType jdbcType) throws SQLException {
            ps.setString(i, parameter.getStoreValue());
        }
    
        /**
         * 得到非空结果,然后转换成E
    * 利用反射调用静态的方法
    * @see org.apache.ibatis.type.BaseTypeHandler#getNullableResult(java.sql.ResultSet, java.lang.String) */
    @Override @SuppressWarnings("unchecked") public E getNullableResult(ResultSet rs, String columnName) throws SQLException { String storeValue = rs.getString(columnName); Preconditions.checkNotNull(type, "Type argument cannot be null"); try { Method fromMethod = type.getMethod(fromStringCode, String.class); return (E) fromMethod.invoke(null, storeValue); } catch (IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } return null; } /** * 空的结果返回类型 * @see org.apache.ibatis.type.BaseTypeHandler#getNullableResult(java.sql.ResultSet, int) */ @Override public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException { try { return type.newInstance(); } catch (InstantiationException | IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * 针对callback的空值结果 * @see org.apache.ibatis.type.BaseTypeHandler#getNullableResult(java.sql.CallableStatement, int) */ @Override public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { try { return type.newInstance(); } catch (InstantiationException | IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }
  2. 注册TypeHandler




    javaType="com.sut.util.meta.WhetherTypeEnum" jdbcType="VARCHAR"/>

    javaType="com.sut.util.meta.SexTypeEnum" jdbcType="VARCHAR" />

  3. 这样我们在mapper就可以使用了。

    
    
    <mapper namespace="com.sut.persist.dao.UserDao">
    
        
        <resultMap type="com.sut.persist.entity.User" id="userResult">
            <id property="id" javaType="int" column="id" />
            <result property="username" javaType="String" column="USERNAME" />
            <result property="password" javaType="String" column="PASSWORD" />
            <result property="enable" javaType="com.sut.util.meta.WhetherTypeEnum" column="enabled" 
                typeHandler="com.sut.util.enumerate.mybatis.GenericEnumUserType" />
            <result property="createTime" javaType="java.util.Date" column="CREATE_TIME" />
            <result property="lastLoginTime" javaType="java.util.Date" column="LAST_LOGIN_DATE" />
            <result property="sex" javaType="com.sut.util.meta.SexTypeEnum" column="SEX" 
                typeHandler="com.sut.util.enumerate.mybatis.GenericEnumUserType" />
            <result property="email" javaType="String" column="EMAIL" />
            <result property="mobile" javaType="String" column="MOBILE" />
            <result property="province" javaType="String" column="PROVINCE" />
            <result property="city" javaType="String" column="CITY" />
            <result property="area" javaType="String" column="AREA" />
            <result property="address" javaType="String" column="ADDRESS" />
            <result property="remark" javaType="String" column="REMARK" />
        resultMap>
    
        
        <select id="getById" parameterType="int"  resultMap="userResult">
            select 
             *
            from user where id = #{id}
        select>
    
        
        <select id="getByName" parameterType="String" resultMap="userResult">
            select * from user where username = #{username}
        select>
    
         
        <select id="getAll" resultMap="userResult">
            select 
                id as id,
                username as username,
                password as password,
                enabled as enable,
                create_time as createTime,
                last_login_date as lastLoginTime,
                email as email,
                mobile as mobile,
                province as province,
                city as city,
                area as area,
                address as address,
                remark as remark
            from user
        select>
    
        
        <delete id="deleteById" parameterType="int">
            delete from user where id = #{id}
        delete>
    
        
        <insert id="insert">
            insert into user(
                username,
                password,
                enabled,
                create_time,
                last_login_date,
                sex,
                email,
                mobile,
                province,
                city,
                area,
                address,
                remark
            )values(
                #{username},
                #{password},
                #{enable, typeHandler=com.sut.util.enumerate.mybatis.GenericEnumUserType, jdbcType=VARCHAR,
                     javaType=com.sut.util.meta.WhetherTypeEnum},
                #{createTime},
                #{lastLoginDate},
                #{sex, typeHandler=com.sut.util.enumerate.mybatis.GenericEnumUserType, jdbcType=VARCHAR, 
                     javaType=com.sut.util.meta.SexTypeEnum},
                #{email},
                #{mobile},
                #{province},
                #{city},
                #{area},
                #{address},
                #{remark}
            )
        insert>
    mapper>
  4. 测试insert和getAll()方法是可以的。

UPDATE:

  1. 2017/01/07 有一个问题:
    当使用自定义typeHandler的时候,如果bean的属性名和表字段的名称是一样的话,就会被mybatis分为mappedColumns,获取的typeHandler的rawType就不正确,如果是不一样的话,就会成为unMappedColumns, 获取的就是对的. 在测试过程中,即便设置AutoMappingBehavior=NONE,也还是会执行column分类. 最后解决办法只能是改变bean的property名称。

  2. 2017/01/23 来自SO的回答说:
    说是将ResultMapping定义的时候,将education的TypeHandler移除掉,然后指定好javaType,jdbcType就可以了。试了一下,果然可以。但是不明白为什么这样。

你可能感兴趣的:(JAVA)