Skip to content

Commit

Permalink
简化Example的xml逻辑,解决由于and,or位置错误导致Example使用空条件时的错误,完善测试
Browse files Browse the repository at this point in the history
  • Loading branch information
abel533 committed Jul 18, 2017
1 parent c345985 commit c2a8312
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/main/java/tk/mybatis/mapper/mapperhelper/SqlHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,9 @@ public static String exampleCheck(Class<?> entityClass) {
public static String exampleWhereClause() {
return "<if test=\"_parameter != null\">" +
"<where>\n" +
"<trim prefixOverrides=\"and |or \">\n" +
" <foreach collection=\"oredCriteria\" item=\"criteria\">\n" +
" ${@tk.mybatis.mapper.util.OGNL@andOr(criteria)} " +
" <if test=\"criteria.valid\">\n" +
" ${@tk.mybatis.mapper.util.OGNL@andOr(criteria)}" +
" <trim prefix=\"(\" prefixOverrides=\"and |or \" suffix=\")\">\n" +
" <foreach collection=\"criteria.criteria\" item=\"criterion\">\n" +
" <choose>\n" +
Expand All @@ -639,7 +638,6 @@ public static String exampleWhereClause() {
" </trim>\n" +
" </if>\n" +
" </foreach>\n" +
"</trim>\n" +
"</where>" +
"</if>";
}
Expand All @@ -651,10 +649,9 @@ public static String exampleWhereClause() {
*/
public static String updateByExampleWhereClause() {
return "<where>\n" +
"<trim prefixOverrides=\"and |or \">\n" +
" <foreach collection=\"example.oredCriteria\" item=\"criteria\">\n" +
" ${@tk.mybatis.mapper.util.OGNL@andOr(criteria)} " +
" <if test=\"criteria.valid\">\n" +
" ${@tk.mybatis.mapper.util.OGNL@andOr(criteria)}" +
" <trim prefix=\"(\" prefixOverrides=\"and |or \" suffix=\")\">\n" +
" <foreach collection=\"criteria.criteria\" item=\"criterion\">\n" +
" <choose>\n" +
Expand All @@ -678,7 +675,6 @@ public static String updateByExampleWhereClause() {
" </trim>\n" +
" </if>\n" +
" </foreach>\n" +
"</trim>\n" +
"</where>";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,31 @@ public void testExcludeColumnsByExample() {
}
}

@Test
public void testAndOr() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = new Example(Country.class);
example.createCriteria().andGreaterThan("id", 100).andLessThan("id", 151);
example.or().andLessThan("id", 41);
List<Country> countries = mapper.selectByExample(example);
//查询总数
Assert.assertEquals(90, countries.size());

//当不使用条件时,也不能出错
example = new Example(Country.class);
example.createCriteria();
example.or();
example.and();
countries = mapper.selectByExample(example);
//查询总数
Assert.assertEquals(183, countries.size());
} finally {
sqlSession.close();
}
}

@Test
public void testOrderBy() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
Expand Down

0 comments on commit c2a8312

Please sign in to comment.