mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 09:38:26 +08:00
!82 QueryWrapper 传入 null 值产生 NPE 问题
Merge pull request !82 from 王帅/main
This commit is contained in:
commit
61be7c544f
@ -947,6 +947,11 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
} else if (!allowNoCondition) {
|
} else if (!allowNoCondition) {
|
||||||
throw new IllegalArgumentException("Not allowed DELETE a table without where condition.");
|
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.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
/**
|
/*
|
||||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
* <p>
|
* <p>
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
@ -200,9 +200,6 @@ public class EntitySqlProvider {
|
|||||||
*/
|
*/
|
||||||
public static String deleteByQuery(Map params, ProviderContext context) {
|
public static String deleteByQuery(Map params, ProviderContext context) {
|
||||||
QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params);
|
QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params);
|
||||||
if (queryWrapper == null) {
|
|
||||||
throw FlexExceptions.wrap("queryWrapper can not be null or empty.");
|
|
||||||
}
|
|
||||||
|
|
||||||
TableInfo tableInfo = ProviderUtil.getTableInfo(context);
|
TableInfo tableInfo = ProviderUtil.getTableInfo(context);
|
||||||
CPI.setFromIfNecessary(queryWrapper, tableInfo.getSchema(), tableInfo.getTableName());
|
CPI.setFromIfNecessary(queryWrapper, tableInfo.getSchema(), tableInfo.getTableName());
|
||||||
@ -366,9 +363,7 @@ public class EntitySqlProvider {
|
|||||||
*/
|
*/
|
||||||
public static String selectListByQuery(Map params, ProviderContext context) {
|
public static String selectListByQuery(Map params, ProviderContext context) {
|
||||||
QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params);
|
QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params);
|
||||||
if (queryWrapper == null) {
|
|
||||||
throw FlexExceptions.wrap("queryWrapper can not be null.");
|
|
||||||
}
|
|
||||||
List<TableInfo> tableInfos = getTableInfos(context, queryWrapper);
|
List<TableInfo> tableInfos = getTableInfos(context, queryWrapper);
|
||||||
for (TableInfo tableInfo : tableInfos) {
|
for (TableInfo tableInfo : tableInfos) {
|
||||||
tableInfo.appendConditions(null, queryWrapper);
|
tableInfo.appendConditions(null, queryWrapper);
|
||||||
@ -396,9 +391,6 @@ public class EntitySqlProvider {
|
|||||||
*/
|
*/
|
||||||
public static String selectObjectByQuery(Map params, ProviderContext context) {
|
public static String selectObjectByQuery(Map params, ProviderContext context) {
|
||||||
QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params);
|
QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params);
|
||||||
if (queryWrapper == null) {
|
|
||||||
throw FlexExceptions.wrap("queryWrapper can not be null.");
|
|
||||||
}
|
|
||||||
|
|
||||||
List<TableInfo> tableInfos = getTableInfos(context, queryWrapper);
|
List<TableInfo> tableInfos = getTableInfos(context, queryWrapper);
|
||||||
|
|
||||||
|
|||||||
@ -28,6 +28,7 @@ import java.util.Collection;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||||
class ProviderUtil {
|
class ProviderUtil {
|
||||||
|
|
||||||
private ProviderUtil() {}
|
private ProviderUtil() {}
|
||||||
@ -44,6 +45,7 @@ class ProviderUtil {
|
|||||||
Object schemaNameObj = params.get(FlexConsts.SCHEMA_NAME);
|
Object schemaNameObj = params.get(FlexConsts.SCHEMA_NAME);
|
||||||
return schemaNameObj != null ? schemaNameObj.toString().trim() : null;
|
return schemaNameObj != null ? schemaNameObj.toString().trim() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getTableName(Map params) {
|
public static String getTableName(Map params) {
|
||||||
Object tableNameObj = params.get(FlexConsts.TABLE_NAME);
|
Object tableNameObj = params.get(FlexConsts.TABLE_NAME);
|
||||||
return tableNameObj != null ? tableNameObj.toString().trim() : null;
|
return tableNameObj != null ? tableNameObj.toString().trim() : null;
|
||||||
@ -76,7 +78,13 @@ class ProviderUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static QueryWrapper getQueryWrapper(Map params) {
|
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) {
|
public static Row getRow(Map params) {
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
/**
|
/*
|
||||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
* <p>
|
* <p>
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
@ -24,6 +24,8 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class If {
|
public class If {
|
||||||
|
|
||||||
|
private If() {
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断对象是否为空
|
* 判断对象是否为空
|
||||||
@ -41,6 +43,7 @@ public class If {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 查看某个对象是否为空,支持数组、集合、map 等
|
* 查看某个对象是否为空,支持数组、集合、map 等
|
||||||
|
*
|
||||||
* @param object
|
* @param object
|
||||||
*/
|
*/
|
||||||
public static boolean notEmpty(Object object) {
|
public static boolean notEmpty(Object object) {
|
||||||
@ -69,6 +72,7 @@ public class If {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 查看某个对象是否为空数据 或者 null
|
* 查看某个对象是否为空数据 或者 null
|
||||||
|
*
|
||||||
* @param object
|
* @param object
|
||||||
*/
|
*/
|
||||||
public static boolean isEmpty(Object object) {
|
public static boolean isEmpty(Object object) {
|
||||||
@ -78,6 +82,7 @@ public class If {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 查看某个 string 对象是否有文本内容
|
* 查看某个 string 对象是否有文本内容
|
||||||
|
*
|
||||||
* @param object
|
* @param object
|
||||||
*/
|
*/
|
||||||
public static boolean hasText(Object object) {
|
public static boolean hasText(Object object) {
|
||||||
|
|||||||
@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package com.mybatisflex.test.model;
|
package com.mybatisflex.test.model;
|
||||||
|
|
||||||
|
import com.mybatisflex.annotation.Column;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,6 +27,7 @@ import java.util.List;
|
|||||||
public class BaseEntity<T, ID, L> extends IdEntity<ID> {
|
public class BaseEntity<T, ID, L> extends IdEntity<ID> {
|
||||||
|
|
||||||
protected T userName;
|
protected T userName;
|
||||||
|
@Column(ignore = true)
|
||||||
protected List<L> roles;
|
protected List<L> roles;
|
||||||
|
|
||||||
public List<L> getRoles() {
|
public List<L> getRoles() {
|
||||||
|
|||||||
@ -4,7 +4,7 @@ spring:
|
|||||||
# console:
|
# console:
|
||||||
# enabled: true
|
# enabled: true
|
||||||
datasource:
|
datasource:
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
# driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
url: jdbc:mysql://localhost:3306/flex_test
|
url: jdbc:mysql://localhost:3306/flex_test
|
||||||
username: root
|
username: root
|
||||||
password: 12345678
|
password: 12345678
|
||||||
|
|||||||
@ -17,14 +17,18 @@
|
|||||||
package com.mybatisflex.test.mapper;
|
package com.mybatisflex.test.mapper;
|
||||||
|
|
||||||
import com.mybatisflex.core.query.QueryWrapper;
|
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.Account;
|
||||||
import com.mybatisflex.test.model.AccountVO;
|
import com.mybatisflex.test.model.AccountVO;
|
||||||
import com.mybatisflex.test.model.Gender;
|
import com.mybatisflex.test.model.Gender;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static com.mybatisflex.test.model.table.AccountTableDef.ACCOUNT;
|
import static com.mybatisflex.test.model.table.AccountTableDef.ACCOUNT;
|
||||||
import static com.mybatisflex.test.model.table.RoleTableDef.ROLE;
|
import static com.mybatisflex.test.model.table.RoleTableDef.ROLE;
|
||||||
@ -86,4 +90,26 @@ class AccountMapperTest {
|
|||||||
accountMapper.update(account);
|
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()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user