前言??
??????SSM專欄更新中,各位大佬覺得寫得不錯,支持一下,感謝了!??????
Spring + Spring MVC + MyBatis_冷兮雪的博客-CSDN博客
終于到了MyBatis最后一篇,這篇講的是動態(tài)SQL的使用。
復雜情況:動態(tài)SQL使用??
動態(tài) SQL?是Mybatis的強大特性之?,能夠完成不同條件下不同的 SQL?拼接。 可以參考官方文檔:mybatis – MyBatis 3 | 動態(tài) SQL
一、<if>標簽??
在注冊用戶的時候,可能會有這樣?個問題,有的信息是必填,有的是選填,那如果在添加?戶的時候有不確定的字段傳入,程序應該 如何實現(xiàn)呢?
這個時候就需要使用動態(tài)標簽 <if> 來判斷了,比如添加的時候性別 sex 為非必填字段,具體實現(xiàn)如下:
<insert id="insert">
insert into user(
username,
password,
<if test="sex != null">
sex,
</if>
birthday,
head
) values (
#{username},
#{password},
<if test="sex != null">
#{sex},
</if>
#{birthday},
#{head}
)
</insert>
需要注意 test 中的 sex,是傳入對象中的屬性,不是數(shù)據(jù)庫字段。而且if標簽都是成對出現(xiàn)的
二、<trim>標簽??
之前的插入用戶功能,只是有?個 sex 字段可能是選填項,如果所有字段都是非必填項,就考慮使用<trim>標簽結(jié)合<if>標簽,對多個字段都采取動態(tài)生成的方式。
<trim>標簽中有如下屬性:
- prefix:表示整個語句塊,以prefix的值作為前綴
- suffix:表示整個語句塊,以suffix的值作為后綴
- prefixOverrides:表示整個語句塊要去除掉的前綴
- suffixOverrides:表示整個語句塊要去除掉的后綴
調(diào)整 UserMapper.xml 的插入語句為:
<insert id="insert">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="username != null">
username,
</if>
<if test="password != null">
password,
</if>
<if test="sex != null">
sex,
</if>
<if test="birthday != null">
birthday,
</if>
<if test="head != null">
head,
</if>
<if test="createTime != null">
create_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="username != null">
#{username},
</if>
<if test="password != null">
#{password},
</if>
<if test="sex != null">
#{sex},
</if>
<if test="birthday != null">
#{birthday},
</if>
<if test="head != null">
#{head},
</if>
<if test="createTime != null">
#{createTime},
</if>
</trim>
</insert>
在以上 sql 動態(tài)解析時,會將第?個 <trim> 部分做如下處理:
- 基于 prefix 配置,前綴部分加上 (
- 基于 suffix 配置,后綴部分加上 )
- 多個 <if>組織的語句都以 , 結(jié)尾,在最后拼接好的字符串還會以 , 結(jié)尾,會基于 suffixO verrides 配置去掉最后?個 ,
- 注意 <if test=“createTime != null”> 中的 createTime 是傳入對象的屬性,不是數(shù)據(jù)庫字段
三、<where>標簽??
傳入的用戶對象,根據(jù)屬性做 where 條件查詢,用戶對象中屬性不為 null 的,都為查詢條件。如user.username 為 "a",則查詢條件為 where username="a":
UserMapper:
List<User> selectByCondition(User user);
UserMapper.xml:
<select id="selectByCondition" parameterType="com.example.ssmdemo1.entity.Userinfo" resultMap="baseMap">
select id, username, password, nickname, sex, birthday, head, create_time from user
<where>
<if test="username != null">
and username=#{username}
</if>
<if test="password != null">
and password=#{password}
</if>
<if test="sex != null">
and sex=#{sex}
</if>
<if test="birthday != null">
and birthday=#{birthday}
</if>
<if test="head != null">
and head=#{head}
</if>
<if test="createTime != null">
and create_time=#{createTime}
</if>
</where>
</select>
四、<set>標簽??
根據(jù)傳入的用戶對象屬性來更新用戶數(shù)據(jù),可以使用<set>標簽來指定動態(tài)內(nèi)容。
UserMapper 接口中修改用戶方法:根據(jù)傳入的用戶 id 屬性,修改其他不為 null 的屬性:
int updateById(User user);
UserMapper.xml 中添加更新用戶?SQL:
<update id="updateById" parameterType="com.example.ssmdemo1.entity.Userinfo">
update user
<set>
<if test="username != null">
username=#{username},
</if>
<if test="password != null">
password=#{password},
</if>
<if test="sex != null">
sex=#{sex},
</if>
<if test="birthday != null">
birthday=#{birthday},
</if>
<if test="head != null">
head=#{head},
</if>
<if test="createTime != null">
create_time=#{createTime},
</if>
</set>
where id=#{id}
</update>
以上<set>標簽也可以使用?<trim prefix="set" suffixOverrides=","> 替換。
五、<foreach>標簽??
對集合進?遍歷時可以使用該標簽。<foreach>標簽有如下屬性:
- collection:綁定方法參數(shù)中的集合,如 List,Set,Map或數(shù)組對象
- item:遍歷時的每?個對象
- open:語句塊開頭的字符串
- close:語句塊結(jié)束的字符串
- separator:每次遍歷之間間隔的字符串
示例:根據(jù)多個文章 id 來刪除文章數(shù)據(jù)。
ArticleMapper 中新增接口方法:
int deleteByIds(List<Integer> ids);
ArticleMapper.xml 中新增刪除 SQL:
<delete id="deleteByIds">
delete from article where id in
<foreach collection="list" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
</delete>
單元測試代碼,刪除id為5和6的用戶:
@Test
void deleteByIds() {
List<Integer> ids=new ArrayList<>();
ids.add(5);
ids.add(6);
int result=userMapper.deleteByIds(ids);
System.out.println("刪除:"+result);
}
六、choose-when-otherwise??
類似于Java中的switch語句,根據(jù)不同條件選擇不同的SQL片段。
<select id="getUserList" resultType="User">
SELECT * FROM users
<where>
<choose>
<when test="status != null">
AND status = #{status}
</when>
<when test="name != null and name != ''">
AND name = #{name}
</when>
<otherwise>
AND active = 1
</otherwise>
</choose>
</where>
</select>
動態(tài)SQL是MyBatis的一個重要特性,它允許你在SQL語句中根據(jù)條件動態(tài)地添加、修改或刪除語句片段,以便更靈活地構(gòu)建SQL查詢和更新操作。
上面這些示例只是MyBatis動態(tài)SQL的一小部分用法。你可以根據(jù)自己的需求和情況,結(jié)合使用這些特性來構(gòu)建更靈活、可維護的數(shù)據(jù)庫操作語句。記得閱讀MyBatis的官方文檔以深入了解動態(tài)SQL的更多用法和細節(jié)。文章來源:http://www.zghlxwxcb.cn/news/detail-627986.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-627986.html
到了這里,關(guān)于MyBatis查詢數(shù)據(jù)庫(4)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!