当查询条件为 null 时,这条查询条件不会生效
Example example = new Example(NgStatisticsWeek.class);
example.createCriteria().andEqualTo("year", o.getYear()).andEqualTo("week", o.getWeek())
.andEqualTo("coreParts", o.getCoreParts());
NgStatisticsWeek weekObj = ngStatisticsWeekMapper.selectOneByExample(example);
举个例子,我第一次查询2022年第26周并且不是核心件的数据(也就是coreParts = 0)的数据
通用mapper转换为的sql为:
SELECT
`id`,
`year`,
`week`,
`create_time`,
`check_batch_num`,
`check_total_num`,
`ng_batch_num`,
`ng_total_num`,
`batch_ng_rate`,
`total_ng_rate`,
`materialname`,
`materialcode`,
`suppliername`,
`suppliercode`,
`respperson`,
`product_line`,
`pv_flag`,
`core_parts`,
`org_code`
FROM
`ng_statistics_week`
WHERE
(
`year` = '2022'
AND `week` = '26'
AND `core_parts` = 0
)
当我查询出没有这条数据时.插入这条数据进去
当我再次用这个方法查询时,如果o.getCoreParts 参数为 null
sql为:
SELECT
`id`,
`year`,
`week`,
`create_time`,
`check_batch_num`,
`check_total_num`,
`ng_batch_num`,
`ng_total_num`,
`batch_ng_rate`,
`total_ng_rate`,
`materialname`,
`materialcode`,
`suppliername`,
`suppliercode`,
`respperson`,
`product_line`,
`pv_flag`,
`core_parts`,
`org_code`
FROM
`ng_statistics_week`
WHERE
(
`year` = '2022'
AND `week` = '26'
)
此时如果数据库中还有一条2022年第26周并且core_parts = 1的数据
会查出两条数据
此时selectOneByExample() 方法会报错
总结:
查询条件为null时,通用mapper不会进行拼接
解决方法:
可以在外面判断,如果为空,可以用andIsNull()方法