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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

AngleFlyyy   2021-03-31 我要评论

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

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们