6、SpringBoot+Mybatis整合------參數傳遞


開發工具:STS

代碼下載鏈接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/7892801d804d2060774f3720f82e776ff318e3ba

前言:

在調用mybatis的查詢條件時,之前,遇到需要驗證多個參數的查詢時,往往需要把所有參數都綁定到一個實體中去,然后調用獲取。

現在,我們來詳細描述mybatis傳遞參數的細節。


 

一、單個參數:

1.定義mapper接口:

 1 package com.xm.mapper;
 2 
 3 import java.util.List;
 4 
 5 import com.xm.pojo.Student;
 6 
 7 public interface StudentMapper {
 8 
 9 
10     /**
11      * 根據name查詢
12      * @param name
13      * @return
14      */
15     public Student getByName(String name);
16     
17 }
StudentMapper.java

 

2.實現mapper映射:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 <mapper namespace="com.xm.mapper.StudentMapper">
 4 
 5     
 6     
 7     <!-- 根據name查詢 -->
 8     <select id="getByName" resultType="student">
 9     select * from student where name=#{name}
10     </select>
11 </mapper>
StudentMapper.xml

 

3.定義測試用例:

 1 package com.xm;
 2 
 3 import org.junit.Test;
 4 import org.junit.runner.RunWith;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.boot.test.context.SpringBootTest;
 7 import org.springframework.test.context.junit4.SpringRunner;
 8 
 9 import com.xm.mapper.StudentMapper;
10 import com.xm.pojo.Student;
11 
12 @RunWith(SpringRunner.class)
13 @SpringBootTest
14 public class StudentTest {
15     @Autowired
16     private StudentMapper studentMapper;
17     
18     @Test
19     public void selectStudent() {
20         
21         Student student = studentMapper.getByName("郭小明");
22         System.out.println(student);
23         
24     }
25     
26 
27 }
StudentTest.java

 

4.測試結果:

(1)數據庫查詢結果:

(2)測試結果輸出:

 

 注意在mapper映射中,單個參數類型,也可以不寫 parameterType="",參數名可以隨意寫,比如#{names}

實現mapper映射:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 <mapper namespace="com.xm.mapper.StudentMapper">
 4 
 5     
 6     
 7     <!-- 根據name查詢 -->
 8     <select id="getByName" resultType="student">
 9     <!-- select * from student where name=#{name} -->
10     select * from student where name=#{names}
11     </select>
12 </mapper>
StudentMapper.xml

 

 

二、多個參數

1.根據參數名查詢

(1)定義mapper接口:

 1 package com.xm.mapper;
 2 
 3 import java.util.List;
 4 
 5 import com.xm.pojo.Student;
 6 
 7 public interface StudentMapper {
 8 
 9     
10     /**
11      * 根據用戶名和id同時查詢
12      * @param id
13      * @param name
14      * @return
15      */
16     public Student getStudentByIdAndName(Integer id,String name);
17     
18 }
StudentMapper.java

 

(2)實現mapper映射:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 <mapper namespace="com.xm.mapper.StudentMapper">
 4 
 5     
 6     <!-- 根據用戶名和id同時查詢 -->
 7     <select id="getStudentByIdAndName" resultType="student">
 8     select * from student where name=#{name} and id=#{id} 
 9     </select>
10 </mapper>
StudentMapper.xml

 

(3)定義測試用例:

 1 package com.xm;
 2 
 3 import org.junit.Test;
 4 import org.junit.runner.RunWith;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.boot.test.context.SpringBootTest;
 7 import org.springframework.test.context.junit4.SpringRunner;
 8 
 9 import com.xm.mapper.StudentMapper;
10 import com.xm.pojo.Student;
11 
12 @RunWith(SpringRunner.class)
13 @SpringBootTest
14 public class StudentTest {
15     @Autowired
16     private StudentMapper studentMapper;
17     
18     
19     @Test
20     public void selectStudent() {
21         
22         /*Student student = studentMapper.getByName("郭小明");*/
23         Student student = studentMapper.getStudentByIdAndName(1, "郭小明");
24         System.out.println(student);
25         
26     }
27     
28 
29 }
StudentTest.java

 

(4)測試結果:

說明:這種簡單的根據參數名傳參是失敗的

2.根據參數key值獲取,獲取規則為param1,param2,param3.........:

(1)實現mapper映射:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 <mapper namespace="com.xm.mapper.StudentMapper">
 4 
 5 
 6     <!-- 根據用戶名和id同時查詢 -->
 7     <select id="getStudentByIdAndName" resultType="student">
 8     <!-- select * from student where name=#{name} and id=#{id}  -->
 9     select * from student where name=#{param2} and id=#{param1} 
10     </select>
11 </mapper>
StudentMapper.xml

 

(2)測試結果:

說明:針對這種情況,如果參數較多的情況下,獲取准確的參數名更好一些

3.綁定參數名

(1)定義mapper接口:

 1 package com.xm.mapper;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.ibatis.annotations.Param;
 6 
 7 import com.xm.pojo.Student;
 8 
 9 public interface StudentMapper {
10 
11     
12     
13     /**
14      * 根據用戶名和id同時查詢
15      * @param id
16      * @param name
17      * @return
18      */
19     public Student getStudentByIdAndName(@Param("id")Integer id,@Param("name")String name);
20     
21 }
StudentMapper.java

 

(2)實現mapper映射:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 <mapper namespace="com.xm.mapper.StudentMapper">
 4 
 5 
 6     <!-- 根據用戶名和id同時查詢 -->
 7     <select id="getStudentByIdAndName" resultType="student">
 8     select * from student where name=#{name} and id=#{id} 
 9     <!-- select * from student where name=#{param2} and id=#{param1}  -->
10     </select>
11 </mapper>
StudentMapper.xml

 

(3)測試結果:

說明:針對參數較多,且參數都屬於同一個pojo類的時候,可以把參數先封裝入實體,再獲取

4.封裝實體參數:

(1)定義mapper接口:

 1 package com.xm.mapper;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.ibatis.annotations.Param;
 6 
 7 import com.xm.pojo.Student;
 8 
 9 public interface StudentMapper {
10 
11     
12     
13     /**
14      * 根據用戶名和id同時查詢
15      * @param id
16      * @param name
17      * @return
18      */
19     public Student getStudentByIdAndName(Student student);
20     
21 }
StudentMapper.java

 

(2)定義測試用例:

 1 package com.xm;
 2 
 3 import org.junit.Test;
 4 import org.junit.runner.RunWith;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.boot.test.context.SpringBootTest;
 7 import org.springframework.test.context.junit4.SpringRunner;
 8 
 9 import com.xm.mapper.StudentMapper;
10 import com.xm.pojo.Student;
11 
12 @RunWith(SpringRunner.class)
13 @SpringBootTest
14 public class StudentTest {
15     @Autowired
16     private StudentMapper studentMapper;
17     
18     @Test
19     public void selectStudent() {
20         
21         /*Student student = studentMapper.getByName("郭小明");*/
22         /*Student student = studentMapper.getStudentByIdAndName(1, "郭小明");*/
23         Student student = new Student();
24         student.setName("郭小明");
25         student.setId(1);
26         Student student2 = studentMapper.getStudentByIdAndName(student);
27         System.out.println(student2);
28         
29     }
30     
31 
32 }
StudentMapper.java

 

(3)測試結果:


 

                                                                                 2018-07-02


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM