Mybatis雜記(一)

Mybatis雜記:

當數據庫涉及到一對一時Mybatis的處理方法:

  • 例如在bean類中定義如下兩個類(Hus和Wife類),在國內還是一夫一妻制的吧,2333(逃。。。)
    然後需要做的就是在Hus和Wife類中各自寫上另一方的引用!
public class Hus {
    private long id;
    private String name;
    private int age;
    private Wife wife;
	//Getter and Setter
	//toString
	//Constuctor
}
public class Wife {
    private long id;
    private String name;
    private int age;
    private Hus hus;
	//Getter and Setter
	//toString
	//Constuctor
}
  • Mapper類中定義如下方法:
List<Hus> findHus_Wife();
  • 重點就落在了XXXMapper.XML文件的編寫上:

有5種編寫的方式:

  1. 定義select標籤,然後寫好SQL語句:
<select id="findHus_Wifes" resultMap="hus_wife_model">
    select a.ID,a.NAME,a.AGE, b.id ids,b.NAME names,b.AGE ages
    from HUS a,WIFE b
    where a.id=b.hus_id
</select>

注意: Hus表和Wife表具有相同的列名id和name,age,如不採用別名,Mybatis會認爲這裏的參數爲相同,故需要立別名來保證Mybatis查找到Wife表的內容!

定義ResultMap:
注意:其中hus和wife經過別名處理,請在mybatis-config.xml中定義別名!或在type內些上bean類的全限定名尚可。

<resultMap id="hus_wife_model" type="hus">
     <id property="id" column="id"></id>
     <result property="name" column="name"/>
     <result property="age" column="age"/>
     <result property="wife.id" column="ids"/>
     <result property="wife.name" column="names"/>
     <result property="wife.age" column="ages"/>
</resultMap>

idnameage都是查詢到的Hus表中的三個數據,當設計到Wife表時,第一後邊的column屬性要變成上方SQL語句中idsnamesages,第二property屬性內要寫成wife.id形式,wife是Hus類中對Wife引用名,即第四條屬性(Mybatis會自動去搜尋getWife()方法,並將其以去掉get,並取後面字段小寫的方式來獲取屬性名),獲取到對象以後,.後寫的就是Wife內的屬性,取屬性方式同理。


  1. select語句同上,重寫ResultMap:

先寫一個type爲Wife的ResultMap:

<resultMap id="wife_model1" type="wife">
     <id property="id" column="ids"></id>
     <result property="name" column="names"/>
     <result property="age" column="ages"/>
 </resultMap>

再寫type爲Hus的ResultMap:
<association>標籤就可以用來關聯一對一的映射關係,property屬性指向引用變量wife,ResultMap屬性內填寫的是wife的模板,也就是先爲Hus類中第四個屬性wife的引用書寫模板,後在定義Hus的模板過程中再將剛纔定義的wife的模板通過association標籤引入,這樣就達到了一對一關聯的目的!

<resultMap id="hus_model2" type="hus">
    <id property="id" column="id"></id>
     <result property="name" column="name"/>
     <result property="age" column="age"/>
     <association property="wife" resultMap="wife_model1"/>
 </resultMap>

  1. select語句同上,重寫ResultMap
<resultMap id="mod1_hus" type="hus">
    <id property="id" column="id"></id>
    <result property="name" column="name" />
    <result property="age" column="age" />
    <association property="wife" javaType="wife">
        <id property="id" column="ids"/>
        <result property="name" column="names" />
        <result property="age" column="ages"/>
    </association>
</resultMap>

這次不同的是用了同一個ResultMap來定義模板,在association標籤中寫入的是wife內的三個屬性,總的來講和2中使用的方法類似,相當於把兩個模板合二爲一!


  1. select繼續不變化,再次變化resultMap
<resultMap id="mod3_hus" type="hus">
   <id property="id" column="id"></id>
    <result property="name" column="name"/>
    <result property="age" column="age"/>
</resultMap>

<resultMap id="mod3_hus_wife" type="hus" extends="mod3_hus">
    <result property="wife.id" column="ids"/>
    <result property="wife.name" column="names"/>
    <result property="wife.age" column="ages"/>
</resultMap>

先定義一個hus的resultMap,然後再定義一個resultMap繼承它!extends 表示從某個模板中把屬性配置繼承過來,類型一致。這樣mod3_hus_wife中就有了繼承的hus屬性和新增的wife屬性,也達到了我們所要的效果!


  1. 轉變思想:
  • 先編寫一個select,作用是基於 hus_id 在wife表查詢一行記錄
<select id="selectWifeByhus_id"  parameterType="Long" resultType="wife">
    select id,name,AGE
    from  WIFE
    where HUS_ID=#{id}
</select>
  • 然後編寫一個resultMapassociationcolumnselect填的是上面編寫的select和他需要傳入的參數值,相當於取當前的id屬性(即hus的id屬性hus_id)說去上面定義的select中通過hus_Id查詢到對應的wife屬性,然後將其填入<association>的子標籤中!
<resultMap id="mod2_hus" type="hus">
    <id property="id" column="id"></id>
    <result property="name" column="name"/>
    <result property="age" column="age"/>
    <!-- column 表示基於該屬性去查詢 -->
    <association property="wife" column="id" select="selectWifeByhus_id">
        <id property="id" column="id"></id>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
    </association>
</resultMap>

這樣我們的主select的作用就是從Hus中取出對應的值就好了!

<select id="findHus_Wife3" resultMap="mod2_hus">
    select id,name,age
    from HUS
</select>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章