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 + selenium + OpenCV破解騰訊防水墻滑動驗證碼功能
這篇文章主要介紹了使用java + selenium + OpenCV破解騰訊防水墻滑動驗證碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
Java數(shù)據(jù)類型Integer與int的區(qū)別詳細解析
這篇文章主要介紹了Java數(shù)據(jù)類型Integer與int的區(qū)別詳細解析,Ingeter是int的包裝類,int的初值為0,Ingeter的初值為null,int和integer(無論new否)比,都為true,因為會把Integer自動拆箱為int再去比,需要的朋友可以參考下2023-12-12
Java實現(xiàn)根據(jù)模板讀取PDF并替換指定內(nèi)容
在實際開發(fā)里,經(jīng)常會遇到需要根據(jù)?PDF?模板文檔生成特定?PDF?的需求,本文將利用Java中的iText實現(xiàn)讀取?PDF?模板文檔并替換指定內(nèi)容,最后重新生成新PDF,感興趣的可以了解下2025-02-02
Java 實現(xiàn)Redis存儲復(fù)雜json格式數(shù)據(jù)并返回給前端
這篇文章主要介紹了Java 實現(xiàn)Redis存儲復(fù)雜json格式數(shù)據(jù)并返回給前端操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

