MyBatis中的XML實現和動態(tài)SQL實現示例詳解
一、XML實現
先在新建的XML文件中寫入如下內容:
<?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標簽里寫入操作數據庫的增刪查改。
1.1增
mapper層聲明的方法為:
Integer insert(UserInfo userInfo);
XML文件中的實現為:
<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文件中的實現為:
<delete id="delete"> delete from userinfo where id = #{id} </delete>
1.3查
mapper層聲明的方法為:
List<UserInfo> queryUserList();
XML文件中的實現為:
<select id="queryUserList" resultType="com.example.demo.model.UserInfo"> select * from userinfo </select>
1.4改
mapper層聲明的方法為:
Integer update(UserInfo userInfo);
XML文件中的實現為:
<update id="update"> update userinfo set password = #{password} where id = #{id} </update>
二、XML方式實現動態(tài)SQL
2.1if標簽
使用示例:
<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標簽里的內容。
2.2trim標簽
使用示例:
<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標簽
使用示例:
<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標簽的作用是刪除代碼塊最前面的and;當查詢條件為空時,會去掉where關鍵字。
2.4set標簽
使用示例:
<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標簽會刪除代碼塊最后面的逗號。
2.5foreach標簽
使用示例:
<update id="batchDelete"> update book_info set `status` = 0 where id in <foreach collection="ids" open="(" close=")" separator="," item="id"> #{id} </foreach> </update>
默認情況下,如果mapper層聲明方法的參數是List類型,則foreach標簽里的collection會等于"list";如果mapper層聲明方法的參數是數組類型,則foreach標簽里的collection會等于"array",這時mybatis自動做的。我們可以在mapper層聲明方法中用@Param注解對聲明方法的參數進行重命名。
2.6include標簽和sql標簽
<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中重復出現的內容提取出來放到sql標簽中,當需要用到sql標簽中的內容時,用include標簽將sql標簽中的內容引進來即可。
到此這篇關于MyBatis中的XML實現和動態(tài)SQL實現的文章就介紹到這了,更多相關MyBatis XML和動態(tài)SQL內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決Maven項目pom.xml導入了Junit包還是用不了@Test注解問題
在Maven項目中,如果在非test目錄下使用@Test注解,可能會因為pom.xml中<scope>test</scope>的設置而無法使用,正確做法是將測試代碼放在src/test/java目錄下,或去除<scope>test</scope>限制,這樣可以確保Junit依賴正確加載并應用于適當的代碼部分2024-10-10Java中構造方法set/get和toString的使用詳解
這篇文章主要介紹了Java中構造方法set/get和toString的使用詳解,構造函數的最大作用就是創(chuàng)建對象時完成初始化,當我們在new一個對象并傳入參數的時候,會自動調用構造函數并完成參數的初始化,需要的朋友可以參考下2019-07-07java操作json對象出現StackOverflow錯誤的問題及解決
這篇文章主要介紹了java操作json對象出現StackOverflow錯誤的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06Mybatis之解決collection一對多問題(顯示的結果沒有整合到一起)
這篇文章主要介紹了Mybatis之解決collection一對多問題(顯示的結果沒有整合到一起),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03