一、CRUD語句
1.1查詢語句
<select id="selList"
resultType="***.resultVo"
parameterType="***.Request">
SELECT * FROM table WHERE *** limit #{offset},#{limit} oder by ***
</select>
1.2插入語句
<insert id="insertOrderRecord" parameterType="map">
INSERT INTO
order_table (id, customer_id, create_time, updated_time)
VALUES(#{ID}, #{employeeId}, #{date},now(), now());
</insert>
1.3修改語句
<update id="updateOrderTable" parameterType = "map">
UPDATE order_table
SET
store_id = #{param.storeId},
updated_time = NOW(),
version = version+1
WHERE
memberKey = #{param.memberKey}
</update>
1.4刪除語句
<delete id="deleteById" parameterType="java.lang.String" >
delete from order_table
where order_id = #{orderId,jdbcType=VARCHAR}
</delete>
屬性配置
屬性 | 描述 |
---|---|
id | 命名空間中的唯一標識符 ,表示此段SQL 執行語句的唯一標識,也是接口的方法名稱 必須一致才能找到。 |
parameterType | 表示SQL語句中需要傳入的參數,類型要與對應的接口方法的類型一致,這個屬性是可選的。 |
resultMap | 定義出參,調用已定義的映射管理器的id的值。 |
resultType | 定義出參,匹配普通Java類型 ,如int,string,long,Date;或自定義的pojo 。出參類型若不指定,將為語句類型默認類型,如語句返回值為 int 。 |
mapper.xml特殊符號
原符號: < <= > >= & ' "
替換符號: < <= > >= & ' "
<![CDATA[]]>
:表示XML解析器忽略解析。在使用MyBatis過程中,有時我們的SQL是寫在XML 映射文件中,如果寫的SQL中有一些特殊的字符的話,在解析XML文件的時候會被當做XML自身元素,但我們不希望如此操作,此時我們可以使用<![CDATA[ ]]>
來解決。
二、常用標簽
Mybatis的mappper.xml中用到的標簽有很多,在mybatis-3-mapper.dtd文件(點擊標簽,可跳轉到該文件)中可以查看,如:<mapper>
、<select>
、<insert>
、<selectKey>
、<update>
、<delete>
、<include>
、<resultMap>
、<association>
、<case>
、<typeAlias>
、<bind>
、<sql>
等。
2.1 <where>、<if>標簽
這兩個是使用比較多的標簽
<where>
<if test="null != theYear and '' != theYear">
the_year=#{theYear}
</if>
</where>
2.2 <sql>片段標簽
通過該標簽可定義能復用的sql語句
片段,在執行sql語句
標簽中直接引用
即可。
<!--定義sql片段---1-->
<sql id="sel_customerListSql1">
o.id,o.cid,o.address,o.create_date,i.count
</sql>
<!--定義sql片段---2-->
<sql id="sel_customerListSql2">
<where>
<if test="null != year and '' != year">
and the_year=#{year}
</if>
</where>
</sql>
<!--引用sql片段-->
select
<include refid="sel_customerListSql1" />
from customer_table o
<include refid="sel_customerListSql2"></include>
2.3 <choose> <when> <otherwise> 標簽
有條件符合,就查詢,相當于 if.....else
。該標簽可以插入在sql標簽
里面。
<select id="listProduct" resultType="Product">
SELECT * FROM product_table
<where>
<choose>
<when test="type == 1">
and `type` not in (4,6,7)
</when>
<otherwise>
and `type` in (4,6)
</otherwise>
</choose>
</where>
</select>
2.4 <set>標簽
與<where>標簽
類似的,在update語句
里也會碰到多個字段
相關的問題, 在這種情況下,就可以使用<set>標簽
。
<update id="updateUser" parameterType="com.example.model.User">
UPDATE user_table
<set>
<if test="name != null"> name = #{name},</if>
<if test="age != null"> age = #{age},</if>
</set>
WHERE id = #{id}
</update>
2.4 < foreach >標簽
<foreach>標簽
來實現循環操作,可以用于遍歷集合
或數組
,并將集合或數組中的元素作為參數傳遞給SQL語句。
<select id="getUserListByIdList" parameterType="list" resultMap="userResultMap">
SELECT * FROM user_table WHERE id IN
<foreach item="id" collection="list" open="(" separator="," close=")">
#{id}
</foreach>
</select>
<foreach>標簽
用于遍歷
傳入的List對象,將List中的元素作為參數傳遞給SQL語句中的#{id}
占位符。
item屬性介紹:item屬性
指定了集合中的元素名稱,collection屬性
指定了要遍歷的集合,open屬性
指定了循環開始時的字符串,separator屬性
指定了元素之間的分隔符,close屬性
指定了循環結束時的字符串。
如果傳入的參數是數組parameterType="array"
,可以使用collection屬性
的值為數組的名稱collection="array"
。
2.5 <bind>標簽
<bind>標簽
就像是再做一次字符串拼接
,網上也有說叫綁定,只是方便后續的使用。
<select id="orderList" resultType="Order">
<bind name="likename" value="'%' + name + '%'" />
select * from order_table where name like #{likename}
</select>
2.6 <resultMap>標簽
<resultMap>
標簽是MyBatis Mapper.xml
中用于定義結果集映射的標簽,它可以將查詢結果映射為Java對象,可以參考Mybatis中強大的resultMap。
在MyBatis中,查詢結果通常是一個ResultSet對象,ResultSet對象中包含了查詢結果的所有行和列。為了將查詢結果轉換成Java對象,需要定義一個結果集映射,將ResultSet中的每一行映射
成一個Java對象。<resultMap>標簽
就是用于定義這個映射關系的。
<!-- resultMap定義 -->
<resultMap id="userMap" type="com.example.model.User">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
</resultMap>
<!-- resultMap的引用,引用名userMap -->
<select id="getUserById" resultMap="userMap">
SELECT * FROM user_table WHERE id = #{id}
</select>
示例
<resultMap>標簽
定義了一個結果集映射
,包含了三個子標簽:<id>
和兩個<result>
。
<resultMap>
的屬性id
:理解為別名
,用于引用;type
:對應的是我們的實體類,全路徑名。<id>子標簽
用于定義主鍵
,此id值用于select元素
屬性的引用;
<result>子標簽
用于定義普通列
;
property屬性
指定了Java對象
的屬性名稱,
column屬性
指定了數據庫
表中的列名稱。
<resultMap>相關標簽
元素名稱 | 描述 |
---|---|
<association> |
關聯一個對象,即一個映射集合 |
<constructor> |
用于實例化類時,注入結果到構造方法中 |
<collection> |
關聯對象的集合 |
<association>標簽
<association>標簽
用于關聯對象。
定義關聯對象類:
@Data
public class User {
//省略用戶屬性...
//角色信息
private Role role;
}
mapper.xml
查詢語句:
<!-- User類中只有一個Role對象,并沒有role_id和role_name字段屬性。 -->
<!-- 所以,我們不能用resultType=User來返回,要使用association來關聯它們。 -->
<!-- <select id="getUserById" resultType="User"> -->
<select id="getUserById" resultType="UserMap">
SELECT
u.id,
u.username,
r.id as 'role_id',
r.name as 'role_name'
FROM
USER u
LEFT JOIN user_roles_table ur ON u.id = ur.user_id
LEFT JOIN role_table r ON r.id = ur.role_id
where u.id=#{id}
</select>
<resultMap id="userMap" type="User">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<association property="role" javaType="Role">
<id property="id" column="role_id"></id>
<result property="name" column="role_name"></result>
</association>
</resultMap>