MyBatis常用標(biāo)簽以及使用技巧總結(jié)
前言
MyBatis常用標(biāo)簽及標(biāo)簽使用技巧
MyBatis的常用標(biāo)簽有很多,比如
<sql id="">:預(yù)定義可以復(fù)用的sql語句
<include refid="">:根據(jù)id引用定義的sql語句
<trim>:空白補(bǔ)全,配合<if>標(biāo)簽使用
<if test="">:條件判斷,該語句返回的true,則該標(biāo)簽內(nèi)的語句就生效
<bind name="" value="">:創(chuàng)建一個(gè)變量,并且可以綁定到上下文
1、統(tǒng)一的增刪改
通過我暫時(shí)的開發(fā)經(jīng)驗(yàn)來說,我認(rèn)為對(duì)一張表增刪改操作,起碼包括:增加一條記錄、刪除一條記錄、修改一條記錄。所以一張表的操作起碼包括:增刪改。
增加一條是主鍵id自增,刪除一條記錄是根據(jù)主鍵id刪除,修改一條記錄是根據(jù)主鍵id修改,返回值都是Integer類型的成功增刪改的記錄條數(shù)。以User對(duì)象為例:
UserMapper.java:
@Mapper
@Repository
public interface UserMapper {
/**
* 增加一條記錄
* @param user 用戶對(duì)象
* @return 增加成功的記錄數(shù)
*/
Integer insertOne(User user);
/**
* 刪除一條記錄
* @param user 用戶對(duì)象
* @return 刪除成功的記錄數(shù)
*/
Integer deleteOne(User user);
/**
* 修改用戶
* @param user 用戶對(duì)象
* @return 修改成功的記錄數(shù)
*/
Integer updateOne(User user);
}
則對(duì)應(yīng)的UserMapper.xml:
<mapper namespace="UserMapper">
<!--update的條件sql(除了自增主鍵id)-->
<sql id="updateCondition">
<if test=" uuid!=null and uuid!='' ">
uuid = #{uuid},
</if>
<if test=" username!=null and username!='' ">
username = #{username},
</if>
<if test=" password!=null and password!='' ">
password = #{password},
</if>
</sql>
<!--新增一條-->
<insert id="insertOne"
keyProperty="id"
useGeneratedKeys="true"
parameterType="User">
INSERT INTO
user
(uuid,username,password)
VALUES
(#{uuid},#{username},#{password})
</insert>
<!--刪除一條-->
<delete id="deleteOne"
parameterType="User">
DELETE FROM user WHERE id = #{id}
</delete>
<!--修改一條(根據(jù)id主鍵)-->
<update id="updateOne"
parameterType="User">
UPDATE
user
<trim prefix="SET" suffixOverrides=",">
<include refid="updateCondition"></include>
</trim>
WHERE
id = #{id}
</update>
</mapper>
由于一般修改語句是根據(jù)id,所以在<update>標(biāo)簽中,WHERE id = #{id}是寫死了的。一般修改的話,是除了id可以修改其余都是可以修改的,所以一般字段會(huì)比較多,利用<sql>標(biāo)簽預(yù)定義好除了id的字段,并且利用<if>標(biāo)簽包裹起來,同時(shí)特別注意:最后要加上逗號(hào)。然后在<trim>標(biāo)簽中利用<include>標(biāo)簽引用預(yù)定義的修改語句的條件。<include>標(biāo)簽就相當(dāng)于直接把該id的sql語句直接粘貼到這里,這里是為了看起整體排版比較舒服所以才這么做的。
2、統(tǒng)一的查詢
通過我暫時(shí)的開發(fā)經(jīng)驗(yàn)來說,我認(rèn)為對(duì)一張表查詢操作,起碼包括:查詢多條記錄(可分頁可條件)、查詢一條記錄(可條件),查詢?cè)摫淼目傆涗洈?shù)。所以一張表的操作起碼包括:三個(gè)查詢。
查詢列表、單個(gè)查詢、查詢?cè)摫淼目傆涗洈?shù),同樣是以User對(duì)象為例:
UserMapper.java:
@Mapper
@Repository
public interface UserMapper {
/**
* 查詢所有user集合(可分頁,可多條件,可單條件)
* @param user 用戶對(duì)象
* @return user集合
*/
List<User> selectList(User user);
/**
* 查詢一條user記錄(可多條件,可單條件)
* @param user
* @return 一條用戶對(duì)象
*/
User selectOne(User user);
/**
* 查詢記錄數(shù)(可條件查詢)
* @param user 用戶對(duì)象
* @return 記錄數(shù)
*/
Long count(User user);
}
分別返回User集合、單個(gè)User對(duì)象以及記錄數(shù)。
對(duì)應(yīng)的UserMapper.xml:
<mapper namespace="UserMapper">
<!--select的條件sql(全有)-->
<sql id="selectCondition">
<if test=" id!=null and id!='' ">
AND u.id = #{id}
</if>
<if test=" uuid!=null and uuid!='' ">
AND u.uuid = #{uuid}
</if>
<if test=" username!=null and username!='' ">
AND u.username = #{username}
</if>
<if test=" password!=null and password!='' ">
AND u.password = #{password}
</if>
</sql>
<!--分頁的條件sql(當(dāng)前頁,每頁記錄數(shù))-->
<sql id="limitCondition">
<if test=" currentPage!=null and currentPage!='' and pageSize!=null and pageSize!='' ">
<bind name="offset" value="pageSize*(currentPage-1)"/>
<bind name="rows" value="pageSize"/>
#{offset},#{rows}
</if>
</sql>
<!--user表的別名-->
<sql id="userAs">
u.id AS uId,
u.uuid AS uUuid,
u.username AS uUsername,
u.password AS uPassword
</sql>
<!--返回的結(jié)果集-->
<resultMap id="userMap" type="User">
<id column="uId" property="id"/>
<result column="uUuid" property="uuid"/>
<result column="uUsername" property="username"/>
<result column="uPassword" property="password"/>
</resultMap>
<!--查詢所有(可分頁,可單條件,可多條件)-->
<select id="selectList"
parameterType="User"
resultMap="userMap">
SELECT
<include refid="userAs"></include>
FROM
user u
<trim prefix="WHERE" prefixOverrides="AND">
<include refid="selectCondition"></include>
</trim>
ORDER BY
u.id
<trim prefix="LIMIT">
<include refid="limitCondition"></include>
</trim>
</select>
<!--查詢一條(可單條件,可多條件)-->
<select id="selectOne"
parameterType="User"
resultMap="userMap">
SELECT
<include refid="userAs"></include>
FROM
user u
<trim prefix="WHERE" prefixOverrides="AND">
<include refid="selectCondition"></include>
</trim>
</select>
<!--查詢記錄數(shù)(可單條件,可多條件)-->
<select id="count"
parameterType="User"
resultType="long">
SELECT
count(u.id)
FROM
user u
<trim prefix="WHERE" prefixOverrides="AND">
<include refid="selectCondition"></include>
</trim>
</select>
</mapper>
根據(jù)三個(gè)需求:查詢?nèi)怼⒉樵儐蝹€(gè)兩個(gè)都可以用到條件查詢,所以都可以用到同樣的sql,所以就可以使用同樣的<sql>標(biāo)簽來預(yù)定義然后復(fù)用。小結(jié)一下就是:
- 查詢?nèi)恚翰樵冏侄?、可全條件、可分頁
- 查詢單個(gè):查詢字段、可全條件
- 查詢記錄數(shù):可全條件
得益于<trim>標(biāo)簽和<if>標(biāo)簽的配合(if標(biāo)簽判斷條件是否成立,如果成立則if標(biāo)簽內(nèi)部語句生效,trim判斷內(nèi)部是否有語句,如果有則trim生效),沒有條件傳入就查詢?nèi)恚瑳]有分頁傳入的時(shí)候就不分頁,做到了有條件則查條件,無條件則全查,有分頁則查分頁,無分頁則全查。同時(shí)定義好結(jié)果集映射(column是查出來的列,property是Pojo對(duì)象的屬性,需要對(duì)應(yīng)好)以及配置好查詢字段的別名(AS)。定義結(jié)果集和配置查詢字段的別名是方便以后擴(kuò)展。
條件查詢的<sql>需要注意的是利用AND連接。
分頁的<sql>需要注意的是利用<bind>標(biāo)簽來定義變量,注意變量的換算,一般采用的是傳入當(dāng)前頁(currentPage)和每頁記錄數(shù)(pageSize),而數(shù)據(jù)庫中l(wèi)imit語句的參數(shù)是limit offset,rows,offset(偏移量,也就是從表中的第幾個(gè)元素開始,0是首位),rows(記錄數(shù),也就是返回多少個(gè)),總的來說就是從第幾個(gè)開始,返回第條記錄。然后利用簡單的數(shù)學(xué)換算,將currentPage和pageSize轉(zhuǎn)換為offset和rows。
<trim>標(biāo)簽的使用,利用<include>標(biāo)簽引入相應(yīng)的sql語句后,再用<trim>標(biāo)簽包圍,注意好prefix、suffix、prefixOverrides和suffixOverrides。
注意
SQL語句中各關(guān)鍵字的順序
3、統(tǒng)一的增刪改查
最后將增刪改和查詢總結(jié)合并起來就是:
UserMapper.java:
@Mapper
@Repository
public interface UserMapper {
/**
* 增加一條記錄
* @param user 用戶對(duì)象
* @return 增加成功的記錄數(shù)
*/
Integer insertOne(User user);
/**
* 刪除一條記錄
* @param user 用戶對(duì)象
* @return 刪除成功的記錄數(shù)
*/
Integer deleteOne(User user);
/**
* 修改用戶
* @param user 用戶對(duì)象
* @return 修改成功的記錄數(shù)
*/
Integer updateOne(User user);
/**
* 查詢所有user集合(可分頁,可多條件,可單條件)
* @param user 用戶對(duì)象
* @return user集合
*/
List<User> selectList(User user);
/**
* 查詢一條user記錄(可多條件,可單條件)
* @param user
* @return 一條用戶對(duì)象
*/
User selectOne(User user);
/**
* 查詢記錄數(shù)(可條件查詢)
* @param user 用戶對(duì)象
* @return 記錄數(shù)
*/
Long count(User user);
}
UserMapper.xml:
<mapper namespace="UserMapper">
<!--select的條件sql(全有)-->
<sql id="selectCondition">
<if test=" id!=null and id!='' ">
AND u.id = #{id}
</if>
<if test=" uuid!=null and uuid!='' ">
AND u.uuid = #{uuid}
</if>
<if test=" username!=null and username!='' ">
AND u.username = #{username}
</if>
<if test=" password!=null and password!='' ">
AND u.password = #{password}
</if>
</sql>
<!--update的條件sql(除了自增主鍵id)-->
<sql id="updateCondition">
<if test=" uuid!=null and uuid!='' ">
uuid = #{uuid},
</if>
<if test=" username!=null and username!='' ">
username = #{username},
</if>
<if test=" password!=null and password!='' ">
password = #{password},
</if>
</sql>
<!--分頁的條件sql(當(dāng)前頁,每頁記錄數(shù))-->
<sql id="limitCondition">
<if test=" currentPage!=null and currentPage!='' and pageSize!=null and pageSize!='' ">
<bind name="offset" value="pageSize*(currentPage-1)"/>
<bind name="rows" value="pageSize"/>
#{offset},#{rows}
</if>
</sql>
<!--user表的別名-->
<sql id="userAs">
u.id AS uId,
u.uuid AS uUuid,
u.username AS uUsername,
u.password AS uPassword
</sql>
<!--返回的結(jié)果集-->
<resultMap id="userMap" type="User">
<id column="uId" property="id"/>
<result column="uUuid" property="uuid"/>
<result column="uUsername" property="username"/>
<result column="uPassword" property="password"/>
</resultMap>
<!--新增一條-->
<insert id="insertOne"
keyProperty="id"
useGeneratedKeys="true"
parameterType="User">
INSERT INTO
user
(uuid,username,password)
VALUES
(#{uuid},#{username},#{password})
</insert>
<!--刪除一條-->
<delete id="deleteOne"
parameterType="User">
DELETE FROM user WHERE id = #{id}
</delete>
<!--修改一條(根據(jù)id主鍵)-->
<update id="updateOne"
parameterType="User">
UPDATE
user
<trim prefix="SET" suffixOverrides=",">
<include refid="updateCondition"></include>
</trim>
WHERE
id = #{id}
</update>
<!--查詢所有(可分頁,可單條件,可多條件)-->
<select id="selectList"
parameterType="User"
resultMap="userMap">
SELECT
<include refid="userAs"></include>
user u
JOIN role r on u.roleId = r.id
<trim prefix="WHERE" prefixOverrides="AND">
<include refid="selectCondition"></include>
</trim>
ORDER BY
u.id
<trim prefix="LIMIT">
<include refid="limitCondition"></include>
</trim>
</select>
<!--查詢一條(可單條件,可多條件)-->
<select id="selectOne"
parameterType="User"
resultMap="userMap">
SELECT
<include refid="userAs"></include>
FROM
user u
<trim prefix="WHERE" prefixOverrides="AND">
<include refid="selectCondition"></include>
</trim>
</select>
<!--查詢記錄數(shù)(可單條件,可多條件)-->
<select id="count"
parameterType="User"
resultType="long">
SELECT
count(u.id)
FROM
user u
<trim prefix="WHERE" prefixOverrides="AND">
<include refid="selectCondition"></include>
</trim>
</select>
</mapper>
對(duì)一張表的操作最起碼就應(yīng)該包括以上:增加一條記錄、刪除一條記錄、修改一條記錄、查詢列表(可條件、可分頁)、查詢一條(可條件)、查詢記錄數(shù)(可條件)。
4、利用IDEA的Code Templates自動(dòng)生成
總結(jié)后觀察到,每張表都有這6個(gè)操作,并且代碼基本上都大致相同,差別只是對(duì)象的名字和表中字段不一樣,那我每次創(chuàng)建Mapper文件都需要手敲嗎?NoNoNo,可以利用IDEA的Code Templates功能,預(yù)先定義好Mapper Java和Mapper Xml的大致結(jié)構(gòu),然后利用傳參傳入對(duì)象名就可以了,這樣每次就能自動(dòng)生成大量代碼,然后只需要修改極少部分代碼就可以了。
Code Templates文件的創(chuàng)建:
打開IDEA,左上角依次點(diǎn)擊:File->搜索template->找到File and Code Templates,在右側(cè)創(chuàng)建新文件,輸入文件名,擴(kuò)展名Java:
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "") package ${PACKAGE_NAME};#end
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
#parse("File Header.java")
@Mapper
@Repository
public interface ${NAME} {
/**
* 增加一條記錄
* @param ${pojo} ${name}對(duì)象
* @return 增加成功的記錄數(shù)
*/
Integer insertOne(${Pojo} ${pojo});
/**
* 刪除一條記錄
* @param ${pojo} ${name}對(duì)象
* @return 刪除成功的記錄數(shù)
*/
Integer deleteOne(${Pojo} ${pojo});
/**
* 修改一條記錄
* @param ${pojo} ${name}對(duì)象
* @return 修改成功的記錄數(shù)
*/
Integer updateOne(${Pojo} ${pojo});
/**
* 查詢所有集合(可分頁,可多條件,可單條件)
* @param ${pojo} ${name}對(duì)象
* @return ${pojo}集合
*/
List<${Pojo}> selectList(${Pojo} ${pojo});
/**
* 查詢一條${pojo}記錄(可多條件,可單條件)
* @param ${pojo} ${name}對(duì)象
* @return 一條${name}對(duì)象
*/
${Pojo} selectOne(${Pojo} ${pojo});
/**
* 查詢記錄數(shù)(可條件查詢)
* @param ${pojo} ${name}對(duì)象
* @return 記錄數(shù)
*/
Long count(${Pojo} ${pojo});
}
${Pojo}:Pojo對(duì)象的類名
${pojo}:Pojo對(duì)象的變量名
這樣下次在新建Mapper.java文件的時(shí)候直接利用該template創(chuàng)建就可以了。
同樣的,Mapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${PRE}.mapper.${NAME}">
<!--select的條件sql(全有)-->
<sql id="selectCondition">
<if test=" arg!=null and arg!='' ">
AND arg = #{arg}
</if>
</sql>
<!--update的條件sql(除了自增主鍵id)-->
<sql id="updateCondition">
<if test=" arg!=null and arg!='' ">
arg = #{arg},
</if>
</sql>
<!--分頁的條件sql(當(dāng)前頁,每頁記錄數(shù))-->
<sql id="limitCondition">
<if test=" currentPage!=null and currentPage!='' and pageSize!=null and pageSize!='' ">
<bind name="offset" value="pageSize*(currentPage-1)"/>
<bind name="rows" value="pageSize"/>
#{offset},#{rows}
</if>
</sql>
<!--返回的結(jié)果集-->
<resultMap id="${Pojo}Map" type="${PRE}.pojo.${Pojo}">
<id column="id" property="id"/>
<result column="" property=""/>
</resultMap>
<!--新增一條記錄-->
<insert id="insertOne"
keyProperty="id"
useGeneratedKeys="true"
parameterType="${PRE}.pojo.${Pojo}">
INSERT INTO
${TABLE_NAME}
()
VALUES
()
</insert>
<!--刪除一條記錄-->
<delete id="deleteOne"
parameterType="${PRE}.pojo.${Pojo}">
DELETE FROM
${TABLE_NAME}
WHERE
id = #{id}
</delete>
<!--修改一條記錄(根據(jù)id主鍵)-->
<update id="updateOne"
parameterType="${PRE}.pojo.${Pojo}">
UPDATE
${TABLE_NAME}
<trim prefix="SET" suffixOverrides=",">
<include refid="updateCondition"></include>
</trim>
WHERE
id = #{id}
</update>
<!--查詢所有(可分頁,可單條件,可多條件)-->
<select id="selectList"
parameterType="${PRE}.pojo.${Pojo}"
resultMap="${Pojo}Map">
SELECT
*
FROM
${TABLE_NAME}
<trim prefix="WHERE" prefixOverrides="AND">
<include refid="selectCondition"></include>
</trim>
ORDER BY
id
<trim prefix="LIMIT">
<include refid="limitCondition"></include>
</trim>
</select>
<!--查詢一條(可單條件,可多條件)-->
<select id="selectOne"
parameterType="${PRE}.pojo.${Pojo}"
resultMap="${Pojo}Map">
SELECT
*
FROM
${TABLE_NAME}
<trim prefix="WHERE" prefixOverrides="AND">
<include refid="selectCondition"></include>
</trim>
</select>
<!--根據(jù)主鍵id(效率高)查詢記錄數(shù)(可單條件,可多條件)-->
<select id="count"
parameterType="${PRE}.pojo.${Pojo}"
resultType="long">
SELECT
count(id)
FROM
${TABLE_NAME}
<trim prefix="WHERE" prefixOverrides="AND">
<include refid="selectCondition"></include>
</trim>
</select>
</mapper>
${PRE}:包的前綴
${Pojo}:對(duì)象的類名
${TABLE_NAME}:表名
即可以非??焖俚厣蒑apper.java和Mapper.xml了
5、連接關(guān)系表
因?yàn)镸ybatis的特性,它可以連表查詢。同樣的,我們利用上述的方法創(chuàng)建好RoleMapper,如果想要在UserMapper中使用RoleMapper中定義好的sql,那么直接使用<include refid="包名+id">,下面是在UserMapper.xml中使用user表和role表連表查詢列表舉例:
<!--查詢所有(可分頁,可單條件,可多條件)-->
<select id="selectList"
parameterType="com.fangaoxs.userserver.pojo.User"
resultMap="userMap">
SELECT
<include refid="userAs"></include>,
<include refid="com.xxx.userserver.mapper.RoleMapper.roleAs"></include>
FROM
user u
JOIN role r on u.roleId = r.id
<trim prefix="WHERE" prefixOverrides="AND">
<include refid="selectCondition"></include>
</trim>
ORDER BY
u.id
<trim prefix="LIMIT">
<include refid="limitCondition"></include>
</trim>
</select>
那么,這樣就實(shí)現(xiàn)了user表和role的聯(lián)合查詢,并且roleMapper.xml中的<sql>也在userMapper.xml中得到了復(fù)用。
總結(jié)
到此這篇關(guān)于MyBatis常用標(biāo)簽以及使用技巧總結(jié)的文章就介紹到這了,更多相關(guān)MyBatis常用標(biāo)簽使用技巧內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java數(shù)組與堆棧相關(guān)知識(shí)總結(jié)
今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章圍繞著Java數(shù)組與堆棧展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Java聊天室之使用Socket實(shí)現(xiàn)通信功能
這篇文章主要為大家詳細(xì)介紹了Java簡易聊天室之使用Socket實(shí)現(xiàn)通信功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以了解一下2022-10-10
shrio中hashedCredentialsMatcher密碼匹配示例詳解
shrio是一個(gè)輕量級(jí)權(quán)限管理框架,密碼的匹配由框架內(nèi)部完成。密碼是否匹配由接口CredentialsMatcher定義實(shí)現(xiàn)類完成,CredentialsMatcher實(shí)現(xiàn)類有SimpleCredentialsMatcher和HashedCredentialsMatcher兩個(gè)2021-10-10
Netty + ZooKeeper 實(shí)現(xiàn)簡單的服務(wù)注冊(cè)與發(fā)現(xiàn)
服務(wù)注冊(cè)和發(fā)現(xiàn)一直是分布式的核心組件。本文介紹了借助 ZooKeeper 做注冊(cè)中心,如何實(shí)現(xiàn)一個(gè)簡單的服務(wù)注冊(cè)和發(fā)現(xiàn)。,需要的朋友可以參考下2019-06-06
一篇文章帶你搞定SpringBoot中的熱部署devtools方法
這篇文章主要介紹了一篇文章帶你搞定SpringBoot中的熱部署devtools方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

