Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

模仿Example.Builder中Sqls构建方法 添加WeekendSqls重载方法 添加测试类 #207

Merged
merged 1 commit into from
Jan 22, 2018
Merged
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-weekend</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
Expand Down
27 changes: 24 additions & 3 deletions src/main/java/tk/mybatis/mapper/entity/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.util.Sqls;
import tk.mybatis.mapper.util.StringUtil;
import tk.mybatis.mapper.weekend.WeekendSqls;

import java.util.*;

Expand Down Expand Up @@ -1041,22 +1042,42 @@ public Builder where(Sqls sqls) {
Sqls.Criteria criteria = sqls.getCriteria();
criteria.setAndOr("and");
this.sqlsCriteria.add(criteria);
return this;
return this;
}

public Builder where(WeekendSqls sqls) {
Sqls.Criteria criteria = sqls.getCriteria();
criteria.setAndOr("and");
this.sqlsCriteria.add(criteria);
return this;
}

public Builder andWhere(Sqls sqls) {
Sqls.Criteria criteria = sqls.getCriteria();
criteria.setAndOr("and");
this.sqlsCriteria.add(criteria);
return this;
return this;
}

public Builder andWhere(WeekendSqls sqls) {
Sqls.Criteria criteria = sqls.getCriteria();
criteria.setAndOr("and");
this.sqlsCriteria.add(criteria);
return this;
}

public Builder orWhere(Sqls sqls) {
Sqls.Criteria criteria = sqls.getCriteria();
criteria.setAndOr("or");
this.sqlsCriteria.add(criteria);
return this;
return this;
}

public Builder orWhere(WeekendSqls sqls) {
Sqls.Criteria criteria = sqls.getCriteria();
criteria.setAndOr("or");
this.sqlsCriteria.add(criteria);
return this;
}

public Builder orderBy(String... properties) {
Expand Down
69 changes: 69 additions & 0 deletions src/test/java/tk/mybatis/mapper/test/weekend/WeekendSqlsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package tk.mybatis.mapper.test.weekend;

/*
import org.apache.ibatis.session.SqlSession;
import org.junit.Assert;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.mapper.CountryMapper;
import tk.mybatis.mapper.mapper.MybatisHelper;
import tk.mybatis.mapper.model.Country;
import tk.mybatis.mapper.util.Sqls;
import tk.mybatis.mapper.weekend.WeekendSqls;

import java.util.List;
*/

/**
* 测试WeekendSql构建者模式类 由于方法引用需要jdk8
* 执行该测试的时候需要临时将pom.xml中maven-compiler-plugin插件jdk编译等级调整为1.8
* 为了防止jdk6编译等级打包出错,将测试用例全部注释
* <p>
* <pre>
* <plugin>
* <artifactId>maven-compiler-plugin</artifactId>
* <configuration>
* <source>1.8</source>
* <target>1.8</target>
* </configuration>
* </plugin>
* </pre>
*
* @author XuYin
*/
public class WeekendSqlsTest {
//@Test
/*
public void testWeekend() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
List<Country> selectByExample = mapper.selectByExample(
new Example.Builder(Country.class).where(Sqls.custom().andLike("countryname", "China")).build());
List<Country> selectByWeekendSql = mapper.selectByExample(new Example.Builder(Country.class)
.where(WeekendSqls.<Country>custom().andLike(Country::getCountryname, "China")).build());
// 判断两个结果数组内容是否相同
Assert.assertArrayEquals(selectByExample.toArray(), selectByWeekendSql.toArray());
} finally {
sqlSession.close();
}
}

//@Test
public void testWeekendComplex() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
List<Country> selectByExample = mapper.selectByExample(new Example.Builder(Country.class)
.where(Sqls.custom().andLike("countryname", "%a%").andGreaterThan("countrycode", "123")).build());
List<Country> selectByWeekendSql = mapper.selectByExample(new Example.Builder(Country.class)
.where(WeekendSqls.<Country>custom().andLike(Country::getCountryname, "%a%")
.andGreaterThan(Country::getCountrycode, "123"))
.build());
// 判断两个结果数组内容是否相同
Assert.assertArrayEquals(selectByExample.toArray(), selectByWeekendSql.toArray());
} finally {
sqlSession.close();
}
}
*/
}