SpringBoot整合Mybatis之各種查詢、模糊查詢、批量刪除、動態(tài)表名操作
一、普通查詢
1. 若查詢出的數(shù)據(jù)只有一條
a>可以通過實體類對象接收
<!-- 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ù)據(jù)轉(zhuǎn)換的map集合作為值,以某個字段的值作為鍵。
二、模糊查詢 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>四、動態(tài)設(shè)置表名 ${tableName}
<!-- List<Dish> getDishs(@Param("tableName") String tableName);-->
<select id="getDishs" resultType="com.athorse.entities.Dish">
select * from ${tableName}
</select>總結(jié):#{}會自動的拼接上'',而${}不會,所以特殊場景下需要使用${}
到此這篇關(guān)于SpringBoot整合Mybatis之各種查詢、模糊查詢、批量刪除、動態(tài)表名的文章就介紹到這了,更多相關(guān)SpringBoot整合Mybatis查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java NIO三大組件與ByteBuffer深入理解及使用
這篇文章主要介紹了Java NIO三大組件與ByteBuffer,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
SpringBoot快速構(gòu)建應(yīng)用程序方法介紹
這篇文章主要介紹了SpringBoot快速構(gòu)建應(yīng)用程序方法介紹,涉及SpringBoot默認(rèn)的錯誤頁面,嵌入式Web容器層面的約定和定制等相關(guān)內(nèi)容,具有一定借鑒價值,需要的朋友可以參考下。2017-11-11
用Spring將Service注入到Servlet中的流程步驟
在Java Web開發(fā)中,??Servlet??是一個非常重要的組件,它用于處理客戶端的請求并生成響應(yīng),而?Spring??框架則是一個廣泛使用的依賴注入框架,可以幫助開發(fā)者管理應(yīng)用中的對象及其依賴關(guān)系,本文將介紹如何使用Spring框架將Service層的對象注入到Servlet中2025-01-01
java實現(xiàn)在性能測試中進(jìn)行業(yè)務(wù)驗證實例
這篇文章主要為大家介紹了java實現(xiàn)在性能測試中進(jìn)行業(yè)務(wù)驗證實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07

