mybatis動(dòng)態(tài)sql之新增與更新方式
mybatis動(dòng)態(tài)sql新增與更新
記錄一個(gè)簡(jiǎn)單的mybatis動(dòng)態(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動(dòng)態(tài)SQL增刪改查
我們?cè)趯?duì)數(shù)據(jù)庫(kù)進(jìn)行增刪改查的時(shí)候,很多時(shí)候我們并不確定我們要進(jìn)行傳入的參數(shù)的個(gè)數(shù),種類以及是否為空。
此時(shí)我們就需要用到mybatis動(dòng)態(tài)sql來(lái)對(duì)數(shù)據(jù)庫(kù)進(jìn)行靈活的交互。
- 步驟一:導(dǎo)入相關(guān)jar包,編寫(xiě)連接數(shù)據(jù)庫(kù)的MybatisUtil工具類
- 步驟二:在src下配置mybatis.xml配置文件。其中對(duì)數(shù)據(jù)庫(kù)連接,映射文件的加載進(jìn)行配置。(簡(jiǎn)寫(xiě)配置可選)
- 步驟三:建立實(shí)體類Student
- 步驟四:增刪改查的方法以及映射文件StudentMapper.xml中配置的編寫(xiě)。
添加數(shù)據(jù)
insert 對(duì)應(yīng)的映射文件中配置:
通過(guò)傳入數(shù)組參數(shù)刪除
deleteArray對(duì)應(yīng)的映射文件中配置:
通過(guò)傳入List集合參數(shù)進(jìn)行刪除
deleteList 對(duì)應(yīng)的映射文件中配置:
更新數(shù)據(jù)
update 對(duì)應(yīng)的映射文件中配置:
神奇的是:
查找數(shù)據(jù)
findAll對(duì)應(yīng)的映射文件配置
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- MyBatis實(shí)現(xiàn)動(dòng)態(tài)SQL的方法
- Mybatis之動(dòng)態(tài)SQL使用小結(jié)(全網(wǎng)最新)
- Mybatis動(dòng)態(tài)Sql標(biāo)簽使用小結(jié)
- MyBatis映射文件中的動(dòng)態(tài)SQL實(shí)例詳解
- 詳解MyBatis特性之動(dòng)態(tài)SQL
- Mybatis使用XML實(shí)現(xiàn)動(dòng)態(tài)sql的示例代碼
- Mybatis中的@Param及動(dòng)態(tài)SQL詳解
- Mybatis動(dòng)態(tài)sql中@Param使用詳解
- MyBatis中實(shí)現(xiàn)動(dòng)態(tài)SQL標(biāo)簽
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)BFS廣搜法解決迷宮問(wèn)題
廣搜BFS的基本思想是: 首先訪問(wèn)初始點(diǎn)v并將其標(biāo)志為已經(jīng)訪問(wèn)。接著通過(guò)鄰接關(guān)系將鄰接點(diǎn)入隊(duì)。然后每訪問(wèn)過(guò)一個(gè)頂點(diǎn)則出隊(duì)。按照順序,訪問(wèn)每一個(gè)頂點(diǎn)的所有未被訪問(wèn)過(guò)的頂點(diǎn)直到所有的頂點(diǎn)均被訪問(wèn)過(guò)。廣度優(yōu)先遍歷類似與層次遍歷2022-04-04IDEA插件之mybatisx插件使用教程(超詳細(xì)!)
MybatisX 是一款基于IDEA的快速開(kāi)發(fā)插件,為效率而生,下面這篇文章主要給大家介紹了關(guān)于IDEA插件之mybatisx插件使用的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06Java編程中的vector類用法學(xué)習(xí)筆記
Vector通常被用來(lái)實(shí)現(xiàn)動(dòng)態(tài)數(shù)組,即可實(shí)現(xiàn)自動(dòng)增長(zhǎng)的對(duì)象數(shù)組,和C++一樣vector類同樣被Java內(nèi)置,下面就來(lái)看一下vector類的基本用法.2016-05-05Java 后端開(kāi)發(fā)中Tomcat服務(wù)器運(yùn)行不了的五種解決方案
tomcat是在使用Java編程語(yǔ)言開(kāi)發(fā)服務(wù)端技術(shù)使用最廣泛的服務(wù)器之一,但經(jīng)常在開(kāi)發(fā)項(xiàng)目的時(shí)候會(huì)出現(xiàn)運(yùn)行不了的情況,這里總結(jié)出幾種能解決的辦法2021-10-10Java使用poi生成word文檔的簡(jiǎn)單實(shí)例
Java POI是一個(gè)用于處理Microsoft Office文件(如Word、Excel和PowerPoint)的API,它是一個(gè)開(kāi)源庫(kù),允許Java開(kāi)發(fā)者讀取、創(chuàng)建和修改這些文檔,本文給大集介紹了Java使用poi生成word文檔的簡(jiǎn)單實(shí)例,感興趣的朋友可以參考下2024-06-06java實(shí)現(xiàn)數(shù)據(jù)庫(kù)主鍵生成示例
這篇文章主要介紹了java實(shí)現(xiàn)數(shù)據(jù)庫(kù)主鍵生成示例,需要的朋友可以參考下2014-03-03