!82 QueryWrapper 传入 null 值产生 NPE 问题

Merge pull request !82 from 王帅/main
This commit is contained in:
Michael Yang 2023-06-21 11:39:17 +00:00 committed by Gitee
commit 61be7c544f
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
7 changed files with 88 additions and 49 deletions

View File

@ -947,6 +947,11 @@ public class CommonsDialectImpl implements IDialect {
} else if (!allowNoCondition) {
throw new IllegalArgumentException("Not allowed DELETE a table without where condition.");
}
} else {
// whereQueryCondition == null
if (!allowNoCondition) {
throw new IllegalArgumentException("Not allowed DELETE a table without where condition.");
}
}
}

View File

@ -1,17 +1,17 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.core.provider;
@ -200,9 +200,6 @@ public class EntitySqlProvider {
*/
public static String deleteByQuery(Map params, ProviderContext context) {
QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params);
if (queryWrapper == null) {
throw FlexExceptions.wrap("queryWrapper can not be null or empty.");
}
TableInfo tableInfo = ProviderUtil.getTableInfo(context);
CPI.setFromIfNecessary(queryWrapper, tableInfo.getSchema(), tableInfo.getTableName());
@ -366,9 +363,7 @@ public class EntitySqlProvider {
*/
public static String selectListByQuery(Map params, ProviderContext context) {
QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params);
if (queryWrapper == null) {
throw FlexExceptions.wrap("queryWrapper can not be null.");
}
List<TableInfo> tableInfos = getTableInfos(context, queryWrapper);
for (TableInfo tableInfo : tableInfos) {
tableInfo.appendConditions(null, queryWrapper);
@ -396,9 +391,6 @@ public class EntitySqlProvider {
*/
public static String selectObjectByQuery(Map params, ProviderContext context) {
QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params);
if (queryWrapper == null) {
throw FlexExceptions.wrap("queryWrapper can not be null.");
}
List<TableInfo> tableInfos = getTableInfos(context, queryWrapper);

View File

@ -28,6 +28,7 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
@SuppressWarnings({"rawtypes", "unchecked"})
class ProviderUtil {
private ProviderUtil() {}
@ -44,6 +45,7 @@ class ProviderUtil {
Object schemaNameObj = params.get(FlexConsts.SCHEMA_NAME);
return schemaNameObj != null ? schemaNameObj.toString().trim() : null;
}
public static String getTableName(Map params) {
Object tableNameObj = params.get(FlexConsts.TABLE_NAME);
return tableNameObj != null ? tableNameObj.toString().trim() : null;
@ -76,7 +78,13 @@ class ProviderUtil {
}
public static QueryWrapper getQueryWrapper(Map params) {
return (QueryWrapper) params.get(FlexConsts.QUERY);
Object obj = params.get(FlexConsts.QUERY);
// QueryWrapper 如果为 null 则创建空的对象
// 避免多次空判断以及 NullPointerException
if (obj == null) {
return QueryWrapper.create();
}
return (QueryWrapper) obj;
}
public static Row getRow(Map params) {

View File

@ -1,17 +1,17 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.core.query;
@ -24,11 +24,13 @@ import java.util.Map;
public class If {
private If() {
}
/**
* 判断对象是否为空
*/
public static boolean isNull(Object object) {
public static boolean isNull(Object object) {
return object == null;
}
@ -41,26 +43,27 @@ public class If {
/**
* 查看某个对象是否为空支持数组集合map
*
* @param object
*/
public static boolean notEmpty(Object object){
if (object == null){
public static boolean notEmpty(Object object) {
if (object == null) {
return false;
}
if (object instanceof Collection){
if (object instanceof Collection) {
return !((Collection<?>) object).isEmpty();
}
if (ClassUtil.isArray(object.getClass())){
return Array.getLength(object) >0;
if (ClassUtil.isArray(object.getClass())) {
return Array.getLength(object) > 0;
}
if (object instanceof Map){
if (object instanceof Map) {
return !((Map<?, ?>) object).isEmpty();
}
if (object instanceof String){
if (object instanceof String) {
return StringUtil.isNotBlank((String) object);
}
return true;
@ -69,18 +72,20 @@ public class If {
/**
* 查看某个对象是否为空数据 或者 null
*
* @param object
*/
public static boolean isEmpty(Object object){
public static boolean isEmpty(Object object) {
return !notEmpty(object);
}
/**
* 查看某个 string 对象是否有文本内容
*
* @param object
*/
public static boolean hasText(Object object){
public static boolean hasText(Object object) {
return object != null && StringUtil.isNotBlank((String) object);
}

View File

@ -16,6 +16,8 @@
package com.mybatisflex.test.model;
import com.mybatisflex.annotation.Column;
import java.util.List;
/**
@ -25,6 +27,7 @@ import java.util.List;
public class BaseEntity<T, ID, L> extends IdEntity<ID> {
protected T userName;
@Column(ignore = true)
protected List<L> roles;
public List<L> getRoles() {

View File

@ -4,7 +4,7 @@ spring:
# console:
# enabled: true
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
# driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/flex_test
username: root
password: 12345678

View File

@ -17,14 +17,18 @@
package com.mybatisflex.test.mapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.test.model.Account;
import com.mybatisflex.test.model.AccountVO;
import com.mybatisflex.test.model.Gender;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Date;
import java.util.List;
import static com.mybatisflex.test.model.table.AccountTableDef.ACCOUNT;
import static com.mybatisflex.test.model.table.RoleTableDef.ROLE;
@ -86,4 +90,26 @@ class AccountMapperTest {
accountMapper.update(account);
}
@Test
void testSelectList() {
List<Account> accounts = accountMapper.selectListByQuery(null);
System.out.println(accounts);
List<Row> account = Db.selectListByQuery("tb_account", null);
System.out.println(account);
}
@Test
void testUpdateAll() {
Account account = new Account();
account.setAge(10);
Assertions.assertThrows(Exception.class, () ->
accountMapper.updateByQuery(account, QueryWrapper.create()));
}
@Test
void testDeleteAll() {
Assertions.assertThrows(Exception.class, () ->
accountMapper.deleteByQuery(QueryWrapper.create()));
}
}