淺談Mybatis之參數(shù)傳遞的幾種姿勢(shì)
在mybatis的日常開發(fā)中,mapper接口中定義的參數(shù)如何與xml中的參數(shù)進(jìn)行映射呢?除了我們常用的@Param注解之外,其他的方式是怎樣的呢?
I. 環(huán)境配置
我們使用SpringBoot + Mybatis + MySql來(lái)搭建實(shí)例demo
- springboot: 2.2.0.RELEASE
- mysql: 5.7.22
1. 項(xiàng)目配置
<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倉(cāng)庫(kù)中,找最新的
另外一個(gè)不可獲取的就是db配置信息,appliaction.yml
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. 數(shù)據(jù)庫(kù)表
用于測(cè)試的數(shù)據(jù)庫(kù)
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 '創(chuàng)建時(shí)間', `update_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時(shí)間', PRIMARY KEY (`id`), KEY `name` (`name`) ) ENGINE=InnoDB AUTO_INCREMENT=551 DEFAULT CHARSET=utf8mb4;
II. 參數(shù)傳遞
接下來(lái)我們看一下Mapper接口中的參數(shù)與xml文件中的參數(shù)映射的幾種姿勢(shì);關(guān)于mybatis項(xiàng)目的搭建,這里就略過(guò),重點(diǎn)信息有下面幾個(gè)
數(shù)據(jù)庫(kù)實(shí)體對(duì)象
@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接口
@Mapper public interface MoneyMapper { }
xml文件,在資源文件夾下,目錄層級(jí)與mapper接口的包路徑完全一致(遵循默認(rèn)的Mapper接口與xml文件綁定關(guān)系,詳情查看SpringBoot系列Mybatis之Mapper接口與Sql綁定幾種姿勢(shì))
<?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注解
在接口的參數(shù)上添加@Param注解,在內(nèi)部指定傳遞給xml的參數(shù)名
一個(gè)簡(jiǎn)單的case如下
int addMoney(@Param("id") int id, @Param("money") int money);
重點(diǎn)關(guān)注上面的參數(shù)
通過(guò)@Param來(lái)指定傳遞給xml時(shí)的參數(shù)名
對(duì)應(yīng)的xml文件中的sql如下,使用#{}來(lái)實(shí)現(xiàn)參數(shù)綁定
<update id="addMoney" parameterType="java.util.Map"> update money set money=money+#{money} where id=#{id} </update>
2. 單參數(shù)
接下來(lái)我們看一下不使用@Param注解時(shí),默認(rèn)場(chǎng)景下,xml中應(yīng)該如何指定參數(shù);因?yàn)閱螀?shù)與多參數(shù)的實(shí)際結(jié)果不一致,這里分開進(jìn)行說(shuō)明
單參數(shù)場(chǎng)景下,xml中的參數(shù)名,可以用任意值來(lái)表明
mapper接口定義如下
/** * 單個(gè)參數(shù)時(shí),默認(rèn)可以直接通過(guò)參數(shù)名來(lái)表示,實(shí)際上#{}中用任意一個(gè)值都可以,沒(méi)有任何限制,都表示的是這個(gè)唯一的參數(shù) * @param id * @return */ MoneyPo findById(int id); /** * 演示xml中的 #{} 為一個(gè)匹配補(bǔ)上的字符串,也可以正確的實(shí)現(xiàn)參數(shù)替換 * @param id * @return */ MoneyPo findByIdV2(int id);
對(duì)應(yīng)的xml文件內(nèi)容如下
<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>
重點(diǎn)看一下上面的findByIdV2,上面的sql中傳參使用的是 #{dd},和mapper接口中的參數(shù)名并不相同,但是最終的結(jié)果卻沒(méi)有什么區(qū)別
3. 多參數(shù)
當(dāng)參數(shù)個(gè)數(shù)超過(guò)1個(gè)的時(shí)候,#{}中的參數(shù),有兩種方式
- param1…N: 其中n代表的接口中的第幾個(gè)參數(shù)
- arg0…N
/** * 不指定參數(shù)名時(shí),mybatis自動(dòng)封裝一個(gè) param1 ... paramN的Map,其中n表示第n個(gè)參數(shù) * 也可以使用 arg0...n 來(lái)指代具體的參數(shù) * * @param name * @param money * @return */ List<MoneyPo> findByNameAndMoney(String name, Integer money);
對(duì)應(yīng)的xml如下
<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中,兩種傳參都是可以的,當(dāng)然不建議使用這種默認(rèn)的方式來(lái)傳參,因?yàn)榉浅2恢庇^,對(duì)于后續(xù)的維護(hù)很不優(yōu)雅
3. Map傳參
如果參數(shù)類型并不是簡(jiǎn)單類型,當(dāng)時(shí)Map類型時(shí),在xml文件中的參數(shù),可以直接使用map中對(duì)應(yīng)的key來(lái)指代
/** * 參數(shù)類型為map時(shí),直接使用key即可 * @param map * @return */ List<MoneyPo> findByMap(Map<String, Object> map);
對(duì)應(yīng)的xml如下
<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對(duì)象
另外一種常見(jiàn)的case是傳參為簡(jiǎn)單的實(shí)體對(duì)象,這個(gè)時(shí)候xml中的參數(shù)也可以直接使用對(duì)象的fieldName來(lái)指代,和map的使用方式差不多
/** * 參數(shù)類型為java對(duì)象,同樣直接使用field name即可 * @param po * @return */ List<MoneyPo> findByPo(MoneyPo po);
對(duì)應(yīng)的xml文件如下
<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. 簡(jiǎn)單參數(shù) + Map參數(shù)
當(dāng)參數(shù)有多個(gè),其中部分為簡(jiǎn)單類型,部分為Map,這樣的場(chǎng)景下參數(shù)如何處理呢?
- 簡(jiǎn)單類型遵循上面的規(guī)則
- map參數(shù)的傳參,使用前綴 + “.” + key的方式
一個(gè)實(shí)例如下
List<MoneyPo> findByIdOrCondition(@Param("id") int id, @Param("map") Map<String, Object> map); List<MoneyPo> findByIdOrConditionV2(int id, Map<String, Object> map);
對(duì)應(yīng)的xml如下
<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.小結(jié)
本文主要介紹mybatis中傳參的幾種姿勢(shì):
- 默認(rèn)場(chǎng)景下,單參數(shù)時(shí),xml文件中可以用任意名稱代替?zhèn)鲄?/li>
- 默認(rèn)場(chǎng)景下,多參數(shù)時(shí),第一個(gè)參數(shù)可用 param1 或 arg0來(lái)表示,第二個(gè)參數(shù)為 param2 或 arg1。。。
- 單參數(shù),且為map時(shí),可以直接使用map的key作為傳參
- 單參數(shù),pojo對(duì)象時(shí),使用對(duì)象的fieldName來(lái)表示傳參
- @Param注解中定義的值,表示這個(gè)參數(shù)與xml中的占位映射關(guān)聯(lián)
- 多參數(shù)場(chǎng)景下,簡(jiǎn)單對(duì)象 + map/pojo時(shí),對(duì)于map/pojo中的參數(shù)占位,可以通過(guò) paramN.xxx 的方式來(lái)完成
III. 不能錯(cuò)過(guò)的源碼和相關(guān)知識(shí)點(diǎn)
項(xiàng)目
工程:https://github.com/liuyueyi/spring-boot-demo
源碼:https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/103-mybatis-xml
到此這篇關(guān)于淺談Mybatis之參數(shù)傳遞的幾種姿勢(shì)的文章就介紹到這了,更多相關(guān)Mybatis 參數(shù)傳遞 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在啟動(dòng)后臺(tái) jar包時(shí),使用指定的 application.yml操作
這篇文章主要介紹了在啟動(dòng)后臺(tái) jar包時(shí),使用指定的 application.yml操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10深入理解SpringCloud之Eureka注冊(cè)過(guò)程分析
eureka是一種去中心化的服務(wù)治理應(yīng)用,其顯著特點(diǎn)是既可以作為服務(wù)端又可以作為服務(wù)向自己配置的地址進(jìn)行注冊(cè),這篇文章主要介紹了深入理解SpringCloud之Eureka注冊(cè)過(guò)程分析2018-05-05解決執(zhí)行maven命令時(shí)提示Process terminated的問(wèn)題
這篇文章主要介紹了解決執(zhí)行maven命令時(shí)提示Process terminated的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09Java多線程之synchronized關(guān)鍵字的使用
這篇文章主要介紹了Java多線程之synchronized關(guān)鍵字的使用,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04