使用内部枚举类作为外部类的参数的Mybatis的参数该如何判断

Posted 奕为

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用内部枚举类作为外部类的参数的Mybatis的参数该如何判断相关的知识,希望对你有一定的参考价值。

新写了一个接口,期望根据不同的参数来给数据库中不同的字段进行传值。这里使用了内部静态枚举类的方式进行传值,在写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 == @com.hierway.vslm.domain.stream.SLineSboxesQueryParam$QueryMethod@BySpecIdList‘> //<<<<<<<<<<<<<<<
 AND sb.spec_id IN
<foreach collection="idList" index="index" item="item" close=")" open="(" separator=".">
 #{item}
</foreach>
 </when>
 <when test=‘queryMethod == @com.hierway.vslm.domain.stream.SLineSboxesQueryParam$QueryMethod@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 &gt;= #{startTime}
        </if>
 <if test="endTime != null">
 AND sb.end_time &lt;= #{endTime}
        </if>
 </where>

</select>

以上是关于使用内部枚举类作为外部类的参数的Mybatis的参数该如何判断的主要内容,如果未能解决你的问题,请参考以下文章

Java基础08内部类枚举类日期和时间MathRandom

使用外部类的模板参数设置内部模板类的默认模板参数

内部类之静态内部类

成员内部类的优点

JAVA知识盲区整理

内部类和外部类的实例变量可以共存