前言
本文主要介紹的是Mybatis中對應實體的mapper.xml裏面的標籤說明
定義SQL語句
select標籤
屬性介紹:
- id :唯一的標識符.映射方法名稱
- parameterType:傳給此語句的參數的全路徑名或別名 例:com.mybatis.demo.model.User或User
- resultType :語句返回值類型或別名。注意,如果是集合,那麼這裏填寫的是集合的泛型,而不是集合本身(resultType 與resultMap 不能並用)
示例
-
<select id="selectById" resultMap="userInfoMap" parameterType="java.lang.Integer"> select <include refid="BASE_COLUMN"/> from <include refid="BASE_TABLE"/> where user_id = #{userId,jdbcType=INTEGER} </select>
insert標籤
屬性介紹:
- id :唯一的標識符
- parameterType:傳給此語句的參數的全路徑名或別名 例:com.mybatis.demo.model.User或User
示例
-
<!--插入變量名 不選擇的--> <insert id="insert" parameterType="com.tmf.mybatis.demo.model.User"> insert into <include refid="BASE_TABLE"/> (user_id,user_name,user_pwd,user_phone) values (#{user_id,jdbcType=INTEGER},#{user_name,jdbcType=VARCHAR},#{user_pwd,jdbcType=VARCHAR},#{user_phone,jdbcType=VARCHAR},) </insert> <!--插入 變量名 可選擇的--> <insert id="insertSelective" parameterType="com.tmf.mybatis.demo.model.User"> insert into <include refid="BASE_TABLE"/> <trim prefix="(" suffix=")" suffixOverrides=","> <if test="userId!=null"> user_id, </if> <if test="userName!=null"> user_name, </if> <if test="userPwd!=null"> user_pwd, </if> <if test="userPhone!=null"> user_phone, </if> </trim> <trim prefix="VALUES(" suffix=")" suffixOverrides=","> <if test="userId!=null"> #{user_id,jdbcType=INTEGER}, </if> <if test="userName!=null"> #{user_name,jdbcType=VARCHAR}, </if> <if test="userPwd!=null"> #{user_pwd,jdbcType=VARCHAR}, </if> <if test="userPhone!=null"> #{user_phone,jdbcType=VARCHAR}, </if> </trim> </insert>
update標籤
屬性同insert
示例
-
<!--根據Id進行更新--> <update id="updateById" parameterType="com.tmf.mybatis.demo.model.User"> update <include refid="BASE_TABLE"/> set user_name=#{userName,jdbcType=VARCHAR}, user_pwd = #{userPwd,jdbcType=VARCHAR}, user_phone = #{userPhone,jdbcType=VARCHAR} where user_id = #{userId,jdbcType=INTEGER} </update> <!--根據主鍵進行更新可選擇的--> <update id="updateByIdSelective" parameterType="com.tmf.mybatis.demo.model.User"> update <include refid="BASE_TABLE"/> <set> <if test="userPhone!=null"> user_name=#{userName,jdbcType=VARCHAR}, </if> <if test="userPwd!=null"> user_pwd=#{userPwd,jdbcType=VARCHAR}, </if> <if test="userPhone!=null"> user_phone=#{userPhone,jdbcType=VARCHAR}, </if> </set> where user_id = #{userId,jdbcType=INTEGER} </update>
delete標籤
屬性同insert
示例
-
<delete id="deleteUser" parameterType="java.lang.Integer"> delete from user where id = #{id} </delete>
配置Java對象屬性與查詢結果集中列名對應關係
resultMap標籤
resultMap 標籤的使用
-
基本作用:
- 建立SQL查詢結果字段與實體屬性的映射關係信息
- 查詢的結果集轉換爲java對象,方便進一步操作。
- 將結果集中的列與java對象中的屬性對應起來並將值填充進去
-
注意:與java對象對應的列不是數據庫中表的列名,而是查詢後結果集的列名
示例
-
<!--查詢返回值--> <resultMap id="userInfoMap" type="com.tmf.mybatis.demo.model.User"> <id column="user_id" property="userId" jdbcType="INTEGER"/> <result column="user_name" property="userName" jdbcType="VARCHAR"/> <result column="user_pwd" property="userPwd" jdbcType="VARCHAR"/> <result column="user_phone" property="userPhone" jdbcType="VARCHAR"/> </resultMap> <!--全查詢 查詢時resultMap引用該resultMap--> <select id="selectAll" resultMap="userInfoMap"> select <include refid="BASE_COLUMN"/> from <include refid="BASE_TABLE"/> </select>
標籤說明:
主標籤:
- id:該resultMap的標誌
- type:返回值的類名,此例中返回Studnet類
子標籤:
- id:用於設置主鍵字段與領域模型屬性的映射關係,此處主鍵爲ID,對應id。
- result:用於設置普通字段與領域模型屬性的映射關係
控制動態SQL拼接
if標籤
if標籤通常用於WHERE語句、UPDATE語句、INSERT語句中,通過判斷參數值來決定是否使用某個查詢條件、判斷是否更新某一個字段、判斷是否插入某個字段的值
示例
-
<if test="userPhone!=null"> user_name=#{userName,jdbcType=VARCHAR}, </if>
foreach標籤
foreach標籤主要用於構建in條件,可在sql中對集合進行迭代。也常用到批量刪除、添加等操作中。
示例
-
<delete id="deleteBatch"> delete from user where id in <foreach collection="array" item="id" index="index" open="(" close=")" separator=","> #{id} </foreach> </delete>
-
我們假如說參數爲---- int[] ids = {1,2,3,4,5} ----那麼打印之後的SQL如下:
delete form user where id in (1,2,3,4,5)
屬性介紹:
- collection:collection屬性的值有三個分別是list、array、map三種,分別對應的參數類型爲:List、數組、map集合。
- item :表示在迭代過程中每一個元素的別名
- index :表示在迭代過程中每次迭代到的位置(下標)
- open :前綴
- close :後綴
- separator :分隔符,表示迭代時每個元素之間以什麼分隔
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>
格式化輸出
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="getServiceListBySearch" resultType="java.util.Map">
SELECT *
FROM EHR_SERVICE_RECORD se
LEFT JOIN EHR_PERSONAL_HEALTH_RECORD pe
ON se.EHR_RCD_ID = pe.RCD_ID
LEFT JOIN MBR_MEMBERSHIP_INFO me
ON pe.RCD_MBR_CARD= me.MBR_CARD
<where>
<if test="SearchVO.bespeakStartTime != null and '' != SearchVO.bespeakStartTime">
se.RCD_SCHEDULE_TIME >= #{SearchVO.bespeakStartTime}
</if>
</where>
</select>
set標籤
沒有使用if標籤時,如果有一個參數爲null,都會導致錯誤。當在update語句中使用if標籤時,如果最後的if沒有執行,則或導致逗號多餘錯誤。使用set標籤可以將動態的配置set關鍵字,和剔除追加到條件末尾的任何不相關的逗號。
<update id="updateByIdSelective" parameterType="com.tmf.mybatis.demo.model.User">
update
<include refid="BASE_TABLE"/>
set
user_name=#{userName,jdbcType=VARCHAR},
user_pwd=#{userPwd,jdbcType=VARCHAR},
user_phone=#{userPhone,jdbcType=VARCHAR},
where user_id = #{userId,jdbcType=INTEGER}
</update>
使用set+if標籤修改後,如果某項爲null則不進行更新,而是保持數據庫原值。
<!--根據主鍵進行更新可選擇的-->
<update id="updateByIdSelective" parameterType="com.tmf.mybatis.demo.model.User">
update
<include refid="BASE_TABLE"/>
<set>
<if test="userPhone!=null">
user_name=#{userName,jdbcType=VARCHAR},
</if>
<if test="userPwd!=null">
user_pwd=#{userPwd,jdbcType=VARCHAR},
</if>
<if test="userPhone!=null">
user_phone=#{userPhone,jdbcType=VARCHAR},
</if>
</set>
where user_id = #{userId,jdbcType=INTEGER}
</update>
trim標籤
trim標籤是一個格式化的標記,主要用於拼接sql的條件語句(前綴或後綴的添加或忽略),可以完成set或者是where標記的功能。
trim屬性介紹
- prefix:前綴覆蓋並增加其內容
- suffix:後綴覆蓋並增加其內容
- prefixOverrides:前綴判斷的條件
- suffixOverrides:後綴判斷的條件
update中
<update id="updateByPrimaryKey" parameterType="Object">
update student set
<trim suffixOverrides="," >
<if test="name != null ">
NAME=#{name},
</if>
<if test="hobby != null ">
HOBBY=#{hobby},
</if>
</trim> where id=#{id}
</update>
如果name和hobby的值都不爲空的話,會執行如下語句
update student set NAME='XX',HOBBY='XX' /*,*/ where id='XX'
select中
<select id="selectByNameOrHobby" resultMap="BaseResultMap">
select * from student
<trim prefix="WHERE" prefixOverrides="AND | OR">
<if test="name != null and name.length()>0"> AND name=#{name}
</if>
<if test="hobby != null and hobby.length()>0"> AND hobby=#{hobby}
</if>
</trim>
</select>
如果name和hobby的值都不爲空的話,會執行如下語句
select * from user WHERE /*and*/ name = ‘xx’ and hobby= ‘xx’
會爲片段添加 “WHERE” 前綴,並忽略第一個 “and” ;
當然,避免出現“WHERE AND”還有其他方法,如下
<!--將where提取出來,並加上“1=1”的查詢條件 -->
select * from student
where 1=1
<trim suffixOverrides=",">
<if test="name != null and name != ''">
and NAME = #{name}
</if>
<if test="hobby != null and hobby != ''">
and HOBBY = #{hobby}
</if>
</trim>
insert中
<insert id="insertSelective" parameterType="com.tmf.mybatis.demo.model.User">
insert into
<include refid="BASE_TABLE"/>
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="userId!=null">
user_id,
</if>
<if test="userName!=null">
user_name,
</if>
<if test="userPwd!=null">
user_pwd,
</if>
<if test="userPhone!=null">
user_phone,
</if>
</trim>
<trim prefix="VALUES(" suffix=")" suffixOverrides=",">
<if test="userId!=null">
#{user_id,jdbcType=INTEGER},
</if>
<if test="userName!=null">
#{user_name,jdbcType=VARCHAR},
</if>
<if test="userPwd!=null">
#{user_pwd,jdbcType=VARCHAR},
</if>
<if test="userPhone!=null">
#{user_phone,jdbcType=VARCHAR},
</if>
</trim>
</insert>
可以生成正確的insert語句
配置關聯關係
association標籤
通常用於一對關係映射,一對一關係推薦使用唯一主外鍵關聯,即兩張表使用外鍵關聯關係,由於是一對一關聯,因此還需要給外鍵列增加unique唯一約束。
一對一關係映射
示例
-
首先需要在對應實體帖子實體Topic中與帖子類型實體TopicType中建立對應的一對一關係
-
private TopicType topicType;
-
<association property="topicType" select="selectType" column="topics_type_id" javaType="com.tmf.bbs.pojo.Type"/>
屬性說明
- property:屬性名
- select:要連接的查詢,也就是帖子類型查詢語句
- column:共同列
- javaType:集合中元素的類型
多對一關係映射
示例
-
<!-- 多對一關聯映射:association --> <association property="clazz" javaType="org.zang.domain.Clazz"> <id property="id" column="id"/> <result property="code" column="code"/> <result property="name" column="name"/> </association> <!-- 根據id查詢學生信息,多表連接,返回resultMap --> <select id="selectStudentById" parameterType="int" resultMap="studentResultMap"> SELECT * FROM tb_clazz c,tb_student s WHERE c.id = s.clazz_id AND s.id = #{id} </select>
-
使用<association…/>元素映射多對一的關聯關係。因爲<select id=“selectStudentById”…/>的sql語句是一條多表連接,關聯tb_clazz表的同時查詢了班級數據,所以<association…/>只是簡單的裝載數據。
-
注意:在實際開發中,由於一對多關係通常映射爲集合對象,而由於多方的數據量可能很大,所以通常使用懶加載;而多對一隻是關聯到一個對象,所以通常使用多表連接直接提取出數據。
collection標籤
通常用於一對多關係映射,或者多對多關係映射。數據庫中一對多關係通常使用主外鍵關聯,外鍵列應該在多方,即多方維護關係。
一對多關係映射
示例
-
首先需要在對應實體帖子實體Topic中與帖子評論實體TopicComment中建立對應的一對多關係
-
private List<TopicComment> topicComment;
-
<collection property="topicComment" column="id" ofType="com.tmf.bbs.pojo.Comment" select="selectComment" fetchType="lazy" />
屬性說明
- property:屬性名
- column:共同列
- ofType:集合中元素的類型
- select:要連接的查詢,也就是帖子評論查詢實體
- fetchType:lazy表示懶加載,fatch機制更多的是爲了性能考慮正常情況下,一對多所關聯的集合對象,都應該被設置成lazy。
多對多關係映射
例如在一個購物系統中,一個用戶可以有多個訂單,這是一對多的關係;一個訂單中可以有多種商品,一種商品也可以屬於多個不同的訂單,訂單和商品就是多對多的關係。
示例
-
訂單實體和商品實體實體中需要指定對應關係
-
// 訂單和商品是多對多的關係,即一個訂單可以包含多種商品 class Order private List<Product> products; // 商品和訂單是多對多的關係,即一種商品可以包含在多個訂單中 class Product private List<Order> orders;
-
訂單實體mapper.xml 和商品實體mapper.xml需要指定關係映射
-
<!-- 一對多關聯映射:collection --> <collection property="orders" javaType="ArrayList" column="id" ofType="org.zang.domain.User" select="org.zang.mapper.OrderMapper.selectOrderByUserId" fetchType="lazy"> <id property="id" column="id"/> <result property="code" column="code"/> <result property="total" column="total"/> </collection> <!-- 多對多映射的關鍵:collection --> <collection property="products" javaType="ArrayList" column="oid" ofType="org.zang.domain.Product" select="org.zang.mapper.ProductMapper.selectProductByOrderId" fetchType="lazy"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="price" column="price"/> <result property="remark" column="remark"/> </collection>
小結
- * 對多關聯映射:collection
- * 對一關聯映射:association
- 一對多使用的都是lazy(懶加載)
定義常量
sql標籤
當多種類型的查詢語句的查詢字段或者查詢條件相同時,可以將其定義爲常量,方便調用。爲求結構清晰也可將sql語句分解。
示例
-
<!--設置統一使用模板 表 列--> <sql id="BASE_TABLE"> t_user </sql> <!--查詢字段--> <sql id="BASE_COLUMN"> user_id,user_name,user_pwd,user_phone </sql> <!-- 查詢條件 --> <sql id="Example_Where_Clause"> where 1=1 <trim suffixOverrides=","> <if test="userId != null and userId !=''"> and user_id = #{userId} </if> <if test="userName != null and userName != ''"> and user_name = #{userName} </if> <if test="userPwd != userPwd "> and user_pwd = #{userPwd} </if> <if test="userPhone != userPhone "> and user_phone = #{userPhone} </if> </trim> </sql>
include標籤
用於引用定義的常量,也就是引用sql標籤中的id
示例
-
<!--查詢 通過Id--> <select id="selectById" resultMap="userInfoMap" parameterType="java.lang.Integer"> select <include refid="BASE_COLUMN"/> from <include refid="BASE_TABLE"/> where user_id = #{userId,jdbcType=INTEGER} </select> <!--全查詢--> <select id="selectAll" resultMap="userInfoMap"> select <include refid="BASE_COLUMN"/> from <include refid="BASE_TABLE"/> </select> <!--根據主鍵進行更新可選擇的--> <update id="updateByIdSelective" parameterType="com.tmf.mybatis.demo.model.User"> update <include refid="BASE_TABLE"/> <set> <if test="userPhone!=null"> user_name=#{userName,jdbcType=VARCHAR}, </if> <if test="userPwd!=null"> user_pwd=#{userPwd,jdbcType=VARCHAR}, </if> <if test="userPhone!=null"> user_phone=#{userPhone,jdbcType=VARCHAR}, </if> </set> where user_id = #{userId,jdbcType=INTEGER} </update> <!--插入變量名 不選擇的--> <insert id="insert" parameterType="com.tmf.mybatis.demo.model.User"> insert into <include refid="BASE_TABLE"/> (user_id,user_name,user_pwd,user_phone) values (#{user_id,jdbcType=INTEGER},#{user_name,jdbcType=VARCHAR},#{user_pwd,jdbcType=VARCHAR},#{user_phone,jdbcType=VARCHAR},) </insert> <!-- 根據條件刪除 --> <delete id="deleteByEntity" parameterType="java.util.Map"> DELETE FROM student <include refid="Example_Where_Clause" /> </delete>
本文主要參考於Mybatis全部標籤與解釋說明
結語
前面如有不正確的地方還希望大家多多指教,希望和志同道合的朋友一起學習,一起進步,先更新到這裏,下次繼續補充。