To implement query, we will first have a database, as shown below, where cityAreaId is a foreign key, which can be ignored this time;
Branches below are my entity class, with name and address attributes;
Methods in the interface:
public List<Branches> finDongTai(@Param("name")String name,@Param("add")String address);//Dynamic public List<Branches> findLike(@Param("name")String name,@Param("add")String address);//FunnyCode for MyBatis' interface mapping file:
Dynamic query:
<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>Fuzzy query:
<select id="findLike" resultType="com.wj.entity.Branches" > SELECT * FROM Branches where name like "%"#{name}"%" and address like "%"#{add}"%" </select>Then the main method is implemented:
List<Branches> list=new BranchesImpl().finDongTai("China Construction Bank", ""); for (Branches branches : list) { System.out.println("Name:"+branches.getName()+"/t---/tAddress: "+branches.getAddress()); }List<Branches> list=new BranchesImpl().findLike("Subbranches", "Road"); for (Branches branches : list) { System.out.println("name: "+branches.getName()+"/t--/tAddress: "+branches.getAddress()); }The result is. . .
Dynamic query:
Fuzzy query:
Summarize
The above is the MyBatis implementation of dynamic query and fuzzy query functions introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!