1. 程式人生 > >MyBati__mapper 中取值(#{} 或${}) 以及 parameterType為(基本型別 或複雜型別)

MyBati__mapper 中取值(#{} 或${}) 以及 parameterType為(基本型別 或複雜型別)

參考資料:

MyBatis學習筆記(三)——parameterType為基本型別時的使用方法

MyBatis的傳入引數parameterType型別

 

1. MyBatis的傳入引數parameterType型別分兩種

   1.1  基本資料型別:int,string,long,Date;

   1.2  複雜資料型別:類和Map

2. 如何獲取引數值:

   2.1  基本資料型別:#{隨意起個名字}  或  ${_parameter} 或 ${value}   注意這裡的區別

   2.2  複雜資料型別:#{屬性名} 或 #{屬性名} ,map中則是#{key} 或 ${key} 

特殊情況:

order by 後面只能用 ${}取值。

例如,傳入型別為 string ,並且order by,那麼只能寫成以下兩種

1 <select id="selectByAge2" resultType="stu">
2     select * from table1 order by ${value}
3 </select>
4 
5 <select id="selectByAge3"
resultType="stu"> 6 select * from table1 order by ${_parameter} 7 </select>

 

另外,當傳入型別為基本型別,沒有用${_parameter} 或 ${value}這樣取值時,會報類似以下錯誤(根據傳入型別的基本型別的不同有所不同)。

例如:傳入型別為 string,取值寫成  ${id}

1 <select id="selectByAge4" resultType="stu">
2     select * from table1 order by ${id}
3 </select>

報錯:

1 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'class java.lang.String'

 

傳入型別為 string ,並且order by的例項程式碼:

mapper.xml 檔案內容:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.maven.mapper.StudentMapper">

<!-- 需要傳參的sql語句  #{} 和 ${} 取值的區別-->
     
      <select id="selectByAge1"  resultType="stu">
        select * from table1 order by #{id}
    </select>
    
    <select id="selectByAge2" resultType="stu">
        select * from table1 order by ${value}
    </select>
    
      <select id="selectByAge3" resultType="stu">
        select * from table1 order by ${_parameter}
    </select>
    
    <select id="selectByAge4" resultType="stu">
        select * from table1 order by ${id}
    </select>
    
</mapper>

對應測試類中呼叫方法的程式碼:

 1 //<!-- 需要傳參的sql語句  #{} 和 ${} 取值的區別-->
 2         // #{id}  
 3         @Test
 4         public void selectByAge1() {
 5             List<Student> list =  session.selectList("com.qphone.maven.mapper.StudentMapper.selectByAge1","id");
 6             for(Student student:list){
 7                 System.out.println(student);
 8             }    
 9         }
10         // ${value} 
11         @Test
12         public void selectByAge2() {
13             List<Student> list =  session.selectList("com.qphone.maven.mapper.StudentMapper.selectByAge2","id");
14             for(Student student:list){
15                 System.out.println(student);
16             }
17         }
18         // ${_parameter}  
19         @Test
20         public void selectByAge3() {
21             List<Student> list =  session.selectList("com.qphone.maven.mapper.StudentMapper.selectByAge3","id");
22             for(Student student:list){
23                 System.out.println(student);
24             }
25         }
26         // #{id}  
27         @Test
28         public void selectByAge4() {
29             
30             List<Student> list =  session.selectList("com.qphone.maven.mapper.StudentMapper.selectByAge4","id");
31             for(Student student:list){
32                 System.out.println(student);
33             }
34         }
測試方法  selectByAge1  返回查詢結果,但是order by不會起作用
測試方法  selectByAge1  返回查詢結果,order by會起作用
測試方法  selectByAge1  返回查詢結果,order by會起作用
測試方法  selectByAge1  報錯

1 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'class java.lang.String'