国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Mybatis 動態(tài)SQL條件查詢(注釋和XML方式都有)

這篇具有很好參考價值的文章主要介紹了Mybatis 動態(tài)SQL條件查詢(注釋和XML方式都有)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

 

需求 : 根據(jù)用戶的輸入情況進行條件查詢

新建了一個 userInfo2Mapper 接口,然后寫下如下代碼,聲明 selectByCondition 這個方法

package com.example.mybatisdemo.mapper;
import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface UserInfo2Mapper {
   
    List<UserInfo> selectByCondition(UserInfo userInfo);
}

我們先用XML的方式實現(xiàn)

在resources 中創(chuàng)建 Userinfo2XMLMapper.xml 文件

?將 Userinfo2XMLMapper.xml 文件中的 namespace 進行修改,改為 userInfo2Mapper 接口中的第一行 package 的內(nèi)容再加上接口名

然后補充如下代碼,用戶輸入什么條件,什么條件就不為空,就可以根據(jù)該條件進行查詢

<?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.mybatisdemo.mapper.UserInfo2Mapper">
    <select id="selectByCondition" resultType="com.example.mybatisdemo.model.UserInfo">
        select * from userinfo
        where
        <if test="username!=null">
            username = #{username}
        </if>
        <if test="age!=null">
            and age = #{age}
        </if>
        <if test="gender!=null">
            and gender = #{gender}
        </if>
    </select>
</mapper>

再回到接口,然后Generate,test,勾選selectByCondition,ok

然后補充代碼

package com.example.mybatisdemo.mapper;
import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;

@Slf4j
@SpringBootTest
class UserInfo2MapperTest {
    @Autowired
    private UserInfo2Mapper userInfo2Mapper;
  
    @Test
    void selectByCondition() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("io");
        userInfo.setAge(23);
        userInfo.setGender(0);
        List<UserInfo> userInfos = userInfo2Mapper.selectByCondition(userInfo);
        log.info(userInfos.toString());
    }
}

然后我的數(shù)據(jù)庫里面是有這些數(shù)據(jù)的Mybatis 動態(tài)SQL條件查詢(注釋和XML方式都有),mybatis,sql,windows

接下來我們執(zhí)行看看結(jié)果?,沒毛病

Mybatis 動態(tài)SQL條件查詢(注釋和XML方式都有),mybatis,sql,windows

?接下來我們對代碼稍作修改,我們不 set gender了,再看看運行結(jié)果

package com.example.mybatisdemo.mapper;
import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;

@Slf4j
@SpringBootTest
class UserInfo2MapperTest {
    @Autowired
    private UserInfo2Mapper userInfo2Mapper;
  
    @Test
    void selectByCondition() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("io");
        userInfo.setAge(23);
        //userInfo.setGender(0);
        List<UserInfo> userInfos = userInfo2Mapper.selectByCondition(userInfo);
        log.info(userInfos.toString());
    }
}

也是可以正常運行的,比限制 gender 多了一條數(shù)據(jù)?

Mybatis 動態(tài)SQL條件查詢(注釋和XML方式都有),mybatis,sql,windows

但是當我們把setUsername也給去掉,只查詢年齡為23的人,發(fā)現(xiàn)報錯了

?我們看日志會發(fā)現(xiàn),多了一個andMybatis 動態(tài)SQL條件查詢(注釋和XML方式都有),mybatis,sql,windows

這時候我們就要用 trim 標簽來消除這個多余的and?(上節(jié)博客也有trim的講解)

然后我們回到 Userinfo2XMLMapper.xml 對代碼進行修改,加上 trim 標簽

<?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.mybatisdemo.mapper.UserInfo2Mapper">
    <select id="selectByCondition" resultType="com.example.mybatisdemo.model.UserInfo">
        select * from userinfo
        where
        <trim prefixOverrides="and">
            <if test="username!=null">
                username = #{username}
            </if>
            <if test="age!=null">
                and age = #{age}
            </if>
            <if test="gender!=null">
                and gender = #{gender}
            </if>
        </trim>
    </select>
</mapper>

這時再次運行程序就能成功啦Mybatis 動態(tài)SQL條件查詢(注釋和XML方式都有),mybatis,sql,windows

另一種方法就是 where 標簽?

<?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.mybatisdemo.mapper.UserInfo2Mapper">
    <select id="selectByCondition" resultType="com.example.mybatisdemo.model.UserInfo">
        select * from userinfo
        <where>
            <if test="username!=null">
                username = #{username}
            </if>
            <if test="age!=null">
                and age = #{age}
            </if>
            <if test="gender!=null">
                and gender = #{gender}
            </if>
        </where>
    </select>
</mapper>

這也是可以成功運行的?

Mybatis 動態(tài)SQL條件查詢(注釋和XML方式都有),mybatis,sql,windows

用 where 還是 用 trim 都可以,這兩個沒啥差別?

但是 trim 標簽有個問題就是,如果所有where條件都為null的時候,會報錯,因為where后面沒東西了

Mybatis 動態(tài)SQL條件查詢(注釋和XML方式都有),mybatis,sql,windows

where 標簽就不會有上面的問題?,如果查詢條件均為空,直接刪除 where 關鍵字

Mybatis 動態(tài)SQL條件查詢(注釋和XML方式都有),mybatis,sql,windows

如果一定要用 trim 標簽也有一種解決方式

Mybatis 動態(tài)SQL條件查詢(注釋和XML方式都有),mybatis,sql,windows

接下來我們看看如何用注釋的方式實現(xiàn)

在 UserInfo2Mapper 接口中寫入下面的代碼,跟XML代碼差不多
package com.example.mybatisdemo.mapper;
import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface UserInfo2Mapper {
   
    @Select("<script>" +
            "select * from userinfo" +
            "        <where>" +
            "            <if test='username!=null'>" +
            "                username = #{username}" +
            "            </if>" +
            "            <if test='age!=null'>" +
            "                and age = #{age}" +
            "            </if>" +
            "            <if test='gender!=null'>" +
            "                and gender = #{gender}" +
            "            </if>" +
            "        </where>"+
            "</script>")
    List<UserInfo> selectByCondition2(UserInfo userInfo);
}

然后就,右鍵,Generate,test,勾選 selectByCondition2,ok,然后補充下面代碼,也跟XML一樣

package com.example.mybatisdemo.mapper;

import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@Slf4j
@SpringBootTest
class UserInfo2MapperTest {
    @Autowired
    private UserInfo2Mapper userInfo2Mapper;

    @Test
    void selectByCondition2() {
        UserInfo userInfo = new UserInfo();
        //userInfo.setUsername("io");
        userInfo.setAge(23);
        //userInfo.setGender(0);
        List<UserInfo> userInfos = userInfo2Mapper.selectByCondition(userInfo);
        log.info(userInfos.toString());
    }
}

運行試試,沒毛病?

Mybatis 動態(tài)SQL條件查詢(注釋和XML方式都有),mybatis,sql,windows文章來源地址http://www.zghlxwxcb.cn/news/detail-814242.html

到了這里,關于Mybatis 動態(tài)SQL條件查詢(注釋和XML方式都有)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • Mybatis xml中排序(order by)條件用#{}查詢失敗

    問題描述: 處理簡單分頁時,發(fā)現(xiàn)從外部傳入的排序條件無法生效,但程序無報錯,正常返回列表,只是排序條件不對; 原因: #{}表示一個占位符,當#{}傳入的數(shù)據(jù)是一個字符串時,會自動將傳入的數(shù)據(jù)加一個雙引號。 解決方法: 使用${}將傳入的數(shù)據(jù)直接顯示生成在sql中

    2024年01月17日
    瀏覽(16)
  • Mybatis 動態(tài) sql 是做什么的?都有哪些動態(tài) sql?能簡述動態(tài) sql 的執(zhí)行原理不?

    ????????OGNL表達式 ????????OGNL,全稱為Object-Graph Navigation Language,它是一個功能強大的表達式語言,用來獲取和設置Java對象的屬性,它旨在提供一個更高的更抽象的層次來對Java對象圖進行導航。 ????????OGNL表達式的基本單位是\\\"導航鏈\\\",一般導航鏈由如下幾個部

    2024年02月15日
    瀏覽(25)
  • Mybatis Plus之DQL(條件查詢方式、查詢投影、查詢條件設定、字段映射與表名映射)

    Mybatis Plus之DQL(條件查詢方式、查詢投影、查詢條件設定、字段映射與表名映射)

    增刪改查四個操作中,查詢是非常重要的也是非常復雜的操作,這塊需要我們重點學習下,這節(jié)我們主要學習的內(nèi)容有: 條件查詢方式 查詢投影 查詢條件設定 字段映射與表名映射 1.1 條件查詢的類 MyBatisPlus將書寫復雜的SQL查詢條件進行了封裝,使用編程的形式完成查詢條件的

    2024年02月05日
    瀏覽(23)
  • Mybatis—XML配置文件、動態(tài)SQL

    Mybatis—XML配置文件、動態(tài)SQL

    學習完Mybatis的基本操作之后,繼續(xù)學習Mybatis—XML配置文件、動態(tài)SQL。 Mybatis的開發(fā)有兩種方式: 注解 XML 之前學習的基本操作都是基于注解開發(fā)。使用Mybatis的注解方式,主要是來完成一些簡單的增刪改查功能。如果需要實現(xiàn)復雜的SQL功能,建議使用XML來配置映射語句,也就

    2024年02月06日
    瀏覽(22)
  • Mybatis-plus動態(tài)條件查詢QueryWrapper的使用

    Mybatis-plus動態(tài)條件查詢QueryWrapper的使用

    queryWrapper是mybatis plus中實現(xiàn)查詢的對象封裝操作類,可以封裝sql對象,包括where條件,order by排序,select哪些字段等等,他的層級關系如下圖: 2.1-案例一:根據(jù)name模糊查看未刪除的用戶列表信息 過濾條件: queryWrapper實現(xiàn): 2.2-案例二:查看姓李的并且郵箱不為空的用戶列表

    2024年02月14日
    瀏覽(22)
  • Mybatis從0到1 SQL注入 參數(shù)占位符 XML配置 動態(tài)SQL

    Mybatis從0到1 SQL注入 參數(shù)占位符 XML配置 動態(tài)SQL

    學習完mybatis入門后,我們繼續(xù)學習mybatis基礎操作。 需求說明: 根據(jù)資料中提供的《tlias智能學習輔助系統(tǒng)》頁面原型及需求,完成員工管理的需求開發(fā)。 通過分析以上的頁面原型和需求,我們確定了功能列表: 查詢 根據(jù)主鍵ID查詢 條件查詢 新增 更新 刪除 根據(jù)主鍵ID刪除

    2024年02月16日
    瀏覽(23)
  • Mybatis-plus 配置自定義sql(.xml文件)查詢語句的步驟

    Mybatis-plus 配置自定義sql(.xml文件)查詢語句的步驟

    這是使用Mybatis-plus 的自動生成實體類代碼生成.xml文件, 所以他會在java目錄下,不在resources目錄下 如果在java目錄下的xml文件,需要分別配置application.yml和pom.xml文件 type-aliases-package:java目錄下邊的第一級包名 mapper-locations: classpath:映射器的地址: 類路徑:也就是.xml所在的包名

    2024年02月16日
    瀏覽(27)
  • 【JaveWeb教程】(27)Mybatis的XML配置文件與Mybatis動態(tài)SQL 詳細代碼示例講解

    【JaveWeb教程】(27)Mybatis的XML配置文件與Mybatis動態(tài)SQL 詳細代碼示例講解

    Mybatis的開發(fā)有兩種方式: 注解 XML 使用Mybatis的注解方式,主要是來完成一些簡單的增刪改查功能。如果需要實現(xiàn)復雜的SQL功能,建議使用XML來配置映射語句,也就是將SQL語句寫在XML配置文件中。 在Mybatis中使用XML映射文件方式開發(fā),需要符合一定的規(guī)范: XML映射文件的名稱

    2024年02月01日
    瀏覽(24)
  • MyBatis XML 映射文件中的 SQL 語句可以分為動態(tài)語句和靜態(tài)語句

    目錄 靜態(tài)查詢: 動態(tài)查詢: 靜態(tài)更新: 動態(tài)更新: 靜態(tài)刪除: 動態(tài)刪除: 動態(tài)語句和靜態(tài)語句在 MyBatis 中的作用如下: 靜態(tài)查詢: 靜態(tài)查詢是指在 SQL 語句中執(zhí)行固定的查詢操作,查詢的條件和內(nèi)容是預先確定的,不會隨著用戶輸入或其他條件的改變而改變。以下是一

    2024年01月18日
    瀏覽(35)
  • MyBatis多表查詢+動態(tài)sql

    MyBatis多表查詢+動態(tài)sql

    在全局配置文件中中設置MyBatis執(zhí)行日志 假設有一個用戶表和文章表,實體類中一個關聯(lián)關系。 用戶實體類 文章實體類 如果想查詢的結(jié)果包含UserInfo的信息就需要使用,?對?映射要使? association 標簽,因為一篇文章只能對應一個作者。 Controller控制器代碼 Service服務層代碼

    2023年04月19日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包