一 .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>