Skip to content

Commit ceb6da7

Browse files
committed
去除1=1这么难?
1 parent 06c6844 commit ceb6da7

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/Wrapper.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,10 @@ public boolean nonEmptyOfEntity() {
106106
if (tableInfo == null) {
107107
return false;
108108
}
109-
if (StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
110-
// 有主键
111-
return Objects.nonNull(ReflectionKit.getMethodValue(entity, tableInfo.getKeyProperty())) &&
112-
tableInfo.getFieldList().stream().anyMatch(e -> fieldStrategyMatch(entity, e));
113-
} else {
114-
// 无主键
115-
return tableInfo.getFieldList().stream().anyMatch(e -> fieldStrategyMatch(entity, e));
109+
if (tableInfo.getFieldList().stream().anyMatch(e -> fieldStrategyMatch(entity, e))) {
110+
return true;
116111
}
112+
return StringUtils.isNotEmpty(tableInfo.getKeyProperty()) ? Objects.nonNull(ReflectionKit.getMethodValue(entity, tableInfo.getKeyProperty())) : false;
117113
}
118114

119115
/**

mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
190190
String sqlScript = table.getAllSqlWhere(false, true, WRAPPER_ENTITY_DOT);
191191
sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", WRAPPER_ENTITY), true);
192192
sqlScript += NEWLINE;
193-
sqlScript += SqlScriptUtils.convertIf(String.format("AND ${%s}", WRAPPER_SQLSEGMENT),
193+
sqlScript += SqlScriptUtils.convertIf(String.format(SqlScriptUtils.convertIf(" AND", "ew.nonEmptyOfEntity and ew.nonEmptyOfNormal", false) + " ${%s}", WRAPPER_SQLSEGMENT),
194194
String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT,
195195
WRAPPER_NONEMPTYOFWHERE), true);
196196
sqlScript = SqlScriptUtils.convertWhere(sqlScript) + NEWLINE;
@@ -279,4 +279,6 @@ protected MappedStatement addMappedStatement(Class<?> mapperClass, String id, Sq
279279
* @return MappedStatement
280280
*/
281281
public abstract MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo);
282+
283+
282284
}

mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlTestDataMapperTest.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ public void d6_selectList() {
234234
@Test
235235
@SuppressWarnings("unchecked")
236236
public void d7_1_selectListForNoLogic() {
237-
// 1. 只有 entity
238237
MysqlData data = new MysqlData().setOrder(1);
238+
// 1. 只有 entity
239239
Assert.assertTrue(CollectionUtils.isNotEmpty(mysqlMapper.selectList(Wrappers.query(data))));
240240
// 2. 有 entity 也有 where 条件
241241
Assert.assertTrue(CollectionUtils.isNotEmpty(mysqlMapper.selectList(Wrappers.query(data).eq(MysqlData::getGroup, 1))));
@@ -247,6 +247,12 @@ public void d7_1_selectListForNoLogic() {
247247
// 5. 只有 order by 或者 last
248248
Assert.assertTrue(CollectionUtils.isNotEmpty(mysqlMapper.selectList(Wrappers.<MysqlData>query()
249249
.lambda().orderByDesc(MysqlData::getOrder).last("limit 1"))));
250+
// 6. 什么都没有情况
251+
Assert.assertTrue(CollectionUtils.isNotEmpty(mysqlMapper.selectList(Wrappers.emptyWrapper())));
252+
// 7. 只有 where 条件
253+
Assert.assertTrue(CollectionUtils.isNotEmpty(mysqlMapper.selectList(Wrappers.query(new MysqlData()).eq(MysqlData::getGroup, 1))));
254+
// 8. 有 where 条件 也有 last 条件
255+
Assert.assertTrue(CollectionUtils.isNotEmpty(mysqlMapper.selectList(Wrappers.query(new MysqlData()).eq(MysqlData::getGroup, 1).last("limit 1"))));
250256
}
251257

252258
@Test
@@ -265,6 +271,12 @@ public void d7_2_selectListForLogic() {
265271
// 5. 只有 order by 或者 last
266272
Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectList(Wrappers.<CommonLogicData>query()
267273
.lambda().orderByAsc(CommonLogicData::getTestInt).last("limit 1"))));
274+
// 6. 什么都没有情况
275+
Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectList(Wrappers.emptyWrapper())));
276+
// 7. 只有 where 条件
277+
Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectList(Wrappers.query(new CommonLogicData()).eq(CommonLogicData::getId, 11))));
278+
// 8. 有 where 条件 也有 last 条件
279+
Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectList(Wrappers.query(new CommonLogicData()).eq(CommonLogicData::getId, 11).last("limit 1"))));
268280
}
269281

270282
@Test

0 commit comments

Comments
 (0)