MyBatis_多查询条件问题

一、多查询条件无法整体接收问题的解决

    在实际工作中,表单中所给出的查询条件有时是无法将其封装成一个对象,即查询方法只能携带多个参数,而不能携带将这多个参数进行封装的一个对象。对于这个问题,有两种解决方案:(1)根据Map查询;(2)使用索引号。

二、根据Map查询

1、修改Dao

1 import java.util.List;
2 import java.util.Map;
3 
4 import com.jmu.bean.Student;
5 
6 public interface IStudentDao {
7     // 根据条件查询问题
8     List<Student> selectStudentsByCondition(Map<String, Object> map);
9 }
com.jmu.dao.IStudentDao

2、修改Test

 1 import java.util.HashMap;
 2 import java.util.List;
 3 import java.util.Map;
 4 
 5 import org.apache.ibatis.session.SqlSession;
 6 import org.apache.log4j.BasicConfigurator;
 7 import org.junit.After;
 8 import org.junit.Before;
 9 import org.junit.Test;
10 
11 import com.jmu.bean.Student;
12 import com.jmu.dao.IStudentDao;
13 import com.jmu.utils.MybatisUtils;
14 
15 public class MyTest {
16     private IStudentDao dao;
17     private SqlSession sqlSession;
18 
19     @Before
20     public void Before() {
21         sqlSession = MybatisUtils.getSqlSession();
22         dao = sqlSession.getMapper(IStudentDao.class);
23         BasicConfigurator.configure();
24     }
25   @After
26   public void after(){
27       if (sqlSession!=null) {
28           sqlSession.commit();
29         
30     }
31       
32   }
33     
34     @Test
35     public void test08() {
36        Student stu = new Student("东东",21,95);
37         Map<String,Object> map=new HashMap<String,Object>();
38         map.put("nameCon", "小");
39         map.put("ageCon", 20);
40         map.put("stu", stu);
41 ;        List<Student> students = dao.selectStudentsByCondition(map);
42         for (Student student : students) {
43             System.out.println(student);
44         }
45 
46     }
47 
48 }
com.jmu.test.MyTest

3、修改map.xml

1 <mapper namespace="com.jmu.dao.IStudentDao">    
2     <select id="selectStudentsByCondition" resultType="Student">
3         select id,name,age,score 
4         from student 
5         where name like '%' #{nameCon} '%' 
6         and age >#{ageCon}   
7         and score >#{stu.score} <!-- 也能放对象的属性 -->
8     </select>
9 </mapper>
mapper.xml

输出:

127 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByCondition  - <==      Total: 1
Student [id=173, name=小明明, score=99.5, age=23]

三、使用索引号

在mapper.xml,#{ }中可以放的内容:

  1. 参数对象的属性
  2. 随意内容,此时的#{ }是个占位符
  3. 参数为map时的key
  4. 参数为map时,若key所对应的value为对象,即可将将对象的属性放入
  5. 参数的索引号
1 <mapper namespace="com.jmu.dao.IStudentDao">    
2     <select id="selectStudentsByCondition" resultType="Student">
3     select id,name,age,score from student where  name like '%' #{0} '%' and age > #{1}
4      
5     </select>
6 </mapper>
mapper.xml
1 import java.util.List;
2 import com.jmu.bean.Student;
3 
4 public interface IStudentDao {
5     // 根据条件查询问题
6     List<Student> selectStudentsByCondition(String name,int i);
7 }
IStudentDao
1 @Test
2     public void test08() {
3       
4 ;        List<Student> students = dao.selectStudentsByCondition("明",20);
5         for (Student student : students) {
6             System.out.println(student);
7         }
8 
9     }
MyTest

输出:

Cause: org.apache.ibatis.binding.BindingException: Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]

 失败,原因好像跟MyBatis的版本有关。

别人博客相关的截图

原文地址:https://www.cnblogs.com/hoje/p/8093463.html