動態(tài)SQL就是根據(jù)不同的條件或需求動態(tài)地生成查詢語句,比如動態(tài)搜索條件、動態(tài)表或列名、動態(tài)排序等。
if標簽
在我們填寫一些信息時,有些信息是必填字段,有的則是非必填的,這些信息的傳入就需要使?動態(tài)標簽 if來判斷了
創(chuàng)建這樣想學(xué)生表就可以進行測試了
drop table if exists stu;
create table stu(
id int primary key auto_increment,
name varchar(100) ,
sex varchar(10)
) default charset 'utf8mb4';
下面是xml語句
<insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="com.example.mybatisdemo.model.User">
INSERT INTO stu(name
<if test="sex != null">, sex</if>) VALUES (#{name}
<if test="sex != null">, #{sex}</if>)
</insert>
通過結(jié)果我們發(fā)現(xiàn),數(shù)據(jù)已經(jīng)被插入進入數(shù)據(jù)庫了,并且sex為空。
trim標簽
如果所有字段都是?必填項,可以考慮使?trim標簽結(jié)合if標簽,對多個字段都采取動態(tài)?成的?式。
trim標簽中有如下屬性:
- prefix:表示整個語句塊,以prefix的值作為前綴
- suffix:表示整個語句塊,以suffix的值作為后綴
- prefixOverrides:表示整個語句塊要去除掉的前綴
- suffixOverrides:表示整個語句塊要去除掉的后綴
通過對前面的xml代碼進行修改結(jié)果如下:
<insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="com.example.mybatisdemo.model.User">
INSERT INTO stu
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null ">name,</if>
<if test="sex != null">sex</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="name != null">#{name},</if>
<if test="sex != null">#{sex}</if>
</trim>
</insert>
數(shù)據(jù)添加成功,依舊只添加了姓名。
where標簽
根據(jù)屬性做 where 條件查詢,?戶對象中屬性不為 null 的,都為查詢條件。
寫一個查詢方法:
@RequestMapping("selectByCondition")
public List<User> selectByCondition(User user){
return userService.selectByCondition(user);
}
xml代碼如下:
</insert>
<select id="selectByCondition" parameterType="com.example.mybatisdemo.model.User" >
select id,name,sex from stu
<where>
<if test="name != null "> and name=#{name} </if>
<if test=" sex != null"> and sex= #{sex} </if>
</where>
</select>
根據(jù)name進行查詢
<where>標簽也可以使? <trim prefix="where" prefixOverrides="and"> 替換。
set標簽
根據(jù)傳?的?戶對象屬性來更新?戶數(shù)據(jù),可以使?set標簽來指定動態(tài)內(nèi)容。
UserMapper 接?中修改?戶?法:根據(jù)傳?的?戶 id 屬性,修改其他不為 null 的屬性:
int updateById(User user);
在xml中添加更新?戶 sql:
<update id="updateById" parameterType="com.example.mybatisdemo.model.User" >
update stu
<set>
<if test="name != null">
name=#{name},
</if>
<if test="sex != null">
sex=#{sex},
</if>
</set>
where id=#{id}
</update>
查看數(shù)據(jù)庫發(fā)現(xiàn)修改成功
<set>標簽也可以使? <trim prefix="set" suffixOverrides=","> 替換
foreach標簽
對集合進?遍歷時可以使?該標簽。foreach標簽有如下屬性:
- collection:綁定?法參數(shù)中的集合,如 List,Set,Map或數(shù)組對象
- item:遍歷時的每?個對象
- open:語句塊開頭的字符串
- close:語句塊結(jié)束的字符串
- separator:用于指定循環(huán)迭代時元素之間的分隔符
例如一次性刪除多個id對應(yīng)的用戶
ArticleMapper 中新增接??法:
int deleteByIds(List<Integer> ids);
UserMapper.xml 中新增刪除 sql:
<delete id="deleteByIds">
delete from stu
where id in
<foreach collection="list" item="item" open="(" close=")" separator=","
>
#{item}
</foreach>
</delete>
我們刪除id為1,2,3的用戶信息
通過查看數(shù)據(jù)庫我們知道,刪除已經(jīng)成功。文章來源:http://www.zghlxwxcb.cn/news/detail-654538.html
以上就是我記錄的一些動態(tài)SQL,如果想了解更多可前往mybatis官網(wǎng)查看 。文章來源地址http://www.zghlxwxcb.cn/news/detail-654538.html
到了這里,關(guān)于MyBatis動態(tài)SQL:打造靈活可變的數(shù)據(jù)庫操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!