欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis中注解與xml配置的對(duì)應(yīng)關(guān)系和對(duì)比分析

 更新時(shí)間:2021年08月03日 17:08:03   作者:有情有趣  
這篇文章主要介紹了mybatis中注解與xml配置的對(duì)應(yīng)關(guān)系和對(duì)比分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

注解與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)文章

  • Hadoop之常用端口號(hào)解讀

    Hadoop之常用端口號(hào)解讀

    這篇文章主要介紹了Hadoop之常用端口號(hào),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • SpringBoot簡(jiǎn)單實(shí)現(xià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方法

    這篇文章主要介紹了使用Spring AntPathMatcher的doMatch方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringBoot使用Mybatis-Generator配置過程詳解

    SpringBoot使用Mybatis-Generator配置過程詳解

    這篇文章主要介紹了SpringBoot使用Mybatis-Generator配置過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Java 程序員必備的 Intellij IDEA 插件

    Java 程序員必備的 Intellij IDEA 插件

    java插件十分方便實(shí)用,以下是我用過不錯(cuò)的Intellij插件,當(dāng)然還有很多插件也都不錯(cuò),下面將我覺得不錯(cuò)的插件分享給大家,希望能幫到大家
    2018-09-09
  • Java Poi 在Excel中輸出特殊符號(hào)的實(shí)現(xiàn)方法

    Java Poi 在Excel中輸出特殊符號(hào)的實(shí)現(xiàn)方法

    這篇文章主要介紹了Java Poi 在Excel中輸出特殊符號(hào)的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • JAVA中的日期時(shí)間類用法總結(jié)

    JAVA中的日期時(shí)間類用法總結(jié)

    這篇文章主要給大家介紹了關(guān)于JAVA中日期時(shí)間類用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • spring中使用Mockito解決Bean依賴樹問題方法

    spring中使用Mockito解決Bean依賴樹問題方法

    在本篇文章里小編給各位整理了關(guān)于spring中使用Mockito解決Bean依賴樹問題方法,有需要的朋友們可以參考下。
    2020-01-01
  • java求最大公約數(shù)與最小公倍數(shù)的方法示例

    java求最大公約數(shù)與最小公倍數(shù)的方法示例

    這篇文章主要介紹了java求最大公約數(shù)與最小公倍數(shù)的方法,涉及java數(shù)值運(yùn)算的相關(guān)操作技巧,并附帶分析了eclipse環(huán)境下設(shè)置運(yùn)行輸入?yún)?shù)的相關(guān)操作技巧,需要的朋友可以參考下
    2017-11-11
  • Vue中computed計(jì)算屬性和data數(shù)據(jù)獲取方式

    Vue中computed計(jì)算屬性和data數(shù)據(jù)獲取方式

    這篇文章主要介紹了Vue中computed計(jì)算屬性和data數(shù)據(jù)獲取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評(píng)論