關於mybatis中mapper.xmlSQL語句書寫的心得


本文主要針對MySQL

----------------------------------------------------------------------------------------------------------

用generatorConfig.xml工具生成我有總結過:http://www.cnblogs.com/fengchaoLove/p/5790352.html

了解的可以去看看。

本文主要針對純手寫。

-----------------------------------------------------------------------------------------------------------

  mapper.xml-->dao接口-->service-->Controller

  Entity實體類

  增刪改查,我們先說查select

  select *(要查詢的字段)from 表名 where id='',and ...

  mapper.xml;

  

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <!--客戶映射 指定到dao接口 -->
 4 <mapper namespace="com.hebg3.mobiledealer.modules.client.sales.dao.CustomerDao">
 5 <!--id與dao接口的方法名一直,指定結果類型,和參數類型 -->
 6 <select id="get" resultType="TCustomer" paramType="string">
 7         SELECT 
 8             *
 9         FROM t_customer a
10         WHERE a.id = #{id}<!--主鍵  -->

  但在工作中,我們發現其實我們要查的沒有那么簡單,和我學習時候的幾點出入:

    1.實體類要用駝峰命名,而數據庫中的字段是下划線割開,導致unknown錯誤。

    2.我們往往不是通過一個字段來查詢,是好幾個字段,甚至還有動態查詢(有這個字段就查,沒有就忽略)。

    3.條件查詢中有List,需要循環查找

    4.條件中有關模糊查詢。

  先來解決第一個問題:

 1 <!-- 庫存映射 -->
 2 <mapper namespace="com.hebg3.mobiledealer.modules.client.store.order.dao.OrderDao">
 3 
 4     <sql id="tOrderColumns">
 5         a.id AS "id",<!-- 主鍵 -->
 6         a.order_no AS "orderNo",<!-- 訂單編號 -->
 7         a.t_customer_id AS "customer.id",<!-- 客戶編號 -->
 8         a.sys_office_id AS "companyOffice.id",<!-- 公司編號 -->
 9         a.order_date AS "orderDate",<!-- 訂單日期 -->
10         a.document_status AS "documentStatus",<!-- 訂單狀態 -->
11         a.send_date AS "sendDate",<!-- 發送時間 -->
12         a.open_id AS "openId",<!-- 微信編號 -->
13         a.create_by AS "createBy.id",<!-- 建立人 -->
14         a.create_date AS "createDate",<!-- 建立時間 -->
15         a.update_by AS "updateBy.id",<!-- 更新人 -->
16         a.update_date AS "updateDate",<!-- 更新時間 -->
17         a.remarks AS "remarks",<!-- 備注 -->
18         a.del_flag AS "delFlag",<!-- 刪除標志 -->
19         
20     </sql>
21 
22     
23 <!-- 根據條件取得 訂單信息列表 -->
24     <select id="findPageOrder" resultType="TOrder">
25         SELECT
26 <!-- refid屬性與上面spl標簽的Id一致 -->
27         <include refid="tOrderColumns" />
28         FROM t_order a
29         <include refid="tOrderJoins" />
30         <where>
31         
32             <if test="Id!=null and id!=''">
33                 id=#{Id}
34             </if>
35             
36 
37     </select>

  <if>標簽實現動態查詢。

  如果想再加入一個查詢條件;

  在if標簽 下面加入

1 <if test="documentStatus!=null and documentStatus!=''">
2                 AND a.document_status =#{documentStatus}
3             </if>

  如果查詢條件中有List集合,可以在參數類中加入List類型的屬性,比如是List<String> documentStatusList:並生成get,set方法;

  

1 <if test="documentStatusList != null"><!-- 根據單據狀態查找 -->
2                 AND a.document_status in
3 <!-- foreach標簽需要指定四個屬性,item,index下標,collection要與指定的屬性名一直,open指定斷開符號和結束符號 -->
4                 <foreach item="item" index="index" collection="documentStatusList" open="(" separator="," close=")">
5                     #{item}
6                 </foreach>
7                  
8             </if>

  

  當然如果你想排序的話

  

1 <choose>
2             <when test="page !=null and page.orderBy != null and page.orderBy != ''"><!-- 根據 排序字段 排序 -->
3                 ORDER BY ${page.orderBy}
4             </when>
5             <otherwise>
6                 ORDER BY a.create_date DESC
7             </otherwise>
8         </choose>

  幾個注意的細節例如

  

  這其中customer都是實體類。

  

 

 

  希望對大家有所幫助。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM