小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

mybatis的association和collection的用法

 貪挽懶月 2022-06-20 發(fā)布于廣東

前言:

在項目中,某些實(shí)體類之間肯定有關(guān)鍵關(guān)系,比如一對一,一對多等。在hibernate 中用 one to one和 one to many,而mybatis 中就用 association和 collection。 association: 一對一關(guān)聯(lián)(has one)collection:一對多關(guān)聯(lián)(has many) 注意,只有在做select查詢時才會用到這兩個標(biāo)簽,都有三種用法,且用法類似。

一、association的三種用法:

先看如下代碼(省略set、get方法):

  1. public class User {

  2.    private Integer userId;

  3.    private String userName;

  4.    private Integer age;

  5.    private Card card;//一個人一張身份證,1對1

  6. }

  1. public class Card {

  2.    private Integer cardId;

  3.    private String cardNum;//身份證號

  4.    private String address;//地址

  5. }

  1. public interface UserDao {

  2.    /**

  3.     * 通過userId查詢user信息

  4.     * @param userId

  5.     * @return

  6.     */

  7.    User queryById(int userId);

  8. }

  1.  <select id="queryById" parameterType="int" resultMap="userMap">

  2.         SELECT u.user_name,u.age,c.card_id,c.card_num,c.address

  3.         FROM tb_user u,tb_card c

  4.         WHERE u.card_id=c.card_id

  5.         AND

  6.         u.user_id=#{userId}

  7.     </select>

以上是實(shí)體類、dao層的設(shè)計以及在UserDao.xml中queryById方法的sql語句的編寫,因?yàn)椴徽撚胊ssociation的哪種方式,sql語句都是一樣的寫,不同的只是userMap的寫法,所以這里先給出這段代碼。User詢Card是一對一關(guān)系,在數(shù)據(jù)庫中,tbuser表通過外鍵cardid關(guān)聯(lián)tb_card表。下面分別用association的三種用法來實(shí)現(xiàn)queryById方法。

1、第一種用法:association中使用select

這種方法需要再定義CardDao.java,如下:

  1. public interface CardDao {

  2.    Card queryCardById(int cardId);

  3. }

在CardDao.xml中實(shí)現(xiàn)該方法:

  1.  <select id="queryCardById" parameterType="int" resultType="Card">

  2.         SELECT *

  3.         FROM tb_card

  4.         WHERE card_id=#{cardId}

  5.  </select>

然后再看UserDao.xml中是如何引用這個方法的:

  1. <resultMap type="User" id="userMap">

  2.    <result property="userName" 

  3.         column="user_name"/>

  4.    <result property="age" 

  5.         column="age"/>

  6.   <association property="card" 

  7.         column="card_id" 

  8.         select="com.zhu.ssm.dao.

  9.         CardDao.queryCardById">

  10.   </association>

  11. </resultMap>

在這里直接通過select引用CardDao的queryById方法。個人感覺這種方法比較麻煩,因?yàn)檫€要在CardDao里定義queryCardById方法并且實(shí)現(xiàn)再引用才有用,不過這種方法思路清晰,易于理解。

2、第二種方法,嵌套resultMap

  1. <resultMap type="Card" id="cardMap">

  2.      <id property="cardId" 

  3.            column="card_id"/>

  4.      <result property="cardNum" 

  5.                   column="card_num"/>

  6.      <result property="address" 

  7.                   column="address"/>

  8. </resultMap>

  9. <resultMap type="User" id="userMap">

  10.     <result property="userName" 

  11.                  column="user_name"/>

  12.     <result property="age" 

  13.                  column="age"/>

  14.     <association property="card" 

  15.                             resultMap="cardMap">

  16.     </association>

  17. </resultMap>

第二種方法就是在UserDao.xml中先定義一個Card的resultMap,然后在User的resultMap的association標(biāo)簽中通過resultMap="cardMap"引用。這種方法相比于第一種方法較為簡單。

3、第三種方法:嵌套resultMap簡化版

  1. <resultMap type="User" id="userMap">

  2.   <result property="userName" 

  3.                  column="user_name"/>

  4.   <result property="age" 

  5.                  column="age"/>

  6.   <association 

  7.          property="card"

  8.           column="card_id" 

  9.          javaType="Card">

  10.      <id property="cardId" 

  11.                column="card_id"/>

  12.      <result property="cardNum"

  13.                       column="card_num"/>

  14.      <result property="address" 

  15.                       column="address"/>

  16.    </association>

  17. </resultMap> 

這種方法就把Card的resultMap定義在了association 標(biāo)簽里面,通過javaType來指定是哪個類的resultMap,個人認(rèn)為這種方法最簡單,缺點(diǎn)就是cardMap不能復(fù)用。具體用哪種方法,視情況而定。

二、collection的三種用法:

一個土豪有多個手機(jī),看如下代碼: User實(shí)體類

  1. public class User{

  2.  private Integer userId;

  3.    private String userName;

  4.    private Integer age;

  5.    private List<MobilePhone> mobilePhone;//土豪,多個手機(jī),1對多

  6. }

手機(jī)類

  1. public class MobilePhone {

  2.    private Integer mobilePhoneId;

  3.    private String brand;//品牌

  4.    private double price;//價格

  5.    private User user;//主人

  6. }

dao層

  1. public interface UserDao {

  2.    /**

  3.     * 通過userId查詢user信息

  4.     * @param userId

  5.     * @return

  6.     */

  7.    User queryById(int userId);

  8. }

UserDao.xml中的select查詢語句:

  1. <select id="queryById" parameterType="int" resultMap="userMap">

  2.         SELECT u.user_name,u.age,

  3.                       m.brand,m.price

  4.         FROM tb_user u,tb_mobile_phone m

  5.         WHERE m.user_id=u.user_id

  6.         AND

  7.         u.user_id=#{userId}

  8. </select>

數(shù)據(jù)庫中,tbmobilephone中user_id作為外鍵。那么下面來看resultMap如何定義:

1、第一種方法:用select,跟association 中使用select類似:

先定義 MobilePhoneDao.java

  1. public interface MobilePhoneDao {

  2.    List<MobilePhone> queryMbByUserId(int userId);

  3. }

然后實(shí)現(xiàn)該方法 MobilePhoneDao.xml

  1. <resultMap type="MobilePhone"

  2.                    id="mobilePhoneMap">

  3.     <id property="mobilePhoneId" 

  4.           column="user_id"/>

  5.     <result property="brand" 

  6.                  column="brand"/>

  7.     <result property="price" 

  8.                  column="price"/>

  9.     <association property="user" 

  10.               column="user_id" select=

  11.      "com.zhu.ssm.dao.UserDao.queryById">

  12.     </association>

  13. </resultMap>

  1. <select id="queryMbByUserId" parameterType="int" resultMap="mobilePhoneMap">

  2.         SELECT brand,price

  3.         FROM tb_mobile_phone

  4.         WHERE user_id=#{userId}

  5.     </select>

做好以上準(zhǔn)備工作,那就可以在UserDao.xml中引用了

  1. <resultMap type="User" id="userMap">

  2.         <id property="userId" column="user_id"/>

  3.         <result property="userName" 

  4.                     column="user_name"/>

  5.    <result property="age" 

  6.                     column="age"/>

  7.    <collection property="mobilePhone" 

  8.                column="user_id" 

  9.                select="com.zhu.ssm.dao

  10.       .MobilePhoneDao.queryMbByUserId">

  11.        </collection>

  12. </resultMap> 

這種方法和association的第一種用法幾乎是一樣的不同之處就是mobilePhMap中用到了association ,queryMbByUserId中要使用mobilePhoneMap,而不能直接使用resultType。

2、第二種方法:嵌套resultMap

  1. <resultMap type="MobilePhone" id="mobilephoneMap">

  2.          <id column="mobile_phone_id" property="mobilePhoneId"/>

  3.          <result column="brand" property="brand" />

  4.         <result column="price" property="price" /></resultMap>

  1.     <resultMap type="User" id="userMap">

  2.         <result property="userName" column="user_name"/>

  3.        <result property="age" column="age"/>

  4.        <collection property="mobilePhone" resultMap="mobilephoneMap" >

  5.        </collection>

  6. </resultMap> 

定義好這兩個resultMap,再引用UserMap就行了。

3、第三種方法:嵌套resultMap簡化版

  1. <resultMap type="User" id="userMap">

  2.         <result property="userName" column="user_name"/>

  3.     <result property="age" column="age"/>

  4.     <collection property="mobilePhone"

  5.                  column="user_id" 

  6.                 ofType="MobilePhone">

  7.      <id column="mobile_phone_id" property="mobilePhoneId" />

  8.      <result column="brand" 

  9.                       property="brand" />

  10.      <result column="price"

  11.                       property="price" />

  12.  </collection>

  13. </resultMap>

這種方法需要注意,一定要有ofType,collection 裝的元素類型是啥ofType的值就是啥,這個一定不能少。注意: 所有resultMap中的type、select 標(biāo)簽中的resultType以及association中的javaType,collection中的ofType,這里只寫了類名,是因?yàn)樵趍ybatis-config.xml中配置了typeAliases,否則就要寫該類的全類名。配置如下:

  1. <typeAliases>

  2. <packagename="com.zhu.smm.entity"/>

  3. </typeAliases>

總結(jié):

1、association表示的是has one的關(guān)系,一對一時使用。user has one card,所以在user的resultMap中接收card時應(yīng)該用association; 

2、collection表示的是has many的關(guān)系,一對多時使用。user has many mobilePhone,所以在user的resultMap中接收mobilePhone時應(yīng)該用collection 。

3、都有三種用法,且非常類似,resultMap要復(fù)用建議第二種方法,不需要復(fù)用建議第三種方法。 

4、特別注意表中主鍵字段要有所區(qū)分,不能都寫成id,要寫成userid、cardid,反正要有所區(qū)分,不然查詢的時候會查不到完整的數(shù)據(jù)。

    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約