Mybatis動(dòng)態(tài)sql中@Param使用詳解
Mybatis中的@param注解的使用場(chǎng)景
1、方法有多個(gè)參數(shù)
2、方法參數(shù)要取別名
3、XML 中的 SQL 使用了 $
4、動(dòng)態(tài)sql中參數(shù)是非自定義pojo類型
前三種,相信大家都非常熟練,這里不再多說,這里主要說下第四種。
當(dāng)方法的參數(shù)為非自定義pojo類型,且使用了動(dòng)態(tài)sql,那么就需要在參數(shù)前加上@Param注解。
測(cè)試:(此處使用SpringBoot2.0+ 、Mybatis-Plus)
數(shù)據(jù)庫數(shù)據(jù)
代碼
pojo:
/** * @author: HanXu * on 2019/12/18 * Class description: */ @Data @AllArgsConstructor @NoArgsConstructor public class Test { private Integer id; private String name; private Integer age; private Integer num; }
mapper接口:
/** * @author: HanXu * on 2019/12/18 * Class description: */ public interface TestMapper extends BaseMapper<Test> { Test sel(Test t); Test seli(Integer id); Test seli2(@Param("id")Integer id); Test sels(String s); Test sels2(@Param("s")String s); }
對(duì)應(yīng)的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="hx.insist.demo.mapper.TestMapper"> <select id="sel" parameterType="hx.insist.demo.domain.Test" resultType="hx.insist.demo.domain.Test"> select * from test where 1 = 1 <if test="id!=null"> and id = #{id} </if> <if test="name!=null"> and name = #{name} </if> <if test="age!=null"> and age = #{age} </if> <if test="num!=null"> and num = #{num} </if> </select> <select id="seli" parameterType="Integer" resultType="hx.insist.demo.domain.Test"> select * from test where 1 = 1 <if test="id!=null"> and id = #{id} </if> </select> <select id="seli2" parameterType="Integer" resultType="hx.insist.demo.domain.Test"> select * from test where 1 = 1 <if test="id!=null"> and id = #{id} </if> </select> <select id="sels" resultType="hx.insist.demo.domain.Test"> select * from test where 1 = 1 <if test="s!=null"> and name = #{s} </if> </select> <select id="sels2" resultType="hx.insist.demo.domain.Test"> select * from test where 1 = 1 <if test="s!=null"> and name = #{s} </if> </select> </mapper>
service層:
/** * @author: HanXu * on 2019/12/18 * Class description: */ @Service public class TestService { @Resource private TestMapper testMapper; //測(cè)試自定義的存在setter方法的對(duì)象 public void selTest(){ Test t = new Test(); t.setId(1); Test sel = testMapper.sel(t); System.out.println(sel); } //測(cè)試Integer public void seliTest(){ Test seli = testMapper.seli(1); System.out.println(seli); } //測(cè)試String public void selsTst(){ Test aaa = testMapper.sels("aaa"); System.out.println(aaa); } //測(cè)試Integer,且寫了@Param public void seliTest2(){ Test seli = testMapper.seli2(1); System.out.println(seli); } //測(cè)試String,且寫了@Param public void selsTest2(){ Test test = testMapper.sels2("aaa"); System.out.println(test); } }
controller層:
@GetMapping("testT") public ResponseEntity test(){ testService.selTest(); return ResponseEntity.ok().build(); } @GetMapping("testI") public ResponseEntity testi(){ testService.seliTest(); return ResponseEntity.ok().build(); } @GetMapping("testS") public ResponseEntity tests(){ testService.selsTst(); return ResponseEntity.ok().build(); } @GetMapping("testI2") public ResponseEntity testI2(){ testService.seliTest2(); return ResponseEntity.ok().build(); } @GetMapping("testS2") public ResponseEntity testS2(){ testService.selsTest2(); return ResponseEntity.ok().build(); }
測(cè)試結(jié)果
測(cè)試Test seli(Integer id);
測(cè)試Test seli2(@Param(“id”)Integer id);
測(cè)試Test sels(String s);
測(cè)試Test sels2(@Param(“s”)String s);
測(cè)試Test sel(Test t);
結(jié)論
上述5個(gè)測(cè)試用例中 分為三對(duì):
Test sel(Test t);//5 Test seli(Integer id);//1 Test seli2(@Param("id")Integer id);//2 Test sels(String s);//3 Test sels2(@Param("s")String s);//4
1和2是一對(duì)測(cè)試,除了2比1多了一個(gè)@Param注解之外其他全部相同,測(cè)試結(jié)果表示:加了注解的能正確執(zhí)行,未加注解的不能正確執(zhí)行。
3和4是一對(duì)測(cè)試,除了4比3多了一個(gè)@Param注解之外其他全部相同,測(cè)試結(jié)果表示:加了注解的能正確執(zhí)行,未加注解的不能正確執(zhí)行。
5和1、3是一對(duì)測(cè)試,不同之處在于5中的參數(shù)是自定義pojo,里面每個(gè)屬性都有setter、getter方法,結(jié)果表示:在不加@Param注解的情況下,自定義pojo作為參數(shù)可以正常執(zhí)行,非自定義pojo(沒有參數(shù)的getter方法)作為參數(shù)不能正常執(zhí)行。
以上基于sql語句中使用了動(dòng)態(tài)sql(存在不確定的語句)
我的猜想:
這跟Mybatis內(nèi)部實(shí)現(xiàn)有關(guān)。當(dāng)你使用動(dòng)態(tài)sql時(shí),#{}去拿你的參數(shù)時(shí),默認(rèn)將參數(shù)當(dāng)成了pojo類型,去取對(duì)應(yīng)屬性的getter方法,而這時(shí)如果你的參數(shù)不是pojo類型,是一個(gè)java定義好的類型,比如參數(shù)為Integer id,String s,使用#{}得到它的值時(shí),Mybatis就會(huì)去直接取相應(yīng)的getter方法,比如反射Integer類,取getId()方法 、反射String類,取getS()方法,但是這些類中并沒有相應(yīng)的方法,那么就會(huì)報(bào)錯(cuò):
org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘id’ in ‘class java.lang.Integer’
org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘s’ in ‘class java.lang.String’
到此這篇關(guān)于Mybatis動(dòng)態(tài)sql中@Param使用詳解的文章就介紹到這了,更多相關(guān)Mybatis動(dòng)態(tài)sql內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis實(shí)現(xiàn)動(dòng)態(tài)SQL的方法
- Mybatis之動(dòng)態(tài)SQL使用小結(jié)(全網(wǎng)最新)
- Mybatis動(dòng)態(tài)Sql標(biāo)簽使用小結(jié)
- MyBatis映射文件中的動(dòng)態(tài)SQL實(shí)例詳解
- 詳解MyBatis特性之動(dòng)態(tài)SQL
- Mybatis使用XML實(shí)現(xiàn)動(dòng)態(tài)sql的示例代碼
- Mybatis中的@Param及動(dòng)態(tài)SQL詳解
- mybatis動(dòng)態(tài)sql之新增與更新方式
- MyBatis中實(shí)現(xiàn)動(dòng)態(tài)SQL標(biāo)簽
相關(guān)文章
Spring Boot 與 Kotlin 上傳文件的示例代碼
這篇文章主要介紹了Spring Boot 與 Kotlin 上傳文件的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01spring boot使用WebClient調(diào)用HTTP服務(wù)代碼示例
這篇文章主要介紹了spring boot使用WebClient調(diào)用HTTP服務(wù)代碼示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12動(dòng)態(tài)代理模擬實(shí)現(xiàn)aop的示例
下面小編就為大家?guī)硪黄獎(jiǎng)討B(tài)代理模擬實(shí)現(xiàn)aop的示例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望對(duì)大家有所幫助2017-11-11Java編程幾個(gè)循環(huán)實(shí)例代碼分享
這篇文章主要介紹了Java編程幾個(gè)循環(huán)實(shí)例代碼分享,多看多練,小編覺得還是挺不錯(cuò)的,這里分享給大家,供需要的朋友參考。2017-10-10Java實(shí)現(xiàn)基于JDBC操作mysql數(shù)據(jù)庫的方法
這篇文章主要介紹了Java實(shí)現(xiàn)基于JDBC操作mysql數(shù)據(jù)庫的方法,結(jié)合實(shí)例形式分析了java使用JDBC實(shí)現(xiàn)針對(duì)mysql數(shù)據(jù)庫的連接、查詢、輸出等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12SpringBoot中整合Minio文件存儲(chǔ)的安裝部署過程
這篇文章主要介紹了SpringBoot整合Minio文件存儲(chǔ)的相關(guān)知識(shí),詳細(xì)介紹了Minio安裝部署過程,需要的朋友可以參考下2022-04-04