1. 程式人生 > >mybatis中的多條件查詢

mybatis中的多條件查詢

app commit out con turn list 接口 system map集合

使用Map集合和索引號

接口:

/**
* 多條件查詢Map集合
* @param map
* @return
*/
public List<Student> findByManyCondition(Map<String,Object> map);

/**
* 多參數查詢使用索引
* @param name
* @param age
* @return
*/
public List<Student> findStudentByCondition(String name,int age);


xml文件(小配置)

<!--多條件查詢Map集合-->
<select id="findByManyCondition" resultType="student">
SELECT *from Student WHERE name LIKE ‘%‘ #{name } ‘%‘ AND age>#{age}
</select>

<!--多參數查詢使用索引-->
<select id="findStudentByCondition" resultType="student">
SELECT *from Student WHERE name LIKE ‘%‘ #{0} ‘%‘ AND age>#{1}
</select>


測試類

/**
* 多條件查詢Map集合
*/
@Test
public void findByManyCondition(){
SqlSession session = MyBatisUtil.getSession();
IStudentDAO mapper = session.getMapper(IStudentDAO.class);
Map<String,Object> map=new HashMap<String,Object>();
map.put("name","張");
map.put("age",20);
List<Student> list = mapper.findByManyCondition(map);
for (Student item:list){
System.out.println(item.getName());
}
session.commit();
session.close();
}



/**
* 多條件查詢使用索引號
*/
@Test
public void findStudentByCondition(){
SqlSession session = MyBatisUtil.getSession();
IStudentDAO mapper = session.getMapper(IStudentDAO.class);
List<Student> list = mapper.findStudentByCondition("張", 20);
for (Student item:list){
System.out.println(item.getName());
}
session.commit();
session.close();
}



mybatis中的多條件查詢