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廣搜法解決迷宮問題
廣搜BFS的基本思想是: 首先訪問初始點v并將其標志為已經(jīng)訪問。接著通過鄰接關(guān)系將鄰接點入隊。然后每訪問過一個頂點則出隊。按照順序,訪問每一個頂點的所有未被訪問過的頂點直到所有的頂點均被訪問過。廣度優(yōu)先遍歷類似與層次遍歷2022-04-04Java編程中的vector類用法學(xué)習(xí)筆記
Vector通常被用來實現(xiàn)動態(tài)數(shù)組,即可實現(xiàn)自動增長的對象數(shù)組,和C++一樣vector類同樣被Java內(nèi)置,下面就來看一下vector類的基本用法.2016-05-05Java 后端開發(fā)中Tomcat服務(wù)器運行不了的五種解決方案
tomcat是在使用Java編程語言開發(fā)服務(wù)端技術(shù)使用最廣泛的服務(wù)器之一,但經(jīng)常在開發(fā)項目的時候會出現(xiàn)運行不了的情況,這里總結(jié)出幾種能解決的辦法2021-10-10java實現(xiàn)數(shù)據(jù)庫主鍵生成示例
這篇文章主要介紹了java實現(xiàn)數(shù)據(jù)庫主鍵生成示例,需要的朋友可以參考下2014-03-03