使用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集合 */@Testpublic 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();}
/** * 多条件查询使用索引号 */@Testpublic 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();}
时间: 2024-10-09 07:37:22