1. 程式人生 > >Mybatis傳單個String和列舉型別的引數應該注意的問題

Mybatis傳單個String和列舉型別的引數應該注意的問題

一 .Mybatis傳遞單個String,應該用_parameter 而不應該用它本身的變數名

<select id="getJobByCode" parameterType="java.lang.String" resultMap="BaseResultMap">
		select 
		o.org_id,
		o.job_name,
		o.job_code,
		o.job_parent_code,
		o.job_type,
		o.job_level
		from tb_job o
		<where>
			<if test='_parameter != null and _parameter != ""'>
				o.job_level = #{_parameter}
			</if>		
		</where>
	</select>

二 .Mybatis傳遞列舉型別引數,應該用自定義一個類實現 TypeHandler<T>,其中T為你指定的列舉類,然後在mapper.xml檔案裡面配置相應的type指定型別即可

(1)定義列舉類

public enum Status {

    VALID("系統下單", "1"), INVALID("外部匯入", "0");

    private String display;

    private String value;

    private Status(String display, String value) {
        this.display = display;
        this.value = value;
    }

    public String getDisplay() {
        return display;
    }

    public static String getDisplay(String value) {
        for (Status status : Status.values()) {
            if (status.getValue().equals(value)) {
                return status.display;
            }
        }
        return null;
    }

    public String getValue() {
        return value;
    }

    public static String getValue(String display) {
        for (Status status : Status.values()) {
            if (status.getDisplay().equals(display)) {
                return status.value;
            }
        }
        return null;
    }

    public static Status displayOf(String display) {
        if (display == null) {
            throw new NullPointerException("display is null");
        }
        for (Status status : Status.values()) {
            if (status.getDisplay().equals(display)) {
                return status;
            }
        }
        throw new IllegalArgumentException("No enum display " + display);
    }

    public static Status newValueOf(String value) {
        if (value == null) {
            throw new NullPointerException("value is null");
        }
        for (Status status : Status.values()) {
            if (status.getValue().equals(value)) {
                return status;
            }
        }
        throw new IllegalArgumentException("No enum new value " + value);
    }
}

(2)定義類實現TypeHandler<T>

public class EnumHandlerStatus implements TypeHandler<Status> {

    @Override
    public void setParameter(PreparedStatement ps, int i, Status status, JdbcType jdbcType) throws SQLException {
        ps.setString(i, status.getValue());
    }

    @Override
    public Status getResult(ResultSet rs, String columnName) throws SQLException {
        String status = rs.getString(columnName);
        return Status.newValueOf(status);
    }

    @Override
    public Status getResult(ResultSet rs, int columnIndex) throws SQLException {
        String status = rs.getString(columnIndex);
        return Status.newValueOf(status);
    }

    @Override
    public Status getResult(CallableStatement cs, int columnIndex) throws SQLException {
        String status = cs.getString(columnIndex);
        return Status.newValueOf(status);
    }

}

(3)xml檔案中配置相應的type

resultMap指定type

<resultMap id="ChannelResult" type="com.ejsino.ejbxbis.entity.system.channel.Channel" >
      <id column="channel_id" property="channelId" jdbcType="INTEGER" />
	  <result column="channel_code" property="channelCode" jdbcType="VARCHAR" />
      <result column="channel_name" property="channelName" jdbcType="VARCHAR" />
	  <result column="efftflage" property="efftflage" jdbcType="VARCHAR"
			  typeHandler="com.ejsino.ejbxbis.config.mybatis.EnumHandlerStatus"/>
      <result column="create_by" property="createBy" jdbcType="VARCHAR" />
      <result column="create_date" property="createDate" jdbcType="TIMESTAMP" />
      <result column="update_by" property="updateBy" jdbcType="VARCHAR" />
      <result column="update_date" property="updateDate" jdbcType="TIMESTAMP" />
  </resultMap>

insert語句指定type

<insert id="addChannel" parameterType="com.ejsino.ejbxbis.entity.system.channel.Channel">
		insert INTO  s_channel(
		channel_code,
		channel_name,
		efftflage,
		create_by,
		create_date,
		update_by,
		update_date
		)VALUES(
        #{channelCode},
        #{channelName},
        #{efftflage,typeHandler=com.ejsino.ejbxbis.config.mybatis.EnumHandlerStatus},
        #{createBy},
        now(),
        #{updateBy},
        #{updateDate}
		);
	</insert>