MyBatisMyBatis_多查询条件问题

一、多询问条件无法完整接收问题的缓解

   
在其实工作中,表单中所给出的询问条件有时是不能够将其封装成一个对象,即查询格局只可以带领六个参数,而无法带走将这三个参数举行包装的一个对象。对于这些题目,有二种缓解方案:(1)依照Map查询;(2)使用索引号。

二、根据Map查询

1、修改Dao

MyBatis 1MyBatis 2

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

MyBatis 3MyBatis 4

 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

MyBatis 5MyBatis 6

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. 参数的索引号

MyBatis 7MyBatis 8

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

MyBatis 9MyBatis 10

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

MyBatis 11MyBatis 12

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的版本有关。

旁人博客相关的截图

MyBatis 13MyBatis 14MyBatis 15

 

网站地图xml地图