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>











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HaleyLiu123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值