亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

mybatis動態(tài)sql之新增與更新方式

 更新時間:2023年07月17日 09:55:10   作者:某猿蚊常叮  
這篇文章主要介紹了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)文章

  • Java數(shù)據(jù)結(jié)構(gòu)BFS廣搜法解決迷宮問題

    Java數(shù)據(jù)結(jié)構(gòu)BFS廣搜法解決迷宮問題

    廣搜BFS的基本思想是: 首先訪問初始點v并將其標志為已經(jīng)訪問。接著通過鄰接關(guān)系將鄰接點入隊。然后每訪問過一個頂點則出隊。按照順序,訪問每一個頂點的所有未被訪問過的頂點直到所有的頂點均被訪問過。廣度優(yōu)先遍歷類似與層次遍歷
    2022-04-04
  • IDEA插件之mybatisx插件使用教程(超詳細!)

    IDEA插件之mybatisx插件使用教程(超詳細!)

    MybatisX 是一款基于IDEA的快速開發(fā)插件,為效率而生,下面這篇文章主要給大家介紹了關(guān)于IDEA插件之mybatisx插件使用的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-06-06
  • Java編程中的vector類用法學(xué)習(xí)筆記

    Java編程中的vector類用法學(xué)習(xí)筆記

    Vector通常被用來實現(xiàn)動態(tài)數(shù)組,即可實現(xiàn)自動增長的對象數(shù)組,和C++一樣vector類同樣被Java內(nèi)置,下面就來看一下vector類的基本用法.
    2016-05-05
  • sharding-jdbc中的事務(wù)詳細解讀

    sharding-jdbc中的事務(wù)詳細解讀

    這篇文章主要介紹了sharding-jdbc中的事務(wù)詳細解讀,sharding-jdbc在分庫分表方面提供了很大的便利性,在使用DB的時候,通常都會涉及到事務(wù)這個概念,而在分庫分表的環(huán)境上再加上事務(wù),就會使事情變得復(fù)雜起來,需要的朋友可以參考下
    2023-12-12
  • Java 后端開發(fā)中Tomcat服務(wù)器運行不了的五種解決方案

    Java 后端開發(fā)中Tomcat服務(wù)器運行不了的五種解決方案

    tomcat是在使用Java編程語言開發(fā)服務(wù)端技術(shù)使用最廣泛的服務(wù)器之一,但經(jīng)常在開發(fā)項目的時候會出現(xiàn)運行不了的情況,這里總結(jié)出幾種能解決的辦法
    2021-10-10
  • Java使用poi生成word文檔的簡單實例

    Java使用poi生成word文檔的簡單實例

    Java POI是一個用于處理Microsoft Office文件(如Word、Excel和PowerPoint)的API,它是一個開源庫,允許Java開發(fā)者讀取、創(chuàng)建和修改這些文檔,本文給大集介紹了Java使用poi生成word文檔的簡單實例,感興趣的朋友可以參考下
    2024-06-06
  • 淺談Java注解和動態(tài)代理

    淺談Java注解和動態(tài)代理

    這篇文章主要介紹了Java中有關(guān)注解和動態(tài)代理的一些知識,涉及了Annotation、數(shù)據(jù)類型等相關(guān)內(nèi)容,需要的朋友可以參考下。
    2017-09-09
  • springcloud本地配置優(yōu)先方式

    springcloud本地配置優(yōu)先方式

    這篇文章主要介紹了springcloud本地配置優(yōu)先方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • java實現(xiàn)數(shù)據(jù)庫主鍵生成示例

    java實現(xiàn)數(shù)據(jù)庫主鍵生成示例

    這篇文章主要介紹了java實現(xiàn)數(shù)據(jù)庫主鍵生成示例,需要的朋友可以參考下
    2014-03-03
  • Java多線程通信實現(xiàn)方式詳解

    Java多線程通信實現(xiàn)方式詳解

    這篇文章主要介紹了Java多線程通信實現(xiàn)方式詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11

最新評論