1. 程式人生 > >mybatis多表聯合查詢

mybatis多表聯合查詢

以前東拼西湊來的,不知道都是哪些連結!


User 、Role 的對應關係是,一個使用者有多個角色,因此,在 User 的實體中加入一個 Role 的屬性private List<Role> roles;對應一對多的關係。

  1.   <select id="queryForList" resultMap="queryForListMap">  
  2.         SELECT  
  3.           u.id,  
  4.           u.username,  
  5.           u.password,  
  6.           r.id r_id,  
  7.           r.name r_name,  
  8.           r.jsms r_jsms,  
  9.           r.bz r_bz,  
  10.           r.jlzt r_jlzt,  
  11.           r.glbm r_glbm  
  12.         FROM  
  13.           user u  
  14.         LEFT JOIN  
  15.           role r  
  16.         ON  
  17.           u.id = r.userid  
  18.     </select>  
  1. <resultMap id="queryForListMap" type="com.sica.domain.User">  
  2.         <
    id column="id" property="id" jdbcType="VARCHAR"/>  
  3.         <result column="username" property="username" jdbcType="VARCHAR"/>  
  4.         <result column="password" property="password" jdbcType="VARCHAR"/>  
  5.         <collection property="roles" javaType="java.util.List" ofType="com.sica.domain.Role"
    >  
  6.             <id column="r_id" property="id" jdbcType="VARCHAR" />  
  7.             <result column="r_name" property="name" jdbcType="VARCHAR" />  
  8.             <result column="r_jsms" property="jsms" jdbcType="VARCHAR" />  
  9.             <result column="r_bz" property="bz" jdbcType="VARCHAR" />  
  10.             <result column="r_jlzt" property="jlzt" jdbcType="INTEGER" />  
  11.             <result column="r_glbm" property="glbm" jdbcType="VARCHAR" />  
  12.         </collection>  
  13.     </resultMap>  

1.MyBatis實現一對一有幾種方式?具體怎麼操作的
        有聯合查詢和巢狀查詢,聯合查詢是幾個表聯合查詢,只查詢一次,
        通過在resultMap裡面配置association節點配置一對一的類就可以完成;

        巢狀查詢是先查一個表,根據這個表裡面
        的結果的外來鍵id,去再另外一個表裡面查詢資料,也是通過association配置,但另外一個表
        的查詢通過select屬性配置

  1. <resultMap id=”blogResult” type=”Blog”>  
  2. <association property="author" column="blog_author_id" javaType="Author"  
  3. select=”selectAuthor”/>  
  4. </resultMap>  
  5. <select id=”selectBlog” parameterType=”int” resultMap=”blogResult”>  
  6. SELECT * FROM BLOG WHERE ID = #{id}  
  7. </select>  
  8. <select id=”selectAuthor” parameterType=”int” resultType="Author">  
  9. SELECT * FROM AUTHOR WHERE ID = #{id}  
  10. </select> 
2.MyBatis實現一對多有幾種方式,怎麼操作的
        有聯合查詢和巢狀查詢,聯合查詢是幾個表聯合查詢,只查詢一次,通過在resultMap裡面配
        置collection節點配置一對多的類就可以完成;

  1. <resultMap id="detailedBlogResultMap" type="Blog">  
  2. <constructor>  
  3. <idArg column="blog_id" javaType="int"/>  
  4. </constructor>  
  5. <result property="title" column="blog_title"/>  
  6. <association property="author" column="blog_author_id" javaType=" Author">  
  7. <id property="id" column="author_id"/>  
  8. <result property="username" column="author_username"/>  
  9. <result property="password" column="author_password"/>  
  10. <result property="email" column="author_email"/>  
  11. <result property="bio" column="author_bio"/>  
  12. <result property="favouriteSection" column="author_favourite_section"/>  
  13. </association>  
  14. <collection property="posts" ofType="Post">  
  15. <id property="id" column="post_id"/>  
  16. <result property="subject" column="post_subject"/>  
  17. <association property="author" column="post_author_id" javaType="Author"/>  
  18. <collection property="comments" column="post_id" ofType=" Comment">  
  19. <id property="id" column="comment_id"/>  
  20. </collection>  
巢狀查詢是先查一個表,根據這個表裡面的
結果的外來鍵id,去再另外一個表裡面查詢資料,也是通過配置collection,但另外一個表的
查詢通過select節點配置

3.使用resultType、ResultMap處理返回結果

resultType:指定返回值結果的完全限定名,處理多表查詢的結果。多表查詢需要定義vo封裝查詢的結果。只能用在單表查詢或者定義vo的情況,如果是定義vo不能將關聯的資料封裝為需要獲得某個類的物件

使用resultMap:不需要定義vo類,將關聯資料對應的類,作為另外一個類的屬性。

      第一步:定義resultMap

      第二步:引用resultMap

      使用resultMap:用來多表關聯的複雜查詢中,通過需要將關聯查詢的資料封裝為某個實體類物件,如果有特殊業務需要或者說明需要將管理資料封裝為實體類的物件,使用resultMap

4.Xml對映檔案中,除了常見的select|insert|updae|delete標籤之外,還有哪些標籤?

還有很多其他的標籤,<resultMap>、<parameterMap>、<sql>、<include>、<selectKey>,加上動態sql的9個標籤,trim|where|set|foreach|if|choose|when|otherwise|bind等,其中<sql>為sql片段標籤,通過<include>標籤引入sql片段,<selectKey>為不支援自增的主鍵生成策略標籤。


choose (when, otherwise)標籤
if標籤是與(and)的關係,而 choose 是或(or)的關係。
choose標籤是按順序判斷其內部when標籤中的test條件出否成立,如果有一個成立,則 choose 結束。當 choose 中所有 when 的條件都不滿則時,則執行 otherwise 中的sql。類似於Java的 switch 語句,choose 為 switch,when 為 case,otherwise 則為 default。
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
        select * from t_blog where 1 = 1 
        <choose>
            <when test="title != null">
                and title = #{title}
            </when>
            <when test="content != null">
                and content = #{content}
            </when>
            <otherwise>
                and owner = "owner1"
            </otherwise>
        </choose>
    </select>

parameterType使用了Java.lang.String型別
bind標籤的value值得寫法:value="'%'+_parameter.getEmail() +'%'"
SQL語句中使用:email like #{pattern},注意是#,而不是$
最後,測試時使用的程式碼也需要注意。

<select id="findUserByFuzzyEmail" resultMap="BaseResultMap"
        parameterType="java.lang.String">
        select id,username,email,status,createtime,updatetime from tb_user
        <bind name="pattern"value="'%'+_parameter.getEmail() +'%'" />
        <where>
            <if test="email != null and email != ''">
                email like #{pattern}
            </if>
        </where>
    </select>

selectKey 標籤 生成主鍵

 在insert語句中,在Oracle經常使用序列、在MySQL中使用函式來自動生成插入表的主鍵,而且需要方法能返回這個生成主鍵。
  1.     <selectKey keyProperty="studentId" resultType="String" order="BEFORE">  
  2.         select nextval('student')  
  3.     </selectKey>  
屬性 描述 取值
keyProperty selectKey 語句生成結果需要設定的屬性。
resultType 生成結果型別,MyBatis 允許使用基本的資料型別,包括String 、int型別。
order

1:BEFORE,會先選擇主鍵,然後設定keyProperty,再執行insert語句;

2:AFTER,就先執行insert 語句再執行selectKey 語句。

BEFORE(orcle)

AFTER(mysql)
if where

sql 片段    include


3、使用foreach進行sql語句拼接 
在向sql傳遞陣列或List,mybatis使用foreach解析,我們可以使用foreach中元素進行sql語句的拼接,請求資料。 
通過一個例子來看: 
需求:SELECT * FROM USER WHERE id=1 OR id=10 OR id=16 
或者:SELECT * FROM USER WHERE id IN(1,10,16)

<if test="ids != null">     
        <foreach collection="ids"item="user_id"open="AND ("close=")" separator="or" >
    每次遍歷需要拼接的串
                id= #{user_id}
        </foreach>
 </if>

其中,collection:指定輸入物件中集合屬性,item: 每個遍歷生成物件,open:開始遍歷時拼接串,close: 結束遍歷是拼接的串,separator: 遍歷的兩個物件中需要拼接的串

使用set+if標籤修改後,如果某項為null則不進行更新(如studentName為null則studentName不更新,sex,birthday更新),而是保持資料庫原值。如下示例:

Xml程式碼  

  1. <!-- 更新學生資訊 -->     
  2. <update id="updateStudent" parameterType="StudentEntity">     
  3.     UPDATE STUDENT_TBL      
  4.     <set>    
  5.         <if test="studentName!=null and studentName!='' ">     
  6.             STUDENT_TBL.STUDENT_NAME = #{studentName},      
  7.         </if>     
  8.         <if test="studentSex!=null and studentSex!='' ">     
  9.             STUDENT_TBL.STUDENT_SEX = #{studentSex},      
  10.         </if>     
  11.         <if test="studentBirthday!=null ">     
  12.             STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},      
  13.         </if>     
  14.         <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">     
  15.             STUDENT_TBL.CLASS_ID = #{classEntity.classID}      
  16.         </if>     
  17.     </set>     
  18.     WHERE STUDENT_TBL.STUDENT_ID = #{studentID};      
  19. </update>  
3.2.3 trim
 trim是更靈活的去處多餘關鍵字的標籤,他可以實踐where和set的效果。

 where例子的等效trim語句:

Xml程式碼  

  1. <!-- 查詢學生list,like姓名,=性別 -->     
  2. <select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">     
  3.     SELECT * from STUDENT_TBL ST      
  4.     <trim prefix="WHERE" prefixOverrides="AND|OR">     
  5.         <if test="studentName!=null and studentName!='' ">     
  6.             ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
  7.         </if>     
  8.         <if test="studentSex!= null and studentSex!= '' ">     
  9.             AND ST.STUDENT_SEX = #{studentSex}      
  10.         </if>     
  11.     </trim>     
  12. </select>  

bind標籤

parameterType使用了Java.lang.String型別
bind標籤的value值得寫法:value="'%'+_parameter.getEmail() +'%'"
SQL語句中使用:email like #{pattern},注意是#,而不是$
最後,