新写了一个接口,期望根据不同的参数来给数据库中不同的字段进行传值。这里使用了内部静态枚举类的方式进行传值,在写mybatis动态sql时,如果是普通对象,一般使用,那么使用枚举类,如何判断枚举类的值呢?
Mapper接口
public class SLineSboxesQueryParam {
private QueryMethod queryMethod;//查询方式的枚举内部类
private List<String> idList;
private LocalDateTime startTime;
private LocalDateTime endTime;
public SLineSboxesQueryParam() {
}
//省略getter setter方法
public void checkHasNecessaryFields() {
if (this.queryMethod == null) {
throw new ApiException(ResultCode.PARAMS_ERROR, "queryMethod is missing");
} else if (CollectionUtils.isEmpty(this.idList)) {
throw new ApiException(ResultCode.PARAMS_ERROR, "idList is missing");
} else if (this.startTime == null) {
throw new ApiException(ResultCode.PARAMS_ERROR, "startTime is missing");
} else if (this.endTime == null) {
throw new ApiException(ResultCode.PARAMS_ERROR,"endTime is missing");
}
}
//定义查询方式
public static enum QueryMethod { //此处定义了内部枚举类
BySpecIdList, BySLineIdList, ByVplIdList;
QueryMethod() {
}
}
}
mappers.xml配置
//resultMap
<resultMap id="sline_sboxes_map" type="com.hierway.vslm.domain.stream.SLineSboxesVO">
<id column="stream_line_id" property="streamLineId"/>
<result column="name" property="lineName"/>
<result column="area_id" property="areaId"/>
<result column="sl_spec_id" property="specId"/>
<result column="sl_vpl_id" property="vplId"/>
<result column="status" property="status"/>
<result column="line_start_time" property="startTime"/>
<result column="line_end_time" property="endTime"/>
<collection property="sboxes" ofType="com.hierway.vslm.dataaccess.mybatis.dao.SBox">
<id property="streamBoxId" column="stream_box_id"/>
<result property="streamLineId" column="line_id"/>
<result property="startTime" column="box_start_time"/>
<result property="endTime" column="box_end_time"/>
<result property="capability" column="capability"/>
<result property="useState" column="use_state"/>
<result property="opState" column="op_state"/>
<result property="setId" column="set_id"/>
<result property="reqId" column="req_id"/>
<result property="specId" column="spec_id"/>
<result property="preSetId" column="pre_set_id"/>
<result property="preUseCapability" column="pre_use_capability"/>
<result property="lastSetId" column="last_set_id"/>
<result property="lastUseCapability" column="last_use_capability"/>
</collection>
</resultMap>
//sql
<!-- List<SLineSboxesVO> getSLineSboxesVOByIdList(SLineSboxesQueryParam param);-->
<select id="getSLineSboxesVOByIdList" resultType="com.hierway.vslm.domain.stream.SLineSboxesQueryParam" resultMap="sline_sboxes_map">
SELECT sl.name,sl.stream_line_id,sl.area_id,sl.spec_id as sl_spec_id,sl.vpl_id as sl_vpl_id,sl.status,sl.start_time as line_start_time,sl.end_time as line_end_time,
sb.stream_box_id,sb.stream_line_id as line_id,sb.start_time as box_start_time,sb.end_time as box_end_time,sb.capability,sb.use_state,sb.op_state,sb.set_id,sb.req_id,sb.spec_id,
sb.pre_set_id,sb.pre_use_capability,sb.last_set_id,sb.last_use_capability
FROM
stream_line as sl
JOIN stream_box as sb ON sl.stream_line_id = sb.stream_line_id
<where>
<choose>
<when test=‘queryMethod == @c[email protected]BySpecIdList‘> //<<<<<<<<<<<<<<<
AND sb.spec_id IN
<foreach collection="idList" index="index" item="item" close=")" open="(" separator=".">
#{item}
</foreach>
</when>
<when test=‘queryMethod == @c[email protected]BySLineIdList‘> //<<<<<<<<<<<<<<<
AND sb.stream_line_id IN
<foreach collection="idList" index="index" item="item" close=")" open="(" separator=".">
#{item}
</foreach>
</when>
<otherwise>
AND vpl_id IN
<foreach collection="idList" index="index" item="item" close=")" open="(" separator=".">
#{item}
</foreach>
</otherwise>
</choose>
<if test="startTime != null">
AND sb.start_time >= #{startTime}
</if>
<if test="endTime != null">
AND sb.end_time <= #{endTime}
</if>
</where>
</select>
原文地址:https://www.cnblogs.com/yiweiblog/p/12652529.html
时间: 2024-10-11 19:15:07