在mybatis的日常開發中,mapper接口中定義的參數如何與xml中的參數進行映射呢?除了我們常用的@Param注解之外,其他的方式是怎樣的呢?
I. 環境配置
我們使用SpringBoot + Mybatis + MySql來搭建實例demo
- springboot: 2.2.0.RELEASE
- mysql: 5.7.22
1. 項目配置
1
2
3
4
5
6
7
8
9
10
11
|
< dependencies > < dependency > < groupId >org.mybatis.spring.boot</ groupId > < artifactId >mybatis-spring-boot-starter</ artifactId > < version >2.2.0</ version > </ dependency > < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > </ dependency > </ dependencies > |
核心的依賴mybatis-spring-boot-starter,至于版本選擇,到mvn倉庫中,找最新的
另外一個不可獲取的就是db配置信息,appliaction.yml
1
2
3
4
5
|
spring: datasource: url: jdbc:mysql://127.0.0.1:3306/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: |
2. 數據庫表
用于測試的數據庫
1
2
3
4
5
6
7
8
9
10
|
CREATE TABLE `money` ( `id` int (11) unsigned NOT NULL AUTO_INCREMENT, ` name ` varchar (20) NOT NULL DEFAULT '' COMMENT '用戶名' , `money` int (26) NOT NULL DEFAULT '0' COMMENT '錢' , `is_deleted` tinyint(1) NOT NULL DEFAULT '0' , `create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創建時間' , `update_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時間' , PRIMARY KEY (`id`), KEY ` name ` (` name `) ) ENGINE=InnoDB AUTO_INCREMENT=551 DEFAULT CHARSET=utf8mb4; |
II. 參數傳遞
接下來我們看一下Mapper接口中的參數與xml文件中的參數映射的幾種姿勢;關于mybatis項目的搭建,這里就略過,重點信息有下面幾個
數據庫實體對象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Data public class MoneyPo { private Integer id; private String name; private Long money; private Integer isDeleted; private Timestamp createAt; private Timestamp updateAt; private Integer cnt; } |
mapper接口
1
2
3
|
@Mapper public interface MoneyMapper { } |
xml文件,在資源文件夾下,目錄層級與mapper接口的包路徑完全一致(遵循默認的Mapper接口與xml文件綁定關系,詳情查看SpringBoot系列Mybatis之Mapper接口與Sql綁定幾種姿勢)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<? 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.git.hui.boot.mybatis.mapper.MoneyMapper" > < resultMap id = "BaseResultMap" type = "com.git.hui.boot.mybatis.entity.MoneyPo" > < id column = "id" property = "id" jdbcType = "INTEGER" /> < result column = "name" property = "name" jdbcType = "VARCHAR" /> < result column = "money" property = "money" jdbcType = "INTEGER" /> < result column = "is_deleted" property = "isDeleted" jdbcType = "TINYINT" /> < result column = "create_at" property = "createAt" jdbcType = "TIMESTAMP" /> < result column = "update_at" property = "updateAt" jdbcType = "TIMESTAMP" /> </ resultMap > < sql id = "money_po" > id, name, money, is_deleted, create_at, update_at </ sql > </ mapper > |
1. @Param注解
在接口的參數上添加@Param注解,在內部指定傳遞給xml的參數名
一個簡單的case如下
1
|
int addMoney( @Param ( "id" ) int id, @Param ( "money" ) int money); |
重點關注上面的參數
通過@Param來指定傳遞給xml時的參數名
對應的xml文件中的sql如下,使用#{}來實現參數綁定
1
2
3
|
< update id = "addMoney" parameterType = "java.util.Map" > update money set money=money+#{money} where id=#{id} </ update > |
2. 單參數
接下來我們看一下不使用@Param注解時,默認場景下,xml中應該如何指定參數;因為單參數與多參數的實際結果不一致,這里分開進行說明
單參數場景下,xml中的參數名,可以用任意值來表明
mapper接口定義如下
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** * 單個參數時,默認可以直接通過參數名來表示,實際上#{}中用任意一個值都可以,沒有任何限制,都表示的是這個唯一的參數 * @param id * @return */ MoneyPo findById( int id); /** * 演示xml中的 #{} 為一個匹配補上的字符串,也可以正確的實現參數替換 * @param id * @return */ MoneyPo findByIdV2( int id); |
對應的xml文件內容如下
1
2
3
4
5
6
7
8
9
10
11
|
< select id = "findById" parameterType = "java.lang.Integer" resultMap = "BaseResultMap" > select < include refid = "money_po" /> from money where id=#{id} </ select > < select id = "findByIdV2" parameterType = "java.lang.Integer" resultMap = "BaseResultMap" > select < include refid = "money_po" /> from money where id=#{dd} </ select > |
重點看一下上面的findByIdV2,上面的sql中傳參使用的是 #{dd},和mapper接口中的參數名并不相同,但是最終的結果卻沒有什么區別
3. 多參數
當參數個數超過1個的時候,#{}中的參數,有兩種方式
- param1…N: 其中n代表的接口中的第幾個參數
- arg0…N
1
2
3
4
5
6
7
8
9
|
/** * 不指定參數名時,mybatis自動封裝一個 param1 ... paramN的Map,其中n表示第n個參數 * 也可以使用 arg0...n 來指代具體的參數 * * @param name * @param money * @return */ List<MoneyPo> findByNameAndMoney(String name, Integer money); |
對應的xml如下
1
2
3
4
5
6
|
< select id = "findByNameAndMoney" resultMap = "BaseResultMap" > select < include refid = "money_po" /> -- from money where name=#{param1} and money=#{param2} from money where name=#{arg0} and money=#{arg1} </ select > |
注意上面的xml中,兩種傳參都是可以的,當然不建議使用這種默認的方式來傳參,因為非常不直觀,對于后續的維護很不優雅
3. Map傳參
如果參數類型并不是簡單類型,當時Map類型時,在xml文件中的參數,可以直接使用map中對應的key來指代
1
2
3
4
5
6
|
/** * 參數類型為map時,直接使用key即可 * @param map * @return */ List<MoneyPo> findByMap(Map<String, Object> map); |
對應的xml如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< select id = "findByMap" resultMap = "BaseResultMap" > select < include refid = "money_po" /> from money < trim prefix = "WHERE" prefixOverrides = "AND | OR" > < if test = "id != null" > id = #{id} </ if > < if test = "name != null" > AND name=#{name} </ if > < if test = "money != null" > AND money=#{money} </ if > </ trim > </ select > |
4. POJO對象
另外一種常見的case是傳參為簡單的實體對象,這個時候xml中的參數也可以直接使用對象的fieldName來指代,和map的使用方式差不多
1
2
3
4
5
6
|
/** * 參數類型為java對象,同樣直接使用field name即可 * @param po * @return */ List<MoneyPo> findByPo(MoneyPo po); |
對應的xml文件如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< select id = "findByPo" parameterType = "com.git.hui.boot.mybatis.entity.MoneyPo" resultMap = "BaseResultMap" > select < include refid = "money_po" /> from money < trim prefix = "WHERE" prefixOverrides = "AND | OR" > < if test = "id != null" > id = #{id} </ if > < if test = "name != null" > AND name=#{name} </ if > < if test = "money != null" > AND money=#{money} </ if > </ trim > </ select > |
5. 簡單參數 + Map參數
當參數有多個,其中部分為簡單類型,部分為Map,這樣的場景下參數如何處理呢?
- 簡單類型遵循上面的規則
- map參數的傳參,使用前綴 + “.” + key的方式
一個實例如下
1
2
3
|
List<MoneyPo> findByIdOrCondition( @Param ( "id" ) int id, @Param ( "map" ) Map<String, Object> map); List<MoneyPo> findByIdOrConditionV2( int id, Map<String, Object> map); |
對應的xml如下
1
2
3
4
5
6
7
|
< select id = "findByIdOrCondition" resultMap = "BaseResultMap" > select < include refid = "money_po" /> from money where id = #{id} or `name`=#{map.name} </ select > < select id = "findByIdOrConditionV2" resultMap = "BaseResultMap" > select < include refid = "money_po" /> from money where id = #{param1} or `name`=#{param2.name} </ select > |
6.小結
本文主要介紹mybatis中傳參的幾種姿勢:
- 默認場景下,單參數時,xml文件中可以用任意名稱代替傳參
- 默認場景下,多參數時,第一個參數可用 param1 或 arg0來表示,第二個參數為 param2 或 arg1。。。
- 單參數,且為map時,可以直接使用map的key作為傳參
- 單參數,pojo對象時,使用對象的fieldName來表示傳參
- @Param注解中定義的值,表示這個參數與xml中的占位映射關聯
- 多參數場景下,簡單對象 + map/pojo時,對于map/pojo中的參數占位,可以通過 paramN.xxx 的方式來完成
III. 不能錯過的源碼和相關知識點
項目
工程:https://github.com/liuyueyi/spring-boot-demo
源碼:https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/103-mybatis-xml
到此這篇關于淺談Mybatis之參數傳遞的幾種姿勢的文章就介紹到這了,更多相關Mybatis 參數傳遞 內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://juejin.cn/post/7011863840962904072