赞
踩
错误代码
QueryWrapper<DeviceInfoDO> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("enterprise_id",tSysUser.getEnterpriseId());
queryWrapper
.like("device_sn",pageQuery.getKeyword())
.or()
.like("device_name",pageQuery.getKeyword())
.or()
.like("device_factory",pageQuery.getKeyword());
根据上面的写法写出的sql语句如下:
WHERE enterprise_id = '98ca9c7540ee476fb931b92d6d1c3cda'
AND device_sn LIKE '%1%'
OR device_name LIKE '%1%'
OR device_factory LIKE '%1%'
这样明显是不对的,根据mysql语句执行顺序or最后执行 ,所以第一个条件 并没有起到and的作用。
QueryWrapper<DeviceInfoDO> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("enterprise_id",tSysUser.getEnterpriseId());
queryWrapper.and(wrapper->wrapper
.like("device_sn",pageQuery.getKeyword())
.or()
.like("device_name",pageQuery.getKeyword())
.or()
.like("device_factory",pageQuery.getKeyword())
);
这样得到的sql语句如下
WHERE enterprise_id = '98ca9c7540ee476fb931b92d6d1c3cda'
AND (device_sn LIKE '%1%'
OR device_name LIKE '%1%'
OR device_factory LIKE '%1%')
这样就可以了
1、from
2、where (or 最后执行)
3、group by
4、having
5、DISTINCT
6、order by
7、limit
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。