mybatis 查询 map 多参数

Posted lamda表达式先驱

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis 查询 map 多参数相关的知识,希望对你有一定的参考价值。

单个查询

 

 

 

 

 返回map

 返回一个大map  主键id作为key value本身作为map

 

 

mybatis中的多条件查询

使用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 查询 map 多参数的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis_多查询条件问题

mybatis中的多条件查询

mybatis05--多条件的查询

MyBatis映射器--多参数传递方式

mybatis 查询一对多 集合中没有值为啥

mybatis怎么一对多查询语句