MyBatis中的XML實(shí)現(xiàn)和動(dòng)態(tài)SQL實(shí)現(xiàn)示例詳解
一、XML實(shí)現(xiàn)
先在新建的XML文件中寫入如下內(nèi)容:
<?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="com.example.demo.mapper.UserInfoMapper"> </mapper>
再在mapper標(biāo)簽里寫入操作數(shù)據(jù)庫的增刪查改。
1.1增
mapper層聲明的方法為:
Integer insert(UserInfo userInfo);
XML文件中的實(shí)現(xiàn)為:
<insert id = "insert"> insert into userinfo (username, password, age, gender, phone) values (#{username}, #{password}, #{age}, #{gender}, #{phone}) </insert>
1.2刪
mapper層聲明的方法為:
Integer delete(Integer id);
XML文件中的實(shí)現(xiàn)為:
<delete id="delete"> delete from userinfo where id = #{id} </delete>
1.3查
mapper層聲明的方法為:
List<UserInfo> queryUserList();
XML文件中的實(shí)現(xiàn)為:
<select id="queryUserList" resultType="com.example.demo.model.UserInfo"> select * from userinfo </select>
1.4改
mapper層聲明的方法為:
Integer update(UserInfo userInfo);
XML文件中的實(shí)現(xiàn)為:
<update id="update"> update userinfo set password = #{password} where id = #{id} </update>
二、XML方式實(shí)現(xiàn)動(dòng)態(tài)SQL
2.1if標(biāo)簽
使用示例:
<update id = "updateBook"> update book_info <set> <if test = "bookName != null"> book_name = #{bookName}, </if> <if test = "author != null"> author = #{author}, </if> <if test = "count != null"> count = #{count}, </if> <if test = "price != null"> price = #{price}, </if> <if test = "publish != null"> publish = #{publish}, </if> <if test = "status != null"> status = #{status}, </if> </set> where id = #{id} </update>
如果滿足bookName!=null這個(gè)條件,則會(huì)顯示if標(biāo)簽里的內(nèi)容。
2.2trim標(biāo)簽
使用示例:
<insert id="insert2" useGeneratedKeys="true" keyProperty="id"> insert into userinfo <trim prefixOverrides="," prefix="(" suffix=")" suffixOverrides=","> <if test="username!=null"> username, </if> <if test="password!=null"> password, </if> <if test="age!=null"> age, </if> <if test="gender!=null"> gender, </if> <if test="phone!=null"> phone, </if> </trim> values <trim prefixOverrides="," prefix="(" suffix=")" suffixOverrides=","> <if test="username!=null"> #{username}, </if> <if test="password!=null"> #{password}, </if> <if test="age!=null"> #{age}, </if> <if test="gender!=null"> #{gender}, </if> <if test="phone!=null"> #{phone}, </if> </trim> </insert>
2.3where標(biāo)簽
使用示例:
<select id="queryUserByWhere" resultType="com.yixing.mybatis.model.UserInfo"> select * from userinfo <where> <if test="userName!=null"> username= #{userName} </if> <if test="age!=null"> and age=#{age} </if> </where> </select>
where標(biāo)簽的作用是刪除代碼塊最前面的and;當(dāng)查詢條件為空時(shí),會(huì)去掉where關(guān)鍵字。
2.4set標(biāo)簽
使用示例:
<update id="update2"> update userinfo <set> <if test="username!=null"> username = #{username}, </if> <if test="password!=null"> password = #{password}, </if> <if test="age!=null"> age = #{age} </if> </set> where id = #{id} </update>
set標(biāo)簽會(huì)刪除代碼塊最后面的逗號(hào)。
2.5foreach標(biāo)簽
使用示例:
<update id="batchDelete"> update book_info set `status` = 0 where id in <foreach collection="ids" open="(" close=")" separator="," item="id"> #{id} </foreach> </update>
默認(rèn)情況下,如果mapper層聲明方法的參數(shù)是List類型,則foreach標(biāo)簽里的collection會(huì)等于"list";如果mapper層聲明方法的參數(shù)是數(shù)組類型,則foreach標(biāo)簽里的collection會(huì)等于"array",這時(shí)mybatis自動(dòng)做的。我們可以在mapper層聲明方法中用@Param注解對(duì)聲明方法的參數(shù)進(jìn)行重命名。
2.6include標(biāo)簽和sql標(biāo)簽
<sql id="cols"> id, username,password,gender,age,phone, </sql> <select id="queryUserList" resultType="com.yixing.mybatis.model.UserInfo"> select <include refid="cols"></include> delete_flag, create_time, update_time from userinfo </select>
我們可以將XML中重復(fù)出現(xiàn)的內(nèi)容提取出來放到sql標(biāo)簽中,當(dāng)需要用到sql標(biāo)簽中的內(nèi)容時(shí),用include標(biāo)簽將sql標(biāo)簽中的內(nèi)容引進(jìn)來即可。
到此這篇關(guān)于MyBatis中的XML實(shí)現(xiàn)和動(dòng)態(tài)SQL實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis XML和動(dòng)態(tài)SQL內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis中動(dòng)態(tài)SQL的使用指南
- MyBatis中實(shí)現(xiàn)動(dòng)態(tài)SQL標(biāo)簽
- 使用MyBatis的動(dòng)態(tài)SQL注解實(shí)現(xiàn)實(shí)體的CRUD操作代碼
- 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使用注解實(shí)現(xiàn)復(fù)雜動(dòng)態(tài)SQL的方法詳解
- MyBatis的動(dòng)態(tài)攔截sql并修改
- mybatis動(dòng)態(tài)生成sql語句的實(shí)現(xiàn)示例
相關(guān)文章
詳解Spring連接數(shù)據(jù)庫的幾種常用的方式
本篇文章主要介紹了Spring連接數(shù)據(jù)庫的幾種常用的方式,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12解決Maven項(xiàng)目pom.xml導(dǎo)入了Junit包還是用不了@Test注解問題
在Maven項(xiàng)目中,如果在非test目錄下使用@Test注解,可能會(huì)因?yàn)閜om.xml中<scope>test</scope>的設(shè)置而無法使用,正確做法是將測試代碼放在src/test/java目錄下,或去除<scope>test</scope>限制,這樣可以確保Junit依賴正確加載并應(yīng)用于適當(dāng)?shù)拇a部分2024-10-10Java中構(gòu)造方法set/get和toString的使用詳解
這篇文章主要介紹了Java中構(gòu)造方法set/get和toString的使用詳解,構(gòu)造函數(shù)的最大作用就是創(chuàng)建對(duì)象時(shí)完成初始化,當(dāng)我們?cè)趎ew一個(gè)對(duì)象并傳入?yún)?shù)的時(shí)候,會(huì)自動(dòng)調(diào)用構(gòu)造函數(shù)并完成參數(shù)的初始化,需要的朋友可以參考下2019-07-07java操作json對(duì)象出現(xiàn)StackOverflow錯(cuò)誤的問題及解決
這篇文章主要介紹了java操作json對(duì)象出現(xiàn)StackOverflow錯(cuò)誤的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Mybatis之解決collection一對(duì)多問題(顯示的結(jié)果沒有整合到一起)
這篇文章主要介紹了Mybatis之解決collection一對(duì)多問題(顯示的結(jié)果沒有整合到一起),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼
這篇文章主要介紹了springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12SpringBoot定時(shí)任務(wù)詳解與案例代碼
SpringBoot是一個(gè)流行的Java開發(fā)框架,它提供了許多便捷的特性來簡化開發(fā)過程,其中之一就是定時(shí)任務(wù)的支持,讓開發(fā)人員可以輕松地在應(yīng)用程序中執(zhí)行定時(shí)任務(wù),本文將詳細(xì)介紹如何在Spring?Boot中使用定時(shí)任務(wù),并提供相關(guān)的代碼示例2023-06-06