Skip to content

Commit 9637270

Browse files
committed
解决Example查询in,notin无效的bug#24
1 parent 4aa1d7b commit 9637270

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

src/main/java/com/github/abel533/mapperhelper/MapperTemplate.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,18 +457,18 @@ protected void newSelectKeyMappedStatement(MappedStatement ms, EntityHelper.Enti
457457

458458
public IfSqlNode ExampleValidSqlNode(Configuration configuration) {
459459
List<SqlNode> whenSqlNodes = new ArrayList<SqlNode>();
460-
IfSqlNode noValueSqlNode = new IfSqlNode(new TextSqlNode(" and ${criterion.condition}"), "criterion.noValue");
460+
IfSqlNode noValueSqlNode = new IfSqlNode(new TextSqlNode("and ${criterion.condition}"), "criterion.noValue");
461461
whenSqlNodes.add(noValueSqlNode);
462-
IfSqlNode singleValueSqlNode = new IfSqlNode(new TextSqlNode(" and ${criterion.condition} #{criterion.value}"), "criterion.singleValue");
462+
IfSqlNode singleValueSqlNode = new IfSqlNode(new TextSqlNode("and ${criterion.condition} #{criterion.value}"), "criterion.singleValue");
463463
whenSqlNodes.add(singleValueSqlNode);
464-
IfSqlNode betweenValueSqlNode = new IfSqlNode(new TextSqlNode(" and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}"), "criterion.betweenValue");
464+
IfSqlNode betweenValueSqlNode = new IfSqlNode(new TextSqlNode("and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}"), "criterion.betweenValue");
465465
whenSqlNodes.add(betweenValueSqlNode);
466466

467467
List<SqlNode> listValueContentSqlNodes = new ArrayList<SqlNode>();
468-
listValueContentSqlNodes.add(new TextSqlNode(" and ${criterion.condition}"));
468+
listValueContentSqlNodes.add(new TextSqlNode("and ${criterion.condition}"));
469469
ForEachSqlNode listValueForEachSqlNode = new ForEachSqlNode(configuration, new StaticTextSqlNode("#{listItem}"), "criterion.value", null, "listItem", "(", ")", ",");
470470
listValueContentSqlNodes.add(listValueForEachSqlNode);
471-
IfSqlNode listValueSqlNode = new IfSqlNode(new MixedSqlNode(listValueContentSqlNodes), "criterion.noValue");
471+
IfSqlNode listValueSqlNode = new IfSqlNode(new MixedSqlNode(listValueContentSqlNodes), "criterion.listValue");
472472
whenSqlNodes.add(listValueSqlNode);
473473

474474
ChooseSqlNode chooseSqlNode = new ChooseSqlNode(whenSqlNodes, null);

src/test/java/com/github/abel533/entity/test/TestExample.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.junit.Assert;
3434
import org.junit.Test;
3535

36+
import java.util.Arrays;
3637
import java.util.List;
3738

3839
/**
@@ -115,6 +116,24 @@ public void testSelectByExample() {
115116
}
116117
}
117118

119+
@Test
120+
public void testSelectByExampleInNotIn() {
121+
SqlSession sqlSession = MybatisHelper.getSqlSession();
122+
try {
123+
CommonMapper commonMapper = sqlSession.getMapper(CommonMapper.class);
124+
EntityMapper entityMapper = new EntityMapper(commonMapper);
125+
126+
Example example = new Example(Country.class);
127+
example.createCriteria().andIn("id", Arrays.asList(new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}))
128+
.andNotIn("id", Arrays.asList(new Object[]{11}));
129+
List<Country> countries = entityMapper.selectByExample(example);
130+
//查询总数
131+
Assert.assertEquals(10, countries.size());
132+
} finally {
133+
sqlSession.close();
134+
}
135+
}
136+
118137
@Test
119138
public void testUpdateByExampleSelective() {
120139
SqlSession sqlSession = MybatisHelper.getSqlSession();

src/test/java/com/github/abel533/test/example/TestSelectByExample.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.junit.Assert;
1010
import org.junit.Test;
1111

12+
import java.util.Arrays;
1213
import java.util.List;
1314

1415
/**
@@ -32,6 +33,22 @@ public void testSelectByExample() {
3233
}
3334
}
3435

36+
@Test
37+
public void testSelectByExampleInNotIn() {
38+
SqlSession sqlSession = MybatisHelper.getSqlSession();
39+
try {
40+
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
41+
Example example = new Example(Country.class);
42+
example.createCriteria().andIn("id", Arrays.asList(new Object[]{1,2,3,4,5,6,7,8,9,10,11}))
43+
.andNotIn("id", Arrays.asList(new Object[]{11}));
44+
List<Country> countries = mapper.selectByExample(example);
45+
//查询总数
46+
Assert.assertEquals(10, countries.size());
47+
} finally {
48+
sqlSession.close();
49+
}
50+
}
51+
3552
@Test
3653
public void testSelectByExample2() {
3754
SqlSession sqlSession = MybatisHelper.getSqlSession();

0 commit comments

Comments
 (0)