一、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 | 命名空間中的唯一標(biāo)識符,表示此段SQL執(zhí)行語句的唯一標(biāo)識,也是接口的方法名稱 必須一致才能找到。 |
| parameterType | 表示SQL語句中需要傳入的參數(shù),類型要與對應(yīng)的接口方法的類型一致,這個(gè)屬性是可選的。 |
| resultMap | 定義出參,調(diào)用已定義的映射管理器的id的值。 |
| resultType | 定義出參,匹配普通Java類型,如int,string,long,Date;或自定義的pojo。出參類型若不指定,將為語句類型默認(rèn)類型,如語句返回值為 int。 |
mapper.xml特殊符號
原符號: < <= > >= & ' "
替換符號: < <= > >= & ' "
<![CDATA[]]>:表示XML解析器忽略解析。在使用MyBatis過程中,有時(shí)我們的SQL是寫在XML 映射文件中,如果寫的SQL中有一些特殊的字符的話,在解析XML文件的時(shí)候會被當(dāng)做XML自身元素,但我們不希望如此操作,此時(shí)我們可以使用<![CDATA[ ]]>來解決。
二、常用標(biāo)簽
Mybatis的mappper.xml中用到的標(biāo)簽有很多,在mybatis-3-mapper.dtd文件(點(diǎn)擊標(biāo)簽,可跳轉(zhuǎn)到該文件)中可以查看,如:<mapper>、<select>、<insert>、<selectKey>、<update>、<delete>、<include>、<resultMap>、<association>、<case>、<typeAlias>、<bind>、<sql>等。
2.1 <where>、<if>標(biāo)簽
這兩個(gè)是使用比較多的標(biāo)簽
<where>
<if test="null != theYear and '' != theYear">
the_year=#{theYear}
</if>
</where>
2.2 <sql>片段標(biāo)簽
通過該標(biāo)簽可定義能復(fù)用的sql語句片段,在執(zhí)行sql語句標(biāo)簽中直接引用即可。
<!--定義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> 標(biāo)簽
有條件符合,就查詢,相當(dāng)于 if.....else。該標(biāo)簽可以插入在sql標(biāo)簽里面。
<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>標(biāo)簽
與<where>標(biāo)簽類似的,在update語句里也會碰到多個(gè)字段相關(guān)的問題, 在這種情況下,就可以使用<set>標(biāo)簽。
<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 >標(biāo)簽
<foreach>標(biāo)簽來實(shí)現(xiàn)循環(huán)操作,可以用于遍歷集合或數(shù)組,并將集合或數(shù)組中的元素作為參數(shù)傳遞給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>標(biāo)簽用于遍歷傳入的List對象,將List中的元素作為參數(shù)傳遞給SQL語句中的#{id}占位符。
item屬性介紹:item屬性指定了集合中的元素名稱,collection屬性指定了要遍歷的集合,open屬性指定了循環(huán)開始時(shí)的字符串,separator屬性指定了元素之間的分隔符,close屬性指定了循環(huán)結(jié)束時(shí)的字符串。
如果傳入的參數(shù)是數(shù)組parameterType="array",可以使用collection屬性的值為數(shù)組的名稱collection="array"。
2.5 <bind>標(biāo)簽
<bind>標(biāo)簽就像是再做一次字符串拼接,網(wǎng)上也有說叫綁定,只是方便后續(xù)的使用。
<select id="orderList" resultType="Order">
<bind name="likename" value="'%' + name + '%'" />
select * from order_table where name like #{likename}
</select>
2.6 <resultMap>標(biāo)簽
<resultMap>標(biāo)簽是MyBatis Mapper.xml中用于定義結(jié)果集映射的標(biāo)簽,它可以將查詢結(jié)果映射為Java對象,可以參考Mybatis中強(qiáng)大的resultMap。
在MyBatis中,查詢結(jié)果通常是一個(gè)ResultSet對象,ResultSet對象中包含了查詢結(jié)果的所有行和列。為了將查詢結(jié)果轉(zhuǎn)換成Java對象,需要定義一個(gè)結(jié)果集映射,將ResultSet中的每一行映射成一個(gè)Java對象。<resultMap>標(biāo)簽就是用于定義這個(gè)映射關(guān)系的。
<!-- 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>標(biāo)簽定義了一個(gè)結(jié)果集映射,包含了三個(gè)子標(biāo)簽:<id>和兩個(gè)<result>。
<resultMap>的屬性id:理解為別名,用于引用;type:對應(yīng)的是我們的實(shí)體類,全路徑名。<id>子標(biāo)簽用于定義主鍵,此id值用于select元素屬性的引用;
<result>子標(biāo)簽用于定義普通列;
property屬性指定了Java對象的屬性名稱,
column屬性指定了數(shù)據(jù)庫表中的列名稱。
<resultMap>相關(guān)標(biāo)簽
| 元素名稱 | 描述 |
|---|---|
<association> |
關(guān)聯(lián)一個(gè)對象,即一個(gè)映射集合 |
<constructor> |
用于實(shí)例化類時(shí),注入結(jié)果到構(gòu)造方法中 |
<collection> |
關(guān)聯(lián)對象的集合 |
<association>標(biāo)簽
<association>標(biāo)簽用于關(guān)聯(lián)對象。
定義關(guān)聯(lián)對象類:
@Data
public class User {
//省略用戶屬性...
//角色信息
private Role role;
}
mapper.xml查詢語句:
<!-- User類中只有一個(gè)Role對象,并沒有role_id和role_name字段屬性。 -->
<!-- 所以,我們不能用resultType=User來返回,要使用association來關(guān)聯(lián)它們。 -->
<!-- <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>