SpringBoot整合Mybatis之各種查詢、模糊查詢、批量刪除、動(dòng)態(tài)表名操作
一、普通查詢
1. 若查詢出的數(shù)據(jù)只有一條
a>可以通過實(shí)體類對(duì)象接收
<!-- Dish getDishByName(@Param("name") String name);-->
<select id="getDishByName" resultType="com.athorse.entities.Dish">
select * from dish where name = #{name}
</select>b>可以通過list集合接收
<!-- List<Dish> getDishByName(@Param("name") String name);-->
<select id="getDishByName" resultType="com.athorse.entities.Dish">
select * from dish where name = #{name}
</select>c>可以通過map集合接收
<!-- Map<String,Object> getDishByName(@Param("name") String name);-->
<select id="getDishByName" resultType="map">
select * from dish where name = #{name}
</select>2. 若查詢出的數(shù)據(jù)有多條
a> 可以通過list集合接收
<!-- List<Dish> getDishByName(@Param("name") String name);-->
<select id="getDishByName" resultType="com.athorse.entities.Dish">
select * from dish where name = #{name}
</select>b>可以通過map類型的list集合接收
<!-- List<Map<String,Object>> getDishByName(@Param("name") String name);-->
<select id="getDishByName" resultType="map">
select * from dish where name = #{name}
</select>c>可以在mapper接口的方法上添加@MapKey注解,此時(shí)就可以將每條數(shù)據(jù)轉(zhuǎn)換的map集合作為值,以某個(gè)字段的值作為鍵。
二、模糊查詢 like "%"#{name}"%"
<!-- List<Dish> getDishByName(@Param("name") String name);-->
<select id="getDishByName" resultType="com.athorse.entities.Dish">
select * from dish where name like "%"#{name}"%"
</select>三、批量刪除 in (${ids})
<!--Integer deleteMore(@Param("ids") String ids);-->
<delete id="deleteMore">
delete from dish where id in (${ids})
</delete>四、動(dòng)態(tài)設(shè)置表名 ${tableName}
<!-- List<Dish> getDishs(@Param("tableName") String tableName);-->
<select id="getDishs" resultType="com.athorse.entities.Dish">
select * from ${tableName}
</select>總結(jié):#{}會(huì)自動(dòng)的拼接上'',而${}不會(huì),所以特殊場(chǎng)景下需要使用${}
到此這篇關(guān)于SpringBoot整合Mybatis之各種查詢、模糊查詢、批量刪除、動(dòng)態(tài)表名的文章就介紹到這了,更多相關(guān)SpringBoot整合Mybatis查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java NIO三大組件與ByteBuffer深入理解及使用
這篇文章主要介紹了Java NIO三大組件與ByteBuffer,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
SpringBoot快速構(gòu)建應(yīng)用程序方法介紹
這篇文章主要介紹了SpringBoot快速構(gòu)建應(yīng)用程序方法介紹,涉及SpringBoot默認(rèn)的錯(cuò)誤頁(yè)面,嵌入式Web容器層面的約定和定制等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-11-11
用Spring將Service注入到Servlet中的流程步驟
在Java Web開發(fā)中,??Servlet??是一個(gè)非常重要的組件,它用于處理客戶端的請(qǐng)求并生成響應(yīng),而?Spring??框架則是一個(gè)廣泛使用的依賴注入框架,可以幫助開發(fā)者管理應(yīng)用中的對(duì)象及其依賴關(guān)系,本文將介紹如何使用Spring框架將Service層的對(duì)象注入到Servlet中2025-01-01
Spring Boot簡(jiǎn)介與快速搭建詳細(xì)步驟
SpringBoot其本身沒有添加什么新的技術(shù),就是整合了一些現(xiàn)有的框架,并提供了一些默認(rèn)的配置,就是這些默認(rèn)的配置,極大的提高了我們的開發(fā)效率。這篇文章主要介紹了Spring Boot簡(jiǎn)介與快速搭建,需要的朋友可以參考下2021-05-05
java實(shí)現(xiàn)在性能測(cè)試中進(jìn)行業(yè)務(wù)驗(yàn)證實(shí)例
這篇文章主要為大家介紹了java實(shí)現(xiàn)在性能測(cè)試中進(jìn)行業(yè)務(wù)驗(yàn)證實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
淺談Java動(dòng)態(tài)代理的實(shí)現(xiàn)
最近,小組同事做代碼改造時(shí),使用到了動(dòng)態(tài)代理,自己閱讀時(shí),發(fā)現(xiàn)對(duì)代理這種設(shè)計(jì)模式都不怎么清楚,導(dǎo)致理解代碼也很困難 自己唯一能看懂的,大概就是handler中的invoke方法 ,文中作出了非常詳細(xì)的介紹,需要的朋友可以參考下2021-05-05

