ssm框架之持久層mybatis動態sql標籤屬性大全
參考並轉自:https://www.cnblogs.com/zjfjava/p/8886432.html
1. 定義sql語句
1.1 select 標籤
- id :唯一的識別符號.
- parameterType:傳給此語句的引數的全路徑名或別名 例:com.test.poso.User或user
- resultType :語句返回值型別或別名。注意,如果是集合,那麼這裡填寫的是集合的泛型,而不是集合本身(resultType 與resultMap 不能並用)
<select id="selectByPrimaryKey" resultMap="BaseResultMap
1.2 insert標籤
屬性介紹:
- id :唯一的識別符號
- parameterType:傳給此語句的引數的全路徑名或別名 例:com.test.poso.User
<insert id="insert" parameterType="Object"> insert into student <trim prefix="(" suffix="
1.3 delete標籤
屬性同 insert
<delete id="deleteByPrimaryKey" parameterType="Object"> delete from student where id=#{id} </delete>
1.4 update標籤
屬性同 insert
2. 配置JAVA物件屬性與查詢結果集中列名對應關係
resultMap 標籤的使用
基本作用:
- 建立SQL查詢結果欄位與實體屬性的對映關係資訊
- 查詢的結果集轉換為java物件,方便進一步操作。
- 將結果集中的列與java物件中的屬性對應起來並將值填充進去
!注意:與java物件對應的列不是資料庫中表的列名,而是查詢後結果集的列名
<resultMap id="BaseResultMap" type="com.online.charge.platform.student.model.Student"> <id property="id" column="id" /> <result column="NAME" property="name" /> <result column="HOBBY" property="hobby" /> <result column="MAJOR" property="major" /> <result column="BIRTHDAY" property="birthday" /> <result column="AGE" property="age" /> </resultMap> <!--查詢時resultMap引用該resultMap --> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="Object"> select id,name,hobby,major,birthday,age from student where id=#{id} </select>
標籤說明:
主標籤:
- id:該resultMap的標誌
- type:返回值的類名,此例中返回Studnet類
子標籤:
- id:用於設定主鍵欄位與領域模型屬性的對映關係,此處主鍵為ID,對應id。
- result:用於設定普通欄位與領域模型屬性的對映關係
3. 動態sql拼接
3.1 if 標籤
if標籤通常用於WHERE語句、UPDATE語句、INSERT語句中,通過判斷引數值來決定是否使用某個查詢條件、判斷是否更新某一個欄位、判斷是否插入某個欄位的值。
<if test="name != null and name != ''"> and NAME = #{name} </if>
3.2 foreach 標籤
foreach標籤主要用於構建in條件,可在sql中對集合進行迭代。也常用到批量刪除、新增等操作中。
<!-- in查詢所有,不分頁 --> <select id="selectIn" resultMap="BaseResultMap"> select name,hobby from student where id in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>
屬性介紹:
- collection:collection屬性的值有三個分別是list、array、map三種,分別對應的引數型別為:List、陣列、map集合。
- item :表示在迭代過程中每一個元素的別名
- index :表示在迭代過程中每次迭代到的位置(下標)
- open :字首
- close :字尾
- separator :分隔符,表示迭代時每個元素之間以什麼分隔
3.3 choose標籤
有時候我們並不想應用所有的條件,而只是想從多個選項中選擇一個。MyBatis提供了choose 元素,按順序判斷when中的條件出否成立,如果有一個成立,則choose結束。當choose中所有when的條件都不滿則時,則執行 otherwise中的sql。類似於Java 的switch 語句,choose為switch,when為case,otherwise則為default。
if是與(and)的關係,而choose是或(or)的關係。
<select id="getStudentListChoose" parameterType="Student" resultMap="BaseResultMap"> SELECT * from STUDENT WHERE 1=1 <where> <choose> <when test="Name!=null and student!='' "> AND name LIKE CONCAT(CONCAT('%', #{student}),'%') </when> <when test="hobby!= null and hobby!= '' "> AND hobby = #{hobby} </when> <otherwise> AND AGE = 15 </otherwise> </choose> </where> </select>
4. 格式化輸出
4.1 where標籤
當if標籤較多時,這樣的組合可能會導致錯誤。 如下:
<select id="getStudentListWhere" parameterType="Object" resultMap="BaseResultMap"> SELECT * from STUDENT WHERE <if test="name!=null and name!='' "> NAME LIKE CONCAT(CONCAT('%', #{name}),'%') </if> <if test="hobby!= null and hobby!= '' "> AND hobby = #{hobby} </if> </select>
當name值為null時,查詢語句會出現 “WHERE AND” 的情況,解決該情況除了將"WHERE"改為“WHERE 1=1”之外,還可以利用where標籤。這個“where”標籤會知道如果它包含的標籤中有返回值的話,它就插入一個‘where’。此外,如果標籤返回的內容是以AND 或OR 開頭的,則它會剔除掉。
<select id="getStudentListWhere" parameterType="Object" resultMap="BaseResultMap"> SELECT * from STUDENT <where> <if test="name!=null and name!='' "> NAME LIKE CONCAT(CONCAT('%', #{name}),'%') </if> <if test="hobby!= null and hobby!= '' "> AND hobby = #{hobby} </if> </where> </select>
4.2 set 標籤
沒有使用if標籤時,如果有一個引數為null,都會導致錯誤。當在update語句中使用if標籤時,如果最後的if沒有執行,則或導致逗號多餘錯誤。使用set標籤可以將動態的配置set關鍵字,和剔除追加到條件末尾的任何不相關的逗號。
<update id="updateStudent" parameterType="Object"> UPDATE STUDENT SET NAME = #{name}, MAJOR = #{major}, HOBBY = #{hobby} WHERE ID = #{id}; </update>
<update id="updateStudent" parameterType="Object"> UPDATE STUDENT SET <if test="name!=null and name!='' "> NAME = #{name}, </if> <if test="hobby!=null and hobby!='' "> MAJOR = #{major}, </if> <if test="hobby!=null and hobby!='' "> HOBBY = #{hobby} </if> WHERE ID = #{id}; </update>
使用set+if標籤修改後,如果某項為null則不進行更新,而是保持資料庫原值。
<update id="updateStudent" parameterType="Object"> UPDATE STUDENT <set> <if test="name!=null and name!='' "> NAME = #{name}, </if> <if test="hobby!=null and hobby!='' "> MAJOR = #{major}, </if> <if test="hobby!=null and hobby!='' "> HOBBY = #{hobby} </if> </set> WHERE ID = #{id}; </update>
4.3 trim標籤
格式化輸出,也可以通過trim標籤設定或忽略前後綴來實現,詳見大佬部落格
5. 配置關聯關係
5.1 collection標籤
5.2 association標籤
關於關聯對映關係,詳細參考這篇部落格
6. 定義常量及引用
6.1 sql標籤
當多種型別的查詢語句的查詢欄位或者查詢條件相同時,可以將其定義為常量,方便呼叫。為求<select>結構清晰也可將sql語句分解。
<!-- 查詢欄位 --> <sql id="Base_Column_List"> ID,MAJOR,BIRTHDAY,AGE,NAME,HOBBY </sql> <!-- 查詢條件 --> <sql id="Example_Where_Clause"> where 1=1 <trim suffixOverrides=","> <if test="id != null and id !=''"> and id = #{id} </if> <if test="major != null and major != ''"> and MAJOR = #{major} </if> <if test="birthday != null "> and BIRTHDAY = #{birthday} </if> <if test="age != null "> and AGE = #{age} </if> <if test="name != null and name != ''"> and NAME = #{name} </if> <if test="hobby != null and hobby != ''"> and HOBBY = #{hobby} </if> <if test="sorting != null"> order by #{sorting} </if> <if test="sort!= null and sort != '' "> order by ${sort} ${order} </if> </trim> </sql>
6.2 include標籤
用於引用定義的常量
<!-- 查詢所有,不分頁 --> <select id="selectAll" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List" /> FROM student <include refid="Example_Where_Clause" /> </select> <!-- 分頁查詢 --> <select id="select" resultMap="BaseResultMap"> select * from ( select tt.*,rownum as rowno from ( SELECT <include refid="Base_Column_List" /> FROM student <include refid="Example_Where_Clause" /> ) tt <where> <if test="pageNum != null and rows != null"> and rownum <![CDATA[<=]]>#{page}*#{rows} </if> </where> ) table_alias where table_alias.rowno>#{pageNum} </select> <!-- 根據條件刪除 --> <delete id="deleteByEntity" parameterType="java.util.Map"> DELETE FROM student <include refid="Example_Where_Clause" /> </delete>