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

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

res property lang value doc enc sele clas cti

參考資料:

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‘

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