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、存儲過程以及高級映射的優秀的...
    笨鳥慢飛閱讀 5,622評論 0 4
  • 每個線程都應該有它自己的SqlSession實例。SqlSession的實例不能共享使用,它是線程不安全的 配置文...
    蕊er閱讀 482評論 0 0
  • 官方文檔 簡介 入門 XML配置 XML映射文件 動態SQL Java API SQL語句構建器 日志 一、 JD...
    拾壹北閱讀 3,552評論 0 52
  • 11 MyBatis一級緩存實現# 11.1 什么是一級緩存? 為什么使用一級緩存?## 每當我們使用MyBati...
    七寸知架構閱讀 10,891評論 12 143
  • 前言 Git 的安裝,使用,將代碼托管到GitHub、GitBlit上 下載安裝 訪問網址https://git-...
    碼字與律動閱讀 554評論 0 1