要實現查詢,咱們就先有個數據庫,截圖如下,其中cityareaid是外鍵,本次可以忽略;
下面branches是我的實體類,里面有name和address屬性;
接口中方法:
1
2
|
public list<branches> findongtai( @param ( "name" )string name, @param ( "add" )string address); //動態 public list<branches> findlike( @param ( "name" )string name, @param ( "add" )string address); //模糊 |
mybatis的接口映射文件的代碼:
動態查詢:
1
2
3
4
5
6
7
8
9
|
<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> |
模糊查詢:
1
2
3
|
<select id= "findlike" resulttype= "com.wj.entity.branches" > select * from branches where name like "%" #{name} "%" and address like "%" #{add} "%" </select> |
然后就是main方法實現了:
1
2
3
4
5
6
7
8
|
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()); } |
結果就是。。。
動態查詢:
模糊查詢:
總結
以上所述是小編給大家介紹的mybatis實現動態查詢、模糊查詢功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://blog.csdn.net/AngleFlyyy/article/details/80578612