MyBatis實現(xiàn)動態(tài)查詢、模糊查詢功能
更新時間:2018年06月05日 14:27:46 作者:AngleFlyyy
這篇文章主要介紹了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("建設銀行", ""); 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()); }
結果就是。。。
動態(tài)查詢:
模糊查詢:
總結
以上所述是小編給大家介紹的MyBatis實現(xiàn)動態(tài)查詢、模糊查詢功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
Intellij IDEA 錄制快捷鍵實現(xiàn)自動格式化的方法
這篇文章主要介紹了Intellij IDEA 錄制快捷鍵實現(xiàn)自動格式化的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09Mybatis Mybatis-Plus傳入多個參數(shù)的處理方式
這篇文章主要介紹了Mybatis Mybatis-Plus傳入多個參數(shù)的處理方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05