Mybatis传入参数类型为List作为条件进行查询

表结构:
表名称为constant
需求:

现在想查询type为2、3的所有数据(甚至想查询type为1,2,3,4,5....,100的所有数据)并且返回的值是Map(key为id,value为constant类)

如果采用一个一个传参的方式进行查询肯定是不行的,以下是通过Mybatis提供的foreach标签并配合in进行查询

Mapper:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.jm.dao.ConstantDao">
    <resultMap id="BaseResultMap" type="com.jm.model.Constant">
        <result column="id" property="id" jdbcType="BIGINT" />
        <result column="key" property="key" jdbcType="VARCHAR" />
        <result column="value" property="value" jdbcType="VARCHAR" />
        <result column="type" property="type" jdbcType="INTEGER" />
    </resultMap>

                <!--此处一定要注意属性的名称是resultMap,并且值为“BaseResultMap”这样就可以保证返回的Map的value是Constant的对象 -->
    <select id="loadConstantByType" resultMap="BaseResultMap">
        select * from constant where type in
        <!-- 这里要将collection属性的值标记为list,不然不知道传入的参数是list separator表示分离器 item表示list中的一个元素 -->
        <foreach item="item" index="index" collection="list" open="("
            separator="," close=")">#{item}</foreach>
    </select>

</mapper>
DAO:
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.MapKey;
import org.springframework.stereotype.Repository;

@Repository
public interface ConstantDao {
    
    /**
     * 注释@MapKey表示表中那个字段作为Map的key
     * @return
     */
    @MapKey("id")
    Map<Long,Constant> loadConstantByType(List<Integer> type);
}
POJO实体类:
public class Constant {
    private Long id;
    private String key;
    private String value;
    private Integer type;
    
    public String toString(){
        StringBuffer s = new StringBuffer("Constant:{");
        s.append("id:").append(this.id).append(",");
        s.append("key:").append(this.key).append(",");
        s.append("value:").append(this.value).append(",");
        s.append("type:").append(this.type).append("}");
        return s.toString();
    }
    public Constant() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key == null ? null : key.trim();
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value == null ? null : value.trim();
    }
    public Integer getType() {
        return type;
    }
    public void setType(Integer type) {
        this.type = type;
    }
}

pojo中的实体类最好重写toString方法

junitTest:
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring.xml", "classpath:spring-mybatis.xml" })
public class MubatisMapTest {
    @Autowired
    private ConstantDao constantDao;
    
    @Test
    public void mapTest1() {
        List<Integer> typeList = Arrays.asList(2,3);
        Map<Long,Constant> constantMap = constantDao.loadConstantByType(typeList);
        System.out.println(constantMap);
    }
}
最终测试结果:
Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 11,165评论 0 4
  • 每个线程都应该有它自己的SqlSession实例。SqlSession的实例不能共享使用,它是线程不安全的 配置文...
    蕊er阅读 3,280评论 0 0
  • 官方文档 简介 入门 XML配置 XML映射文件 动态SQL Java API SQL语句构建器 日志 一、 JD...
    拾壹北阅读 8,853评论 0 52
  • 11 MyBatis一级缓存实现# 11.1 什么是一级缓存? 为什么使用一级缓存?## 每当我们使用MyBati...
    七寸知架构阅读 13,702评论 12 143
  • 前言 Git 的安装,使用,将代码托管到GitHub、GitBlit上 下载安装 访问网址https://git-...
    码字与律动阅读 3,529评论 0 1