背景
假設我們要保存一個小狗的信息到數據庫中
通常的做法
我們在使用mybatis 接口和xml對應的時候,常常是這樣寫的:
接口
1
2
3
|
public interface DogDao { void save( @Param ( "dogName" ) String dogName, @Param ( "age" ) int age); } |
xml
1
2
3
4
|
< insert id = "save" > INSERT INTO dog values (null, #{dogName}, #{age}); </ insert > |
因為mybatis在執行的之后動態生成實現類,而在java中使用反射會將方法中的參數名稱擦除,所以如果在xml中要接收到規定參數名稱的參數,通過注解標識是簡單又可行的方法,一方面可以自定義方法參數的綁定,另外一方面在xml中也可以起有意義的名稱與@Param中的value對應。
如果不加@Param注解,也想用xml接收響應的參數
1
2
3
|
public interface DogDao { void save(Integer id, String dogName, int age); } |
由于在反射和jdk動態代理會將方法名稱抹除,所以在解析參數的時候不能像上面的xml#{dogName}這樣接收,會將參數的名稱序列化成以下方式,param1…paramn
所以在xml中我們要用以下方式來接收:
1
2
3
4
|
< insert id = "save" > INSERT INTO dog values (null, #{param2}, #{param2}); </ insert > |
不想用@Param修飾,但是希望在xml中用方法中的原參數名稱接收
我們在jdk不能使用反射獲取參數名稱,但是在jdk1.8之后提供了Parameter這個反射類,可以配置-parameter這個參數到javac編譯器上,可以用來獲取方法參數上的名稱,但是javac編譯器默認是關閉的,所以我們在編譯代碼的時候應該打開它,我們用maven插件的方式開啟
在:pom文件中加入以下插件:
1
2
3
4
5
6
7
8
9
|
< plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-compiler-plugin</ artifactId > < version >3.8.0</ version > < configuration > < compilerArgument >-parameters</ compilerArgument > < encoding >UTF-8</ encoding > </ configuration > </ plugin > |
這樣,javac編譯器就能將方法的參數在反射編譯的時候一同傳遞了,我們也不用借助注解就能達到在xml中獲取方法中的實際參數的名稱,除了加入編譯插件,還要加入mybatis的配置開啟:
1
2
3
|
< settings > < setting name = "useActualParamName" value = "false" /> </ settings > |
官方文檔是這樣對這個配置說明的:
允許使用方法簽名中的名稱作為語句參數名稱。 為了使用該特性,你的項目必須采用 Java 8 編譯,并且加上 -parameters 選項。(新增于 3.4.1)
java 代碼:
1
2
3
|
public interface DogDao { void save(Integer id, String dogName, int age); } |
xml代碼:
1
2
3
4
|
< insert id = "save" > INSERT INTO dog values (null, #{dogName}, #{age}); </ insert > |
補充:mybatis傳多個參數(不使用@param注解情況和使用@param注解的情況)
方法1:順序傳參法
1.不使用@param注解傳遞多個參數的情況
注: 使用jdk1.7得到的是: [1, 0, param1, param2]
使用1.8得到的則是: [arg1, arg0, param1, param2]
#{}里面的數字代表你傳入參數的順序。
這種方法不建議使用,sql層表達不直觀,且一旦順序調整容易出錯。
舉個栗子:
Dao層
1
|
List<User> demo( int userid, String name); |
對應的XML編寫
jdk1.7
1
2
3
4
|
< select id = "demo" resultMap = "User" > select * from user where user_id=#{0} and name= #{1} </ select > |
jdk1.8之后
第一種寫法
1
2
3
4
|
< select id = "demo" resultMap = "User" > select * from user where user_id=#{arg0} and name= #{arg1} </ select > |
第二種寫法
1
2
3
4
|
< select id = "demo" resultMap = "User" > select * from user where user_id=#{param0} and name= #{param1} </ select > |
方法2:@Param注解傳參法
1
2
3
4
5
|
public User selectUser( @Param ( "userName" ) String name, int @Param ( "deptId" ) deptId); <select id= "selectUser" resultMap= "UserResultMap" > select * from user where user_name = #{userName} and dept_id = #{deptId} </select> |
#{}里面的名稱對應的是注解@Param括號里面修飾的名稱。
這種方法在參數不多的情況還是比較直觀的,推薦使用。
方法3:Map傳參法
1
2
3
4
5
|
public User selectUser(Map<String, Object> params); <select id= "selectUser" parameterType= "java.util.Map" resultMap= "UserResultMap" > select * from user where user_name = #{userName} and dept_id = #{deptId} </select> |
#{}里面的名稱對應的是Map里面的key名稱。
這種方法適合傳遞多個參數,且參數易變能靈活傳遞的情況。
方法4:Java Bean傳參法
1
2
3
4
5
|
public User selectUser(User user); <select id= "selectUser" parameterType= "com.test.User" resultMap= "UserResultMap" > select * from user where user_name = #{userName} and dept_id = #{deptId} </select> |
#{}里面的名稱對應的是User類里面的成員屬性。
這種方法很直觀,但需要建一個實體類,擴展不容易,需要加屬性,看情況使用。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。如有錯誤或未考慮完全的地方,望不吝賜教。
原文鏈接:https://blog.csdn.net/tangyaya8/article/details/90300554