3中Mybatis批量查询方式

1、如果是一个参数的时:

 

List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);

TestDAO.getItermByList(list);
//DAO的接口:
//public List<HashMap<String,String>> getItermByList(List<Integer> list)

xml中的sql:
<select id="getItermByList" resultType="java.util.HashMap">
		SELECT id,display_name as name from t_recommendation_info where id in
		<foreach item="item" index="index" collection="list"
				 open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>

2、如果是一个map多个参数的时候:

List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
HashMap<String,Object> parames = new HashMap<String,Object>();

parames.put("mapList",list);
TestDAO.getItermByList(parames);
//DAO接口:
//public List<HashMap<String,String>> getItermByList(HashMap<String,Object> parames);

xml中的sql:
<select id="getItermByList" resultType="java.util.HashMap">
		SELECT id,display_name as name from t_recommendation_info where id in
		<foreach item="item" index="index" collection="mapList"
				 open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>

注意collection给的值

3、如果是一个对象:

List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);

//TestModel  的实现
public class TestModel {

    private List<Integer> searchList;

    public List<Integer> getSearchList() {
        return searchList;
    }

    public void setSearchList(List<Integer> searchList) {
        this.searchList = searchList;
    }
}

TestModel tm = new TestModel();
tm.setSearchList(list);
TestDAO.getItermByList(tm)

//DAO接口:
//public List<HashMap<String,String>> getItermByList(TestModel parames);
//XML中的sql:
<select id="getItermByList" resultType="java.util.HashMap">
		SELECT id,display_name as name from t_recommendation_info where id in
		<foreach item="item" index="index" collection="searchList"
				 open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>

注意collection中的参数和对象的变量是一样的。

你可能感兴趣的:(3中Mybatis批量查询方式)