MyBatis中的XML實現(xiàn)和動態(tài)SQL實現(xiàn)示例詳解
一、XML實現(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文件中的實現(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文件中的實現(xiàn)為:
<delete id="delete">
delete from userinfo where id = #{id}
</delete>1.3查
mapper層聲明的方法為:
List<UserInfo> queryUserList();
XML文件中的實現(xiàn)為:
<select id="queryUserList" resultType="com.example.demo.model.UserInfo">
select * from userinfo
</select>1.4改
mapper層聲明的方法為:
Integer update(UserInfo userInfo);
XML文件中的實現(xiàn)為:
<update id="update">
update userinfo
set password = #{password}
where id = #{id}
</update>二、XML方式實現(xiàn)動態(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這個條件,則會顯示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)查詢條件為空時,會去掉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)簽會刪除代碼塊最后面的逗號。
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會等于"list";如果mapper層聲明方法的參數(shù)是數(shù)組類型,則foreach標(biāo)簽里的collection會等于"array",這時mybatis自動做的。我們可以在mapper層聲明方法中用@Param注解對聲明方法的參數(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)容時,用include標(biāo)簽將sql標(biāo)簽中的內(nèi)容引進(jìn)來即可。
到此這篇關(guān)于MyBatis中的XML實現(xiàn)和動態(tài)SQL實現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis XML和動態(tài)SQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis中動態(tài)SQL的使用指南
- MyBatis中實現(xiàn)動態(tài)SQL標(biāo)簽
- 使用MyBatis的動態(tài)SQL注解實現(xiàn)實體的CRUD操作代碼
- MyBatis實現(xiàn)動態(tài)SQL的方法
- Mybatis之動態(tài)SQL使用小結(jié)(全網(wǎng)最新)
- Mybatis動態(tài)Sql標(biāo)簽使用小結(jié)
- MyBatis映射文件中的動態(tài)SQL實例詳解
- 詳解MyBatis特性之動態(tài)SQL
- Mybatis使用注解實現(xiàn)復(fù)雜動態(tài)SQL的方法詳解
- MyBatis的動態(tài)攔截sql并修改
- mybatis動態(tài)生成sql語句的實現(xiàn)示例
相關(guān)文章
關(guān)于ObjectUtils.isEmpty()?和?null?的區(qū)別
這篇文章主要介紹了關(guān)于ObjectUtils.isEmpty()?和?null?的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
基于@ConfigurationProperties的三種用法及說明
SpringBoot配置屬性綁定詳解,介紹@Component若您配置類、@ConfigurationProperties綁定屬性及三種常見應(yīng)用場景,附帶代碼示例與測試步驟2026-06-06
mybaties plus實體類設(shè)置typeHandler不生效的解決
這篇文章主要介紹了mybaties plus實體類設(shè)置typeHandler不生效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
Java應(yīng)用CPU占用過高問題的快速定位和解決方法
本文介紹了快速定位和解決Java應(yīng)用CPU占用過高問題的標(biāo)準(zhǔn)流程,并通過一個實際案例演示,核心思想是通過進(jìn)程、線程、線程棧和源代碼的逐步排查,最終定位到問題代碼,需要的朋友可以參考下2025-11-11
Mybatis攔截器如何實現(xiàn)數(shù)據(jù)權(quán)限過濾
本文介紹了MyBatis攔截器的使用,通過實現(xiàn)Interceptor接口對SQL進(jìn)行處理,實現(xiàn)數(shù)據(jù)權(quán)限過濾功能,通過在本地線程變量中存儲數(shù)據(jù)權(quán)限相關(guān)信息,并在攔截器的intercept方法中進(jìn)行SQL增強(qiáng)處理2024-12-12
SpringCloud Gateway中斷言路由和過濾器的使用詳解
這篇文章主要介紹了SpringCloud Gateway中斷言路由和過濾器的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
Java中實現(xiàn)定時任務(wù)的兩種方法舉例詳解
這篇文章主要給大家介紹了關(guān)于Java中實現(xiàn)定時任務(wù)的兩種方法,文中總結(jié)了各種實現(xiàn)方式的優(yōu)缺點(diǎn),并給出了推薦的使用場景,通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12

