SpringBoot多条件查询

多条件查询

首先,在sqlProvide写sql语句和条件判断(mybatisGenerator自动生成文件)

 public String selectDeviceBrandBo(DeviceByCodeBo deviceByCodeBo){
            /*
             * 1. 给出一个sql语句前半部
             */
            StringBuilder sql = new StringBuilder("select d.id, name_chs,code,ds.`status` from device d left join brand b on b.id=d.brand_id\n" +
                    "                   left join device_status ds on ds.device_id=d.id \n" +
                    "                    where d.status=0 and 1=1");
            if(!("".equals(deviceByCodeBo.getCode()))&&deviceByCodeBo.getCode()!=null ) {
                sql.append(" and code like concat('%',#{code},'%')");
            }
            if(!("".equals(deviceByCodeBo.getStatus()))&&deviceByCodeBo.getStatus()!=null ) {
                sql.append(" and ds.status=#{status}");
            }
            /*
             * 三、执行query
             */
            return sql.toString();
        }

再到mapper映射关系

    @SelectProvider(type=DeviceSqlProvider.class, method="selectDeviceBrandBo")
    @Results({
            @Result(column="id", property="id"),
            @Result(column="name_chs", property="nameChs"),
            @Result(column="code", property="code"),
            @Result(column="status", property="status")


    })
    List<DeviceBo> selectDeviceBrandBo(DeviceByCodeBo deviceByCodeBo);

在实现类直接调用就可以了

  @PostMapping(value = "/queryByCode")
    @ApiOperation(value="根据编码或者状态查询", notes="根据编码或者状态查询")
    @Override
    public Msg<List<DeviceBo>> queryByCode(@RequestBody DeviceByCodeBo deviceByCodeBo) {
        Msg msg = Msg.ok();
        try {

            List<DeviceBo> brandBos = deviceMapper.selectDeviceBrandBo(deviceByCodeBo);
            msg.setData(brandBos);
        }catch(Exception e){
            e.printStackTrace();
        }
        return msg;
    }

你可能感兴趣的:(mybatis,SpringBoot,IDEA)