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ù)庫進行靈活的交互。
- 步驟一:導入相關jar包,編寫連接數(shù)據(jù)庫的MybatisUtil工具類
- 步驟二:在src下配置mybatis.xml配置文件。其中對數(shù)據(jù)庫連接,映射文件的加載進行配置。(簡寫配置可選)
- 步驟三:建立實體類Student
- 步驟四:增刪改查的方法以及映射文件StudentMapper.xml中配置的編寫。
添加數(shù)據(jù)
insert 對應的映射文件中配置:
通過傳入數(shù)組參數(shù)刪除
deleteArray對應的映射文件中配置:
通過傳入List集合參數(shù)進行刪除
deleteList 對應的映射文件中配置:
更新數(shù)據(jù)
update 對應的映射文件中配置:
神奇的是:
查找數(shù)據(jù)
findAll對應的映射文件配置
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java 后端開發(fā)中Tomcat服務器運行不了的五種解決方案
tomcat是在使用Java編程語言開發(fā)服務端技術使用最廣泛的服務器之一,但經(jīng)常在開發(fā)項目的時候會出現(xiàn)運行不了的情況,這里總結出幾種能解決的辦法2021-10-10java實現(xiàn)數(shù)據(jù)庫主鍵生成示例
這篇文章主要介紹了java實現(xiàn)數(shù)據(jù)庫主鍵生成示例,需要的朋友可以參考下2014-03-03