亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

MyBatis中的XML實(shí)現(xiàn)和動(dòng)態(tài)SQL實(shí)現(xiàn)示例詳解

 更新時(shí)間:2024年02月18日 10:15:20   作者:zhanlongsiqu  
這篇文章主要介紹了MyBatis中的XML實(shí)現(xiàn)和動(dòng)態(tài)SQL實(shí)現(xiàn),我們可以將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)來即可,感興趣的朋友跟隨小編一起看看吧

一、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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Spring連接數(shù)據(jù)庫的幾種常用的方式

    詳解Spring連接數(shù)據(jù)庫的幾種常用的方式

    本篇文章主要介紹了Spring連接數(shù)據(jù)庫的幾種常用的方式,具有一定的參考價(jià)值,有需要的可以了解一下。
    2016-12-12
  • Java 之類型轉(zhuǎn)換與多態(tài)詳情

    Java 之類型轉(zhuǎn)換與多態(tài)詳情

    Java使用類創(chuàng)造新的類型(type),并使用繼承來便利我們創(chuàng)建類。再深一層講類型,并是多態(tài)(polymorphism)的概念。本文將給大家介紹Java 的類型轉(zhuǎn)換與多態(tài),需要的小伙伴可以參考下面文章的具體內(nèi)容
    2021-09-09
  • 詳解Java中wait和sleep的區(qū)別

    詳解Java中wait和sleep的區(qū)別

    這篇文章主要介紹了Java中wait和sleep的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 解決Maven項(xiàng)目pom.xml導(dǎo)入了Junit包還是用不了@Test注解問題

    解決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-10
  • Java中構(gòu)造方法set/get和toString的使用詳解

    Java中構(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-07
  • java操作json對(duì)象出現(xiàn)StackOverflow錯(cuò)誤的問題及解決

    java操作json對(duì)象出現(xiàn)StackOverflow錯(cuò)誤的問題及解決

    這篇文章主要介紹了java操作json對(duì)象出現(xiàn)StackOverflow錯(cuò)誤的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Mybatis之解決collection一對(duì)多問題(顯示的結(jié)果沒有整合到一起)

    Mybatis之解決collection一對(duì)多問題(顯示的結(jié)果沒有整合到一起)

    這篇文章主要介紹了Mybatis之解決collection一對(duì)多問題(顯示的結(jié)果沒有整合到一起),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Java全面深入探究SpringBoot攔截器與文件上傳

    Java全面深入探究SpringBoot攔截器與文件上傳

    攔截器對(duì)使用SpringMvc、Struts的開發(fā)人員來說特別熟悉,因?yàn)槟阒灰肴プ龊靡粋€(gè)項(xiàng)目必然會(huì)用到它,文件上傳是一個(gè)很常見的功能。在項(xiàng)目開發(fā)過程中,我們通常都會(huì)使用一些成熟的上傳組件來實(shí)現(xiàn)對(duì)應(yīng)的功能
    2022-05-05
  • springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼

    springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼

    這篇文章主要介紹了springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • SpringBoot定時(shí)任務(wù)詳解與案例代碼

    SpringBoot定時(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

最新評(píng)論