MyBatis动态参数为数组、list、Map,并使用oder by给结果排序

在使用Mybatis时,传入的参数有可能为数组、链表和Map,如果参数为数组或链表,主要使用foreach标签来进行迭代处理,以下给出示例:
实体类

public class TestResult {
    private Integer resultId;
    private Date startTime;
    ...
    private Integer taskId;
    private Integer deviceId;
    ...
}

映射文件
1.数组
传入参数为数组,Mapper.xml文件中为:

<select id="selectByTaskIds" resultType="TestResult">
    SELECT
    *
    FROM t_test_result
    WHERE fk_task_id IN
    <foreach collection="array" item="taskIds" index="index" open="(" separator="," close=")">
        #{taskIds}
    </foreach>
</select>

foreach标签中,collection的值为array。

2.List
传入参数为List,则Mapper.xml文件中为:

<select id="selectByTaskIds" resultType="TestResult">
    SELECT
    *
    FROM t_test_result
    WHERE fk_task_id IN
    <foreach collection="list" item="taskIds" index="index" open="(" separator="," close=")">
        #{taskIds}
    </foreach>
</select>

foreach标签中,collection的值为list。
由上可知,根据参数类型的不同,foreach标签里面的collection值也不同,数组为array,List为list。其中 item的值可以任意取名,但是必须与#{}里面的值一样
建议使用mybatis-generator直接生成dao(后续添加链接),然后再添加其他功能。

3.Map
传入参数为Map,

<select id="getResultsMapParmas" resultType="TestResult">
        SELECT
        *
        FROM t_test_result
        <if test="taskIds != null and taskIds.size != 0">
            WHERE fk_task_id IN
            <foreach collection="taskIds" item="taskIds" index="index" open="(" separator="," close=")">
                #{taskIds}
            </foreach>
        </if>
        <if test="orderName != null and orderName != ''">
            ORDER BY ${orderName} DESC
        </if>
        <if test="startRow != null and pageSize != null and pageSize != ''">
            limit #{startRow},#{pageSize}
        </if>
    </select>

测试类

@Test
public void testGetResultsMapParmas(){

    //参数是数组,则在xml文件中if判断时,taskIds.length != 0
//    int[] array = new int[5];
//    array[0] = 9;
//    array[1] = 12;

    //参数是列表,则在xml文件中if判断时,taskIds.size != 0
    List<Integer> taskIds = new ArrayList<>();
    taskIds.add(9);
    taskIds.add(12);

    Map<String, Object> params = new HashMap<>();
    String orderName = "start_time";
    params.put("taskIds",taskIds);
    params.put("orderName", orderName);
    //设置分页,startRow可以为0,pageSize不能为0
    params.put("startRow", 0);
    params.put("pageSize",1);

    List<TestResult> results = testResultDao.getResultsMapParmas(params);
    for(TestResult result : results){
        System.out.println("resultId:" + result.getResultId() + " ** startTime:" + result.getStartTime());
    }
}

由上面的映射文件与单元测试方法可以看出, 如果Map中value为数组或者链表,那么在foreach标签中的collection值必须与它的key值一样,否则会报错。当value为数组时,在if标签中判断时为taskIds.length != 0,当value为链表时,则在if标签中判断时为taskIds.size != 0
如果使用mybatis-generator自动生成dao,则可以对某张表配置enableSelectByExample=”true”,然后使用Example来传入List参数,并添加其他条件。

4.Order by排序
在使用order by对查询结果进行排序时,要使用${}把列名括起来 ORDER BY ${orderName} ,否则排序无效。原因可以参考mybatis中的#和$的区别。其实我们在进行单元测试的时候,将日志输出级别调为DEBUG,运行测试,然后在控制台查看SQL语句,就可以看到,当使用${}时,为ORDER BY start_time DESC,当使用#{}时,为OEDER BY ? DESC。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值