美文网首页
使用内部枚举类作为外部类的参数的Mybatis的参数该如何判断

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

作者: 奕为 | 来源:发表于2019-10-15 16:38 被阅读0次

新写了一个接口,期望根据不同的参数来给数据库中不同的字段进行传值。这里使用了内部静态枚举类的方式进行传值,在写mybatis动态sql时,如果是普通对象,一般使用<if test =" queryMethod == 'BySpec'">,那么使用枚举类,如何判断枚举类的值呢?

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的参数该如何判断

    新写了一个接口,期望根据不同的参数来给数据库中不同的字段进行传值。这里使用了内部静态枚举类的方式进行传值,在写my...

  • Java内部类反射上的坑

    java的内部类,使用反射时, 构造器参数会多出外部类的类型作为第一个参数。具体l例子描述如下: 内部类定义如下:...

  • JavaSE第22篇:Lambda表达式、函数式接口

    核心概述:在开发中,我们经常使用匿名内部类作为实参传递参数,我们可以发现匿名内部类的格式比较繁琐,那么如何简化呢?...

  • 10.匿名内部类的应用场景

    匿名内部类作为参数传递

  • 内部类

    内部类共分为: 一,成员内部类 二,局部内部类 在方法中定义类其中注意参数,如果传入参数则hi会被替换为xx,如果...

  • javase part24 匿名局部内部类

    匿名局部内部类 就是没有名字的局部内部类匿名局部内部类使用: 参数为一个接口类型,但是想省去创建一个类文件,就可以...

  • EnumSet,EnumMap

    EnumSet 枚举单参数能参与switch case值判断: 测试结果: 每个枚举类型颜色是该Color类的一个...

  • Java Lambda 和 Kotlin Lambda 的区别

    Java 匿名内部类在编译时会创建一个 class ,增加类的加载开销,运行时该内部类无论是否用到外部参数每次都会...

  • Java - 详解匿名内部类

    使用匿名内部类 匿名内部类由于没有名字,所以它的创建方式有点儿奇怪。创建格式如下: new 父类构造器(参数列表)...

  • 内部类形参为何要用final

    我们给匿名内部类传递参数的时候,若该形参在内部类中需要被使用,那么该形参必须要为final。也就是说:**当所在的...

网友评论

      本文标题:使用内部枚举类作为外部类的参数的Mybatis的参数该如何判断

      本文链接:https://www.haomeiwen.com/subject/rjbsmctx.html