mybatis动态查询,模糊查询

mybatis动态查询,模糊查询

第一种:直接动态sql注解的形式

@Select("<script>"
        +"select * from problem where pro_res=0 and pro_state=#{state}  "
        +"      <if test='name != null' >   "
        +"     and app_id in (select app_id from application where app_name like '%${name}%' )"
        +"      </if>"
        +"      <if test='title != null' >   "
        +"            and pro_title like '%${title}%' "
        +"      </if>"
        +"      <if test='level != null' >"
        +"            and pro_level=#{level} "
        +"      </if>"
        +"      <if test='industry != null' >"
        +"            and pro_industry like '%${industry}%' "
        +"      </if>"
        +"      <if test='company != null' >"
        +"            and pro_company like '%${company}%' "
        +"      </if>"
        +"      <if test='keyword != null' >"
        +"            and pro_keyword like '%${keyword}%' "
        +"      </if>"
        +"</script>")
@ResultMap("problemMap")
List<Problem> searchByConditions(String name, @Param("title") String title, String level, String industry, String company, String keyword,@Param("state") String state);

第二种,基于Example的,适合于单个表的条件查询,模糊查询

@Override
public List<Subject> searchByConditions(String name, String degree, String title, String field, String institute) {
    SubjectExample subjectExample=new SubjectExample();
    SubjectExample.Criteria or = subjectExample.or();
    if (StringUtils.isNotBlank(name)){
        or.andSubNameLike("%"+name+"%");
    }
    if (StringUtils.isNotBlank(degree)){
        or.andSubEducationLike(degree);
    }
    if (StringUtils.isNotBlank(title)){
        or.andSubHeadLike("%"+title+"%");
    }
    if (StringUtils.isNotBlank(field)){
        or.andSubFieldLike("%"+field+"%");
    }
    if (StringUtils.isNotBlank(institute)){
        or.andSubWorkplaceLike("%"+institute+"%");
    }
    return subjectMapper.selectByExample(subjectExample);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值