mybatis動態(tài)sql之新增與更新方式
mybatis動態(tài)sql新增與更新
記錄一個簡單的mybatis動態(tài)sql例子
新增
<?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="org.ruiskey.mapper.UserMapper">
? ? <insert id="save">
? ? ? ? insert into user
? ? ? ? ? ? <trim prefix="(" suffix=")" suffixOverrides=",">
? ? ? ? ? ? ? ? <if test="id != null and id != ''">
? ? ? ? ? ? ? ? ? ? id,
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="username != null and username != ''">
? ? ? ? ? ? ? ? ? ? username,
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="password != null and password != ''">
? ? ? ? ? ? ? ? ? ? password,
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="nickname != null and nickname != ''">
? ? ? ? ? ? ? ? ? ? nickname,
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="email != null and email != ''">
? ? ? ? ? ? ? ? ? ? email,
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="phone != null and phone != ''">
? ? ? ? ? ? ? ? ? ? phone,
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="address != null and address != ''">
? ? ? ? ? ? ? ? ? ? address
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? </trim>
? ? ? ? ? ? <trim prefix="values (" suffix=")" suffixOverrides=",">
? ? ? ? ? ? ? ? <if test="id != null and id != ''">
? ? ? ? ? ? ? ? ? ? #{id},
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="username != null and username != ''">
? ? ? ? ? ? ? ? ? ? #{username},
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="password != null and password != ''">
? ? ? ? ? ? ? ? ? ? #{password},
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="nickname != null and nickname != ''">
? ? ? ? ? ? ? ? ? ? #{nickname},
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="email != null and email != ''">
? ? ? ? ? ? ? ? ? ? #{email},
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="phone != null and phone != ''">
? ? ? ? ? ? ? ? ? ? #{phone},
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="address != null and address != ''">
? ? ? ? ? ? ? ? ? ? #{address}
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? </trim>
? ? </insert>
</mapper>更新
<update id="update">
? ? update user
? ? <set>
? ? ? ? <if test="username != null and username != ''">
? ? ? ? ? ? username = #{username}
? ? ? ? </if>
? ? ? ? <if test="password != null and password != ''">
? ? ? ? ? ? password = #{password}
? ? ? ? </if>
? ? ? ? <if test="nickname != null and nickname != ''">
? ? ? ? ? ? nickname = #{nickname}
? ? ? ? </if>
? ? ? ? <if test="email != null and email != ''">
? ? ? ? ? ? email = #{email}
? ? ? ? </if>
? ? ? ? <if test="phone != null and phone != ''">
? ? ? ? ? ? phone = #{phone}
? ? ? ? </if>
? ? ? ? <if test="address != null and address != ''">
? ? ? ? ? ? address = #{address}
? ? ? ? </if>
? ? </set>
? ? <where>
? ? ? ? id = #{id}
? ? </where>
</update>mybatis動態(tài)SQL增刪改查
我們在對數(shù)據(jù)庫進行增刪改查的時候,很多時候我們并不確定我們要進行傳入的參數(shù)的個數(shù),種類以及是否為空。
此時我們就需要用到mybatis動態(tài)sql來對數(shù)據(jù)庫進行靈活的交互。
- 步驟一:導(dǎo)入相關(guān)jar包,編寫連接數(shù)據(jù)庫的MybatisUtil工具類
- 步驟二:在src下配置mybatis.xml配置文件。其中對數(shù)據(jù)庫連接,映射文件的加載進行配置。(簡寫配置可選)
- 步驟三:建立實體類Student

- 步驟四:增刪改查的方法以及映射文件StudentMapper.xml中配置的編寫。
添加數(shù)據(jù)

insert 對應(yīng)的映射文件中配置:



通過傳入數(shù)組參數(shù)刪除

deleteArray對應(yīng)的映射文件中配置:

通過傳入List集合參數(shù)進行刪除

deleteList 對應(yīng)的映射文件中配置:


更新數(shù)據(jù)

update 對應(yīng)的映射文件中配置:

神奇的是:

查找數(shù)據(jù)

findAll對應(yīng)的映射文件配置

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot集成Drools打造動態(tài)規(guī)則管理模板引擎
本文詳解SpringBoot與Drools的整合方法,涵蓋依賴管理、規(guī)則文件編寫、KieService配置等,旨在通過規(guī)則引擎實現(xiàn)業(yè)務(wù)邏輯動態(tài)化管理,提升微服務(wù)應(yīng)用的靈活性、可維護性及快速部署能力,對SpringBoot Drools相關(guān)知識感興趣的朋友一起看看吧2025-07-07
springboot?ConfigurationProperties的綁定源碼示例解析
這篇文章主要為大家介紹了springboot?ConfigurationProperties的綁定源碼示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
淺談springboot之JoinPoint的getSignature方法
這篇文章主要介紹了springboot之JoinPoint的getSignature方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
PowerDesigner連接數(shù)據(jù)庫的實例詳解
這篇文章主要介紹了PowerDesigner連接數(shù)據(jù)庫的實例詳解的相關(guān)資料,如有疑問請留言或者到本站社區(qū)交流討論,需要的朋友可以參考下2017-10-10
springboot之@KafkaListener注解自動裝配流程分享
@KafkaListener是Spring Kafka框架提供的注解,Spring Boot對其提供了原生支持,通過引入依賴并在配置文件中加入Kafka相關(guān)配置,可以觸發(fā)Kafka組件的自動狀態(tài),掃描@KafkaListener注解是Spring Boot自動裝配的一部分2026-01-01

