MyBatis實現(xiàn)動態(tài)查詢、模糊查詢功能
要實現(xiàn)查詢,咱們就先有個數(shù)據(jù)庫,截圖如下,其中cityAreaId是外鍵,本次可以忽略;

下面Branches是我的實體類,里面有name和address屬性;
接口中方法:
public List<Branches> finDongTai(@Param("name")String name,@Param("add")String address);//動態(tài)
public List<Branches> findLike(@Param("name")String name,@Param("add")String address);//模糊
MyBatis的接口映射文件的代碼:
動態(tài)查詢:
<select id="finDongTai" resultType="com.wj.entity.Branches" >
SELECT * FROM Branches where 1=1
<if test="name!=''and name!=null">
and name =#{name}
</if>
<if test="add!=''and add!=null">
and address =#{add}
</if>
</select>
模糊查詢:
<select id="findLike" resultType="com.wj.entity.Branches" >
SELECT * FROM Branches where name like "%"#{name}"%" and address like "%"#{add}"%"
</select>
然后就是main方法實現(xiàn)了:
List<Branches> list=new BranchesImpl().finDongTai("建設(shè)銀行", "");
for (Branches branches : list) {
System.out.println("名稱:"+branches.getName()+"\t---\t地址:"+branches.getAddress());
}
List<Branches> list=new BranchesImpl().findLike("支行", "路");
for (Branches branches : list) {
System.out.println("名稱:"+branches.getName()+"\t---\t地址:"+branches.getAddress());
}
結(jié)果就是。。。
動態(tài)查詢:

模糊查詢:

總結(jié)
以上所述是小編給大家介紹的MyBatis實現(xiàn)動態(tài)查詢、模糊查詢功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Springboot+Vue+axios實現(xiàn)文章收藏功能
這篇文章主要為大家詳細介紹了Springboot+Vue+axios實現(xiàn)文章收藏功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08
jmeter實現(xiàn)接口關(guān)聯(lián)的兩種方式(正則表達式提取器和json提取器)
Jmeter用于接口測試時,后一個接口經(jīng)常需要用到前一次接口返回的結(jié)果,本文主要介紹了jmeter實現(xiàn)接口關(guān)聯(lián)的兩種方式,感興趣的小伙伴們可以參考一下2021-11-11
Spring Security注解方式權(quán)限控制過程
這篇文章主要介紹了Spring Security注解方式權(quán)限控制過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
Springboot升級到2.7.2結(jié)合nacos遇到的坑及解決
這篇文章主要介紹了Springboot升級到2.7.2結(jié)合nacos遇到的坑及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06

