mybatis 自定義枚舉轉換類

    最近在搭建maven多模塊項目: http://git.oschina.net/alexgaoyh/MutiModule-parent java

    項目總體代碼部分在上文連接的git中,下面的代碼段落只是部分我的以爲比較重要的段落 git

    在選擇持久化DAO部分的時候,選擇了mybatis,實體中變量的定義,想來來使用Enum代替常量的某些好處。因此纔有下文的這些功能: sql

    

package com.alexgaoyh.MutiModule.domain;

import java.io.Serializable;

import com.alexgaoyh.MutiModule.domain.enumState.DeleteFlagState;

public class BaseEntity implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 997102449095543774L;

	/**
	 * 編號,主鍵標示
	 */
	private Integer id;
	
	/**
	 * 是否刪除狀態
	 */
	private DeleteFlagState deleteFlagState = DeleteFlagState.NO;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public DeleteFlagState getDeleteFlagState() {
		return deleteFlagState;
	}

	public void setDeleteFlagState(DeleteFlagState deleteFlagState) {
		this.deleteFlagState = deleteFlagState;
	}
}



package com.alexgaoyh.MutiModule.persist.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import com.alexgaoyh.MutiModule.domain.enumState.DeleteFlagState;

/**
 * 自定義DeleteFlagState轉換類 <br>
 * 存儲屬性:DeleteFlagState.getCode() <br>
 */
public class DeleteFlagStateHandler extends BaseTypeHandler<DeleteFlagState> {
 
    private Class<DeleteFlagState> type;
 
    private final DeleteFlagState[] enums;
 
    /**
     * 設置配置文件設置的轉換類以及枚舉類內容,供其餘方法更便捷高效的實現
     * @param type 配置文件中設置的轉換類
     */
    public DeleteFlagStateHandler(Class<DeleteFlagState> type) {
        if (type == null)
            throw new IllegalArgumentException("Type argument cannot be null");
        this.type = type;
        this.enums = type.getEnumConstants();
        if (this.enums == null)
            throw new IllegalArgumentException(type.getSimpleName()
                    + " does not represent an enum type.");
    }
 
    @Override
    public DeleteFlagState getNullableResult(ResultSet rs, String columnName) throws SQLException {
        // 根據數據庫存儲類型決定獲取類型,本例子中數據庫中存放INT類型
        int i = rs.getInt(columnName);
         
        if (rs.wasNull()) {
            return null;
        } else {
            // 根據數據庫中的code值,定位DeleteFlagState子類
            return locateDeleteFlagState(i);
        }
    }
 
    @Override
    public DeleteFlagState getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        // 根據數據庫存儲類型決定獲取類型,本例子中數據庫中存放INT類型
        int i = rs.getInt(columnIndex);
        if (rs.wasNull()) {
            return null;
        } else {
            // 根據數據庫中的code值,定位DeleteFlagState子類
            return locateDeleteFlagState(i);
        }
    }
 
    @Override
    public DeleteFlagState getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        // 根據數據庫存儲類型決定獲取類型,本例子中數據庫中存放INT類型
        int i = cs.getInt(columnIndex);
        if (cs.wasNull()) {
            return null;
        } else {
            // 根據數據庫中的code值,定位DeleteFlagState子類
            return locateDeleteFlagState(i);
        }
    }
 
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, DeleteFlagState parameter, JdbcType jdbcType)
            throws SQLException {
        // baseTypeHandler已經幫咱們作了parameter的null判斷
        ps.setInt(i, parameter.getCode());
 
    }
     
    /**
     * 枚舉類型轉換,因爲構造函數獲取了枚舉的子類enums,讓遍歷更加高效快捷
     * @param code 數據庫中存儲的自定義code屬性
     * @return code對應的枚舉類
     */
    private DeleteFlagState locateDeleteFlagState(int code) {
        for(DeleteFlagState status : enums) {
            if(status.getCode().equals(Integer.valueOf(code))) {
                return status;
            }
        }
        throw new IllegalArgumentException("未知的枚舉類型:" + code + ",請覈對" + type.getSimpleName());
    }
 
}



<?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">
	
<!-- namespace 必須是DemoMapper接口的路徑 --> 
<mapper namespace="com.alexgaoyh.MutiModule.persist.demo.DemoMapper">

	<resultMap type="Demo" id="DemoResultMap">
		<id column="id" property="id"/>
		<result column="name" property="name"/>
		<result column="deleteFlagState" property="deleteFlagState" typeHandler="com.alexgaoyh.MutiModule.persist.handler.DeleteFlagStateHandler"/>
	</resultMap>
	
	<insert id="insertDemo" parameterType="Demo">
		insert into Demo(deleteFlagState, name) 
		values(
		#{deleteFlagState, typeHandler=com.alexgaoyh.MutiModule.persist.handler.DeleteFlagStateHandler},
		#{name})
	</insert>
	
	<select id="selectDemoById" resultMap="DemoResultMap" parameterType="java.lang.Integer">
		select * from Demo where id=#{id}
	</select>
	
</mapper>
相關文章
相關標籤/搜索