mybatis中注解與xml配置的對(duì)應(yīng)關(guān)系和對(duì)比分析
注解與xml配置的對(duì)應(yīng)關(guān)系
mybatis中注解就是簡(jiǎn)單不需要寫配置文件,適合簡(jiǎn)單的數(shù)據(jù)處理,理解起來比較容易,不動(dòng)態(tài)生成SQL時(shí)候可以用用。
需要綁定,有些時(shí)候不如配置文件,配置文件擴(kuò)展強(qiáng)。 選擇合適的方式應(yīng)用在合適的場(chǎng)景,注解主要應(yīng)用于sql語(yǔ)句比較簡(jiǎn)單容易理解的情況下可讀性高;生成動(dòng)態(tài)sql時(shí)用xml配置文件要更簡(jiǎn)潔,擴(kuò)展性強(qiáng)
常用的注解和xml的對(duì)應(yīng)關(guān)系
@CacheNamespace
類 <cache>@CacheNamespaceRef
類 <cacheRef>@Results
方法 <resultMap>@Result
方法 <result> <id>@One
方法 <association>@Many
方法 <collection>@select
<select>@Insert
<insert>@Update
<update>@Delete
方法 <delete>@InsertProvider
<insert> 允許創(chuàng)建動(dòng)態(tài)SQL@UpdateProvider
<update> 允許創(chuàng)建動(dòng)態(tài)SQL@DeleteProvider
<delete> 允許創(chuàng)建動(dòng)態(tài)SQL@SelectProvider
<select> 允許創(chuàng)建動(dòng)態(tài)SQL@Param
參數(shù) N/A 如果你的映射器的方法需要多個(gè)參數(shù), 這個(gè)注解可以被應(yīng)用于映射器的方法 參數(shù)來給每個(gè)參數(shù)一個(gè)名字。否則,多 參數(shù)將會(huì)以它們的順序位置來被命名 (不包括任何 RowBounds 參數(shù)) 比如。 #{param1} , #{param2} 等 , 這 是 默 認(rèn) 的 。 使用 @Param(“person”),參數(shù)應(yīng)該被命名為 #{person}。@Options
方法 映射語(yǔ)句的屬性 這個(gè)注解提供訪問交換和配置選項(xiàng)的 寬廣范圍, 它們通常在映射語(yǔ)句上作為 屬性出現(xiàn)。 而不是將每條語(yǔ)句注解變復(fù) 雜,Options 注解提供連貫清晰的方式 來訪問它們
注解樣例和xml配置樣例
舉幾個(gè)比較典型和常用的
一對(duì)一關(guān)聯(lián)查詢
注解方式
@Select("select * from authority") @Results(id="au", value=@Result(column="uid", property="user", one=@One(select="findUserByid", fetchType=FetchType.LAZY))) List<Authority> findAll();
@Select
里面填寫要查詢的主表的sql語(yǔ)句@Results
里面映射一個(gè)id="au"的返回結(jié)果集value=@Result()
表示某一屬性的映射關(guān)系column
為對(duì)應(yīng)從表的外鍵名property
為主表實(shí)體類的從表實(shí)體類屬性名one
表示一對(duì)一映射fetchType=FetchType.LAZY
表示為惰性加載,當(dāng)查詢的結(jié)構(gòu)數(shù)據(jù)需要用到從表的數(shù)據(jù)才會(huì)調(diào)用select中的從表的查詢方法select
為關(guān)聯(lián)查詢的另一個(gè)從表的查詢方法uid
為select里的參數(shù)findUserByid
為mapper中定義的方法
@Select("select * from user where id = #{id}") User findUserByid(int id);
此方法可以在xml中配置也可以在本方法中用注解配置
xml中配置方式
<resultMap type="com.jt.mybatis.entity.Authority" id="au"> <association property="user" column="uid" javaType="com.jt.mybatis.entity.User" select="findByUserId"> </association> </resultMap> <select id="findAll" resultMap="au"> select * from authority </select> <select id="findUserByid" resultType="com.jt.mybatis.entity.User"> select * from user where id= #{id} </select>
測(cè)試方法
@Test public void testA(){ AuthorityMapper mapper = session.getMapper(AuthorityMapper.class); mapper.findAll().get(0).getUser(); }
一對(duì)多關(guān)聯(lián)查詢
xml配置方式
<resultMap type="com.jt.mybatis.entity.User" id="user"> <id column="id" property="id" /> <collection property="authoritieList" column="id" fetchType="lazy" select="findAuthorityByUid"> <id column="id" property="id" /> </collection> </resultMap> <select id="findUserByUserName" resultMap="user"> select * from user where username = #{username} </select> <select id="findAuthorityByUid" resultType="com.jt.mybatis.entity.Authority"> select * from authority where uid = #{uid} </select>
注解方式
@Select("select * from user where username = #{username}") @Results(id="user", value=@Result(column="id", property="authoritieList", many=@Many(fetchType=FetchType.LAZY, select="findAuthorityByUid"))) User findUserByUserName(String username); @Select("select * from authority where uid = #{uid}") List<Authority> findAuthorityByUid(int uid);
many表示一對(duì)多映射
測(cè)試方法
@Test public void testB(){ AuthorityMapper mapper = session.getMapper(AuthorityMapper.class); mapper.findUserByUserName("admin").getAuthoritieList(); }
動(dòng)態(tài)sql
注解方式
@SelectProvider(type=AuthorityProvider.class,method="returnSelectSQL") List<Authority> findByIdAndUid(Authority authority); class AuthorityProvider{ public String returnSelectSQL(Authority authority){ SQL sql = new SQL(){{ SELECT("*"); FROM("authority"); if(authority.getId() != 0){ WHERE("id = " + authority.getId()); } if(authority.getUid() != 0){ WHERE("uid = " + authority.getUid()); } }}; return sql.toString(); } } //用XXXProvider的注解是動(dòng)態(tài)生成sql語(yǔ)句的, //type=AuthorityProvider.class為生成動(dòng)態(tài)語(yǔ)句的具體類 //method="returnSelectSQL"為生成動(dòng)態(tài)語(yǔ)句的方法 //SQL類為動(dòng)態(tài)生成sql的類
測(cè)試方法
@Test public void testC(){ AuthorityMapper mapper = session.getMapper(AuthorityMapper.class); Authority authority = new Authority(); mapper.findByIdAndUid(authority); //執(zhí)行此語(yǔ)句返回的sql語(yǔ)句為DEBUG [main] - ==> Preparing: SELECT * FROM authority authority.setId(1); mapper.findByIdAndUid(authority); //執(zhí)行此語(yǔ)句返回的sql語(yǔ)句為DEBUG [main] - ==> Preparing: SELECT * FROM authority WHERE (id = 1) authority.setUid(2); mapper.findByIdAndUid(authority); //執(zhí)行此語(yǔ)句返回的sql語(yǔ)句為DEBUG [main] - ==> Preparing: SELECT * FROM authority WHERE (id = 1 AND uid = 2) }
mybatis 注解和xml 優(yōu)缺點(diǎn)
xml:
增加了xml文件,修改麻煩,條件不確定(ifelse判斷),容易出錯(cuò),特殊轉(zhuǎn)義字符比如大于小于
注釋:
復(fù)雜sql不好用,搜集sql不方便,管理不方便,修改需重新編譯
#和$區(qū)別:
相同
- 都是對(duì)參數(shù)進(jìn)行標(biāo)記的符號(hào)
- #是預(yù)編譯,防止sql注入
- $ 相當(dāng)于一個(gè)占位符,不能防止sql注入
小知識(shí):
如果字段有關(guān)鍵字,則可以用反單引號(hào)修飾 比如desc-》`desc` 這樣就不會(huì)報(bào)錯(cuò)了
resultType 只有和對(duì)象屬性一樣才能映射成功
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot簡(jiǎn)單實(shí)現(xiàn)文件上傳
這篇文章主要介紹了SpringBoot簡(jiǎn)單實(shí)現(xiàn)文件上傳,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-09-09使用Spring AntPathMatcher的doMatch方法
這篇文章主要介紹了使用Spring AntPathMatcher的doMatch方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09SpringBoot使用Mybatis-Generator配置過程詳解
這篇文章主要介紹了SpringBoot使用Mybatis-Generator配置過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02Java Poi 在Excel中輸出特殊符號(hào)的實(shí)現(xiàn)方法
這篇文章主要介紹了Java Poi 在Excel中輸出特殊符號(hào)的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07java求最大公約數(shù)與最小公倍數(shù)的方法示例
這篇文章主要介紹了java求最大公約數(shù)與最小公倍數(shù)的方法,涉及java數(shù)值運(yùn)算的相關(guān)操作技巧,并附帶分析了eclipse環(huán)境下設(shè)置運(yùn)行輸入?yún)?shù)的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Vue中computed計(jì)算屬性和data數(shù)據(jù)獲取方式
這篇文章主要介紹了Vue中computed計(jì)算屬性和data數(shù)據(jù)獲取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03