溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

mybatis中怎么配置注解

發(fā)布時(shí)間:2021-08-04 14:10:30 來(lái)源:億速云 閱讀:126 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

mybatis中怎么配置注解,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

注解與xml配置的對(duì)應(yīng)關(guān)系

mybatis中注解就是簡(jiǎn)單不需要寫(xiě)配置文件,適合簡(jiǎn)單的數(shù)據(jù)處理,理解起來(lái)比較容易,不動(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 類(lèi) <cache>

  • @CacheNamespaceRef 類(lèi) <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ù)來(lái)給每個(gè)參數(shù)一個(gè)名字。否則,多 參數(shù)將會(huì)以它們的順序位置來(lái)被命名 (不包括任何 RowBounds 參數(shù)) 比如。 #{param1} , #{param2} 等 , 這 是 默 認(rèn) 的 。 使用 @Param(“person”),參數(shù)應(yīng)該被命名為 #{person}。

  • @Options 方法 映射語(yǔ)句的屬性 這個(gè)注解提供訪問(wèn)交換和配置選項(xiàng)的 寬廣范圍, 它們通常在映射語(yǔ)句上作為 屬性出現(xiàn)。 而不是將每條語(yǔ)句注解變復(fù) 雜,Options 注解提供連貫清晰的方式 來(lái)訪問(wèn)它們

注解樣例和xml配置樣例

舉幾個(gè)比較典型和常用的

一對(duì)一關(guān)聯(lián)查詢(xú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里面填寫(xiě)要查詢(xún)的主表的sql語(yǔ)句

  • @Results里面映射一個(gè)id="au"的返回結(jié)果集

  • value=@Result()表示某一屬性的映射關(guān)系

  • column為對(duì)應(yīng)從表的外鍵名

  • property為主表實(shí)體類(lèi)的從表實(shí)體類(lèi)屬性名

  • one表示一對(duì)一映射

  • fetchType=FetchType.LAZY表示為惰性加載,當(dāng)查詢(xún)的結(jié)構(gòu)數(shù)據(jù)需要用到從表的數(shù)據(jù)才會(huì)調(diào)用select中的從表的查詢(xún)方法

  • select為關(guān)聯(lián)查詢(xún)的另一個(gè)從表的查詢(xún)方法

  • 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)查詢(xú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ǔ)句的具體類(lèi)
 //method="returnSelectSQL"為生成動(dòng)態(tài)語(yǔ)句的方法
 //SQL類(lèi)為動(dòng)態(tài)生成sql的類(lèi)

測(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)義字符比如大于小于

注釋?zhuān)?/h4>

  復(fù)雜sql不好用,搜集sql不方便,管理不方便,修改需重新編譯

#和$區(qū)別:

相同

  • 都是對(duì)參數(shù)進(jìn)行標(biāo)記的符號(hào)

  • #是預(yù)編譯,防止sql注入

  • $ 相當(dāng)于一個(gè)占位符,不能防止sql注入

看完上述內(nèi)容,你們掌握mybatis中怎么配置注解的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI