Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public String selectCountByExample(MappedStatement ms) {
if (isCheckExampleEntityClass()) {
sql.append(SqlHelper.exampleCheck(entityClass));
}
if (getConfig().isSafeSelect()) {
sql.append(SqlHelper.exampleHasAtLeastOneCriteriaCheck("_parameter"));
}
sql.append(SqlHelper.exampleCountColumn(entityClass));
sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
sql.append(SqlHelper.exampleWhereClause());
Expand Down Expand Up @@ -105,6 +108,9 @@ public String selectByExample(MappedStatement ms) {
if (isCheckExampleEntityClass()) {
sql.append(SqlHelper.exampleCheck(entityClass));
}
if (getConfig().isSafeSelect()) {
sql.append(SqlHelper.exampleHasAtLeastOneCriteriaCheck("_parameter"));
}
sql.append("<if test=\"distinct\">distinct</if>");
//支持查询指定列
sql.append(SqlHelper.exampleSelectColumns(entityClass));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public String delete(MappedStatement ms) {
StringBuilder sql = new StringBuilder();
//如果设置了安全删除,就不允许执行不带查询条件的 delete 方法
if (getConfig().isSafeDelete()) {
sql.append(SqlHelper.notAllNullParameterCheck("_parameter", EntityHelper.getColumns(entityClass)));
sql.append(SqlHelper.notAllNullParameterCheck("_parameter", EntityHelper.getColumns(entityClass), isNotEmpty()));
}
// 如果是逻辑删除,则修改为更新表,修改逻辑删除字段的值
if (SqlHelper.hasLogicDeleteColumn(entityClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package tk.mybatis.mapper.provider.base;

import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;
Expand All @@ -51,6 +52,9 @@ public String selectOne(MappedStatement ms) {
//修改返回值类型为实体类型
setResultType(ms, entityClass);
StringBuilder sql = new StringBuilder();
if (getConfig().isSafeSelect()) {
sql.append(SqlHelper.notAllNullParameterCheck("_parameter", EntityHelper.getColumns(entityClass), isNotEmpty()));
}
sql.append(SqlHelper.selectAllColumns(entityClass));
sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
sql.append(SqlHelper.whereAllIfColumns(entityClass, isNotEmpty()));
Expand All @@ -68,6 +72,9 @@ public String select(MappedStatement ms) {
//修改返回值类型为实体类型
setResultType(ms, entityClass);
StringBuilder sql = new StringBuilder();
if (getConfig().isSafeSelect()) {
sql.append(SqlHelper.notAllNullParameterCheck("_parameter", EntityHelper.getColumns(entityClass), isNotEmpty()));
}
sql.append(SqlHelper.selectAllColumns(entityClass));
sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
sql.append(SqlHelper.whereAllIfColumns(entityClass, isNotEmpty()));
Expand Down Expand Up @@ -110,6 +117,9 @@ public String selectByPrimaryKey(MappedStatement ms) {
public String selectCount(MappedStatement ms) {
Class<?> entityClass = getEntityClass(ms);
StringBuilder sql = new StringBuilder();
if (getConfig().isSafeSelect()) {
sql.append(SqlHelper.notAllNullParameterCheck("_parameter", EntityHelper.getColumns(entityClass), isNotEmpty()));
}
sql.append(SqlHelper.selectCount(entityClass));
sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
sql.append(SqlHelper.whereAllIfColumns(entityClass, isNotEmpty()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package tk.mybatis.mapper.provider.base;

import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;
Expand All @@ -48,6 +49,9 @@ public BaseUpdateProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
public String updateByPrimaryKey(MappedStatement ms) {
Class<?> entityClass = getEntityClass(ms);
StringBuilder sql = new StringBuilder();
if (getConfig().isSafeUpdate()) {
sql.append(SqlHelper.notAllNullParameterCheck("_parameter", EntityHelper.getPKColumns(entityClass), isNotEmpty()));
}
sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
sql.append(SqlHelper.updateSetColumns(entityClass, null, false, false));
sql.append(SqlHelper.wherePKColumns(entityClass, true));
Expand All @@ -63,6 +67,9 @@ public String updateByPrimaryKey(MappedStatement ms) {
public String updateByPrimaryKeySelective(MappedStatement ms) {
Class<?> entityClass = getEntityClass(ms);
StringBuilder sql = new StringBuilder();
if (getConfig().isSafeUpdate()) {
sql.append(SqlHelper.notAllNullParameterCheck("_parameter", EntityHelper.getPKColumns(entityClass), isNotEmpty()));
}
sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
sql.append(SqlHelper.updateSetColumns(entityClass, null, true, isNotEmpty()));
sql.append(SqlHelper.wherePKColumns(entityClass, true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class SafeDeleteByFieldTest extends BaseTest {
protected Config getConfig() {
Config config = super.getConfig();
config.setSafeDelete(true);
config.setNotEmpty(true);
return config;
}

Expand All @@ -40,6 +41,19 @@ public void testSafeDeleteNull() {
}
}

@Test(expected = PersistenceException.class)
public void testSafeDeleteEmptyString() {
SqlSession sqlSession = getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Country country = new Country();
country.setCountryname("");
mapper.delete(country);
} finally {
sqlSession.close();
}
}

@Test(expected = PersistenceException.class)
public void testSafeDeleteByExample() {
SqlSession sqlSession = getSqlSession();
Expand All @@ -62,4 +76,17 @@ public void testSafeDeleteByExampleNull() {
}
}

@Test(expected = PersistenceException.class)
public void testSafeDeleteByExampleWithEmptyStringCriterion() {
SqlSession sqlSession = getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = new Example(Country.class);
example.createCriteria().andEqualTo("countryname", "");
mapper.deleteByExample(example);
} finally {
sqlSession.close();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class SafeDeleteByMethodTest extends BaseTest {
protected Config getConfig() {
Config config = super.getConfig();
config.setSafeDelete(true);
config.setNotEmpty(true);
//和 SafeDeleteByFieldTest 测试的区别在此,这里将会使后面调用 EntityField.getValue 时,使用 getter 方法获取值
config.setEnableMethodAnnotation(true);
return config;
Expand Down Expand Up @@ -42,6 +43,19 @@ public void testSafeDeleteNull() {
}
}

@Test(expected = PersistenceException.class)
public void testSafeDeleteEmptyString() {
SqlSession sqlSession = getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Country country = new Country();
country.setCountryname("");
mapper.delete(country);
} finally {
sqlSession.close();
}
}


@Test(expected = PersistenceException.class)
public void testSafeDeleteByExample() {
Expand All @@ -65,6 +79,19 @@ public void testSafeDeleteByExampleNull() {
}
}

@Test(expected = PersistenceException.class)
public void testSafeDeleteByExampleWithEmptyStringCriterion() {
SqlSession sqlSession = getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = new Example(Country.class);
example.createCriteria().andEqualTo("countryname", "");
mapper.deleteByExample(example);
} finally {
sqlSession.close();
}
}

@Test(expected = PersistenceException.class)
public void testSafeDeleteByExampleWithNullValueCriterion() {
SqlSession sqlSession = getSqlSession();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package tk.mybatis.mapper.base.select;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import org.junit.Assert;
import org.junit.Test;
import tk.mybatis.mapper.base.BaseTest;
import tk.mybatis.mapper.base.Country;
import tk.mybatis.mapper.base.CountryMapper;
import tk.mybatis.mapper.entity.Config;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.model.CountryExample;

public class SafeSelectByFieldTest extends BaseTest {

@Override
protected Config getConfig() {
Config config = super.getConfig();
config.setSafeSelect(true);
config.setNotEmpty(true);
return config;
}

@Test(expected = PersistenceException.class)
public void testSafeSelect() {
SqlSession sqlSession = getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
mapper.select(new Country());
} finally {
sqlSession.close();
}
}

@Test(expected = PersistenceException.class)
public void testSafeSelectNull() {
SqlSession sqlSession = getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
mapper.select(null);
} finally {
sqlSession.close();
}
}

@Test(expected = PersistenceException.class)
public void testSafeSelectEmptyString() {
SqlSession sqlSession = getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Country country = new Country();
country.setCountryname("");
mapper.select(country);
} finally {
sqlSession.close();
}
}

@Test(expected = PersistenceException.class)
public void testSafeSelectCountEmptyString() {
SqlSession sqlSession = getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Country country = new Country();
country.setCountryname("");
mapper.selectCount(country);
} finally {
sqlSession.close();
}
}

@Test(expected = PersistenceException.class)
public void testSafeSelectByExample() {
SqlSession sqlSession = getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
mapper.selectByExample(new Example(Country.class));
} finally {
sqlSession.close();
}
}

@Test(expected = PersistenceException.class)
public void testSafeSelectByExampleNull() {
SqlSession sqlSession = getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
mapper.selectByExample(null);
} finally {
sqlSession.close();
}
}

@Test(expected = PersistenceException.class)
public void testSafeSelectByExampleWithEmptyStringCriterion() {
SqlSession sqlSession = getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = new Example(Country.class);
example.createCriteria().andEqualTo("countryname", "");
mapper.selectByExample(example);
} finally {
sqlSession.close();
}
}

@Test(expected = PersistenceException.class)
public void testSafeSelectByGeneratedExampleWithEmptyStringCriterion() {
SqlSession sqlSession = getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
CountryExample example = new CountryExample();
example.createCriteria().andCountrynameEqualTo("");
mapper.selectByExample(example);
} finally {
sqlSession.close();
}
}

@Test
Comment on lines +119 to +120
public void testSafeSelectWithCondition() {
SqlSession sqlSession = getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Country country = new Country();
country.setCountryname("China");
Assert.assertEquals(1, mapper.select(country).size());

Example example = new Example(Country.class);
example.createCriteria().andEqualTo("countryname", "China");
Assert.assertEquals(1, mapper.selectByExample(example).size());

CountryExample generatedExample = new CountryExample();
generatedExample.createCriteria().andCountrynameEqualTo("China");
Assert.assertEquals(1, mapper.selectByExample(generatedExample).size());

Assert.assertEquals(183, mapper.selectAll().size());
} finally {
sqlSession.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ protected Config getConfig() {
return config;
}

@Test(expected = PersistenceException.class)
public void testSafeUpdateByPrimaryKeyNull() {
SqlSession sqlSession = getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
mapper.updateByPrimaryKey(new Country());
} finally {
sqlSession.close();
}
}

@Test(expected = PersistenceException.class)
public void testSafeUpdateByPrimaryKeySelectiveNull() {
SqlSession sqlSession = getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
mapper.updateByPrimaryKeySelective(new Country());
} finally {
sqlSession.close();
}
}

@Test(expected = PersistenceException.class)
public void testSafeUpdate() {
SqlSession sqlSession = getSqlSession();
Expand All @@ -29,6 +51,19 @@ public void testSafeUpdate() {
}
}

@Test(expected = PersistenceException.class)
public void testSafeUpdateWithEmptyStringCriterion() {
SqlSession sqlSession = getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = new Example(Country.class);
example.createCriteria().andEqualTo("countryname", "");
mapper.updateByExample(new Country(), example);
} finally {
sqlSession.close();
}
}

@Test(expected = PersistenceException.class)
public void testSafeUpdateNull() {
SqlSession sqlSession = getSqlSession();
Expand Down
Loading
Loading