MyBatis实现动态查询、模糊查询功能

要实现查询,咱们就先有个数据库,截图如下,其中cityAreaId是外键,本次可以忽略;

下面Branches是我的实体类,里面有name和address属性;

接口中方法:

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的接口映射文件的代码:

动态查询:

<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方法实现了:

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实现动态查询、模糊查询功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

以上是 MyBatis实现动态查询、模糊查询功能 的全部内容, 来源链接: utcz.com/z/346468.html

回到顶部