Mybatis 基礎xml對映

秦丨竹發表於2020-10-22

第一個簡單的查詢過程

這個介紹 ID result的區別

https://www.cnblogs.com/hamhog/p/3959451.html

<result column="資料庫欄位名" property="實體類屬性" jdbcType="資料庫欄位型別" />

public class company {
    private int id;
    private String name;
    private int addre;
    private Addre addrep;
public class Addre {
    private int id;
    private String place;
    private List<company> company;

結果:

 <resultMap id="assocResult" type="testdo.company">
        <result column="r_id" property="addrep.id"/>
        <result column="r_name" property="addrep.place"/>
    </resultMap>
    <select id="selectDepartWithRegion" resultMap="assocResult">
		select d.id,d.name,r.id r_id,r.name r_name
		from s_dept d,s_region r
		where d.region_id = r.id
	</select>

 使用有參構造器來呼叫語句:

<constructor>
    <idArg column="id" javaType="_int"/>
    <arg column="name" javaType="string"/>
</constructor>

association是用於一對一和多對一,而collection是用於一對多的關係

第一種 Association

<resultMap id="assocResult" type="testdo.company"  >
    <result column="id" property="id"/>
    <result column="name" property="name"/>
    <association property="addrep" javaType="testdo.Addre">
        <id column="r_id" property="id"/>
        <result property="place" column="r_name"/>
    </association>
</resultMap>

在association裡可以用構造器來代替ID和result

 <association property="addrep" javaType="testdo.Addre">
    <constructor>
        <idArg column="r_id" javaType="_int"/>
        <arg column="r_name" javaType="string"/>
    </constructor>
<!--     <id column="r_id" property="id"/>
     <result property="place" column="r_name"/>-->

 </association>

兩者效果一樣

也可以在建一個resultMap

<resultMap type="testdo.Addre" id="regionMap">
    <id column="id" property="id"/>
    <result column="name" property="place"/>
</resultMap>

然後在原來的resultMap里加

<association property="addrep" resultMap="regionMap"/>

也可以達到同樣的效果

更簡單一種方法:

類似封裝公共的部分 然後呼叫它:

這是公共的部分

<resultMap type="testdo.Addre" id="regionMap">

<id column="id" property="id"/>

<result column="name" property="place"/>

</resultMap>

通過兩個表查詢查詢一個部門的資訊及部門對於地區的資訊:

association是用來處理多對一或一對一關係的,使用可以在這個地方使用 ,由於要以company型別輸出,所以介面裡應該以

List<company> 形式返回 ,這是內嵌select方式

 <resultMap type="testdo.Addre" id="regionMap">
       <id column="id" property="id"/>
       <result column="name" property="place"/>
   </resultMap>
   <resultMap type="testdo.company" id="assocSelect">
       <!-- id,name屬性值和列名一致,可以自動對映 -->
      <id property="id" column="id"/>
       <result column="name" property="name"/>
       <association property="addrep" column="region_id"
                    select="selectRegionById">
       </association>
   </resultMap>
   <select id="selectDepart" resultMap="assocSelect">
   select id,name,region_id
   from s_dept
</select>

   <select id="selectRegionById" resultMap="regionMap">
   select id ,name
   from s_region
   where id = #{id}
</select>

 Collection一對多的查詢:

<select id="selectRegionWithDeparts" resultMap="regionMap">
		select r.id,r.name,d.id d_id,d.name d_name
		from s_region r,s_dept d
		where r.id = d.region_id(+)
	</select>


<resultMap id="assocResultc" type="testdo.company">
        <id column="id" property="id"/>
        <result property="name" column="name"/>
    </resultMap>
    <resultMap id="regionMap" type="testdo.Addre">
        <id column="id" property="id"/>
        <result property="place" column="name"/>
        <collection property="company"  resultMap="assocResultc" columnPrefix="d_"/>
    </resultMap>

 一對多的兩表查詢:


    <resultMap id="collSelect" type="testdo.Addre">
        <id column="id" property="id"/>
        <result property="place" column="name"/>
        <collection property="company" column="id"
                    select="selectDepartByRegionId" />
    </resultMap>
    <select id="selectDepartByRegionId" resultMap="assocResultc">
		select id,name
		from s_dept
		where region_id = #{id}
	</select>
    <select id="selectRegion" resultMap="collSelect">
		select id,name
		from s_region
	</select>

多對多和一對多是一樣的

相關文章