通用Mapper新特性:ExampleBuilder 2017年12月18日
package tk.mybatis.mapper.test.example;
import org.apache.ibatis.session.SqlSession;
import org.junit.Assert;
import org.junit.Test;
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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author wuyi
* @date 2017/11/18
*/
public class TestExampleBuilder {
@Test
public void testExampleBuilder() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class).build();
List<Country> countries = mapper.selectByExample(example);
Assert.assertEquals(183, countries.size());
// 下面的查询会有缓存
Example example0 = Example.builder(Country.class)
.select().build();
List<Country> countries0 = mapper.selectByExample(example0);
Assert.assertEquals(183, countries0.size());
} finally {
sqlSession.close();
}
}
@Test
public void testDistinct() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.distinct()
.build();
List<Country> countries = mapper.selectByExample(example);
Assert.assertEquals(183, countries.size());
// distinct和order by冲突问题
Example example0 = Example.builder(Country.class)
.selectDistinct("id", "countryname").build();
List<Country> countries0 = mapper.selectByExample(example0);
Assert.assertEquals(183, countries0.size());
} finally {
sqlSession.close();
}
}
@Test
public void testForUpdate() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.select("countryname")
.where(Sqls.custom().andGreaterThan("id", 100))
.orderByAsc("countrycode")
.forUpdate()
.build();
List<Country> countries = mapper.selectByExample(example);
Assert.assertEquals(83, countries.size());
} finally {
sqlSession.close();
}
}
@Test
public void testEqualTo() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.where(Sqls.custom().andEqualTo("id", "35"))
.build();
List<Country> countries = mapper.selectByExample(example);
Country country = countries.get(0);
Assert.assertEquals(Integer.valueOf(35), country.getId());
Assert.assertEquals("China", country.getCountryname());
Assert.assertEquals("CN", country.getCountrycode());
} finally {
sqlSession.close();
}
}
@Test
public void testBetween() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.where(Sqls.custom().andBetween("id", 34, 35))
.build();
List<Country> countries = mapper.selectByExample(example);
Country country35 = countries.get(0);
Assert.assertEquals(Integer.valueOf(35), country35.getId());
Assert.assertEquals("China", country35.getCountryname());
Assert.assertEquals("CN", country35.getCountrycode());
Country country34 = countries.get(1);
Assert.assertEquals(Integer.valueOf(34), country34.getId());
Assert.assertEquals("Chile", country34.getCountryname());
Assert.assertEquals("CL", country34.getCountrycode());
} finally {
sqlSession.close();
}
}
@Test
public void testIn() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.where(Sqls.custom().andIn("id", new ArrayList<Integer>(Arrays.asList(35, 183))))
.build();
List<Country> countries = mapper.selectByExample(example);
Country country35 = countries.get(1);
Assert.assertEquals(Integer.valueOf(35), country35.getId());
Assert.assertEquals("China", country35.getCountryname());
Assert.assertEquals("CN", country35.getCountrycode());
Country country183 = countries.get(0);
Assert.assertEquals(Integer.valueOf(183), country183.getId());
Assert.assertEquals("Zambia", country183.getCountryname());
Assert.assertEquals("ZM", country183.getCountrycode());
} finally {
sqlSession.close();
}
}
/*
* @description: 单个where组合查询测试
* 直接把example的构造放到selectByExample()函数里
* */
@Test
public void testWhereCompound0() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
List<Country> countries = mapper.selectByExample(
Example.builder(Country.class)
.where(Sqls.custom()
.andEqualTo("countryname", "China")
.andEqualTo("id", 35)
.orIn("id", new ArrayList<Integer>(Arrays.asList(35, 183)))
.orLike("countryname","Ye%")
)
.build());
Country country35 = countries.get(2);
Assert.assertEquals(Integer.valueOf(35), country35.getId());
Assert.assertEquals("China", country35.getCountryname());
Assert.assertEquals("CN", country35.getCountrycode());
Country country183 = countries.get(0);
Assert.assertEquals(Integer.valueOf(183), country183.getId());
Assert.assertEquals("Zambia", country183.getCountryname());
Assert.assertEquals("ZM", country183.getCountrycode());
Country country179 = countries.get(1);
Assert.assertEquals(Integer.valueOf(179), country179.getId());
Assert.assertEquals("Yemen", country179.getCountryname());
Assert.assertEquals("YE", country179.getCountrycode());
} finally {
sqlSession.close();
}
}
/*
* @description: 单个where组合查询测试
* */
@Test
public void testWhereCompound1() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.where(Sqls.custom()
.andBetween("id", 35, 50)
.orLessThan("id", 40)
.orIsNull("countryname")
)
.build();
List<Country> countries = mapper.selectByExample(example);
Assert.assertEquals(50, countries.size());
} finally {
sqlSession.close();
}
}
/*
* @description: 多个where连接的查询语句测试
* */
@Test
public void testWhereAndWhereCompound() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.where(Sqls.custom()
.andEqualTo("countryname", "China")
.andEqualTo("id", 35)
)
.andWhere(Sqls.custom()
.andEqualTo("id", 183)
)
.build();
List<Country> countries = mapper.selectByExample(example);
Assert.assertEquals(0, countries.size());
} finally {
sqlSession.close();
}
}
/*
* @description: 多个where连接的查询语句测试
* */
@Test
public void testWhereOrWhereCompound() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.where(Sqls.custom()
.andEqualTo("countryname", "China")
.andEqualTo("id", 35)
)
.orWhere(Sqls.custom()
.andEqualTo("id", 183)
)
.build();
List<Country> countries = mapper.selectByExample(example);
Assert.assertEquals(2, countries.size());
} finally {
sqlSession.close();
}
}
/*
* @description: 多个where连接的查询语句测试
* */
@Test
public void testMultiWhereCompound() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.selectDistinct()
.where(Sqls.custom()
.andEqualTo("countryname", "China")
.andEqualTo("id", 35)
)
.orWhere(Sqls.custom()
.andBetween("countryname", 'C', 'H')
.andNotLike("countryname", "Co%")
)
.andWhere(Sqls.custom()
.andLessThan("id", "100")
.orGreaterThan("id", "55")
)
.orWhere(Sqls.custom()
.andEqualTo("countryname", "Cook Is.")
)
.orderByAsc("id", "countryname")
.orderByDesc("countrycode")
.forUpdate()
.build();
List<Country> countries = mapper.selectByExample(example);
Assert.assertEquals(35, countries.size());
} finally {
sqlSession.close();
}
}
/*
* @description: 测试order by
* orderBy()默认为Asc(升序),与orderByAsc()一样
* */
@Test
public void testOrderBy() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.where(Sqls.custom().andBetween("id", 50, 55))
.orderBy("id").orderByAsc("countryname").orderByDesc("countrycode")
.build();
List<Country> countries = mapper.selectByExample(example);
for (Country country :countries) {
System.out.println(country.getId() + " " + country.getCountryname() + " " + country.getCountrycode());
}
Assert.assertEquals(6, countries.size());
} finally {
sqlSession.close();
}
}
}
import tk.mybatis.mapper.util.Sqls;
package tk.mybatis.mapper.test.example;
import org.apache.ibatis.session.SqlSession;
import org.junit.Assert;
import org.junit.Test;
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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author wuyi
* @date 2017/11/18
*/
public class TestExampleBuilder {
@Test
public void testExampleBuilder() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class).build();
List<Country> countries = mapper.selectByExample(example);
Assert.assertEquals(183, countries.size());
// 下面的查询会有缓存
Example example0 = Example.builder(Country.class)
.select().build();
List<Country> countries0 = mapper.selectByExample(example0);
Assert.assertEquals(183, countries0.size());
} finally {
sqlSession.close();
}
}
@Test
public void testDistinct() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.distinct()
.build();
List<Country> countries = mapper.selectByExample(example);
Assert.assertEquals(183, countries.size());
// distinct和order by冲突问题
Example example0 = Example.builder(Country.class)
.selectDistinct("id", "countryname").build();
List<Country> countries0 = mapper.selectByExample(example0);
Assert.assertEquals(183, countries0.size());
} finally {
sqlSession.close();
}
}
@Test
public void testForUpdate() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.select("countryname")
.where(Sqls.custom().andGreaterThan("id", 100))
.orderByAsc("countrycode")
.forUpdate()
.build();
List<Country> countries = mapper.selectByExample(example);
Assert.assertEquals(83, countries.size());
} finally {
sqlSession.close();
}
}
@Test
public void testEqualTo() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.where(Sqls.custom().andEqualTo("id", "35"))
.build();
List<Country> countries = mapper.selectByExample(example);
Country country = countries.get(0);
Assert.assertEquals(Integer.valueOf(35), country.getId());
Assert.assertEquals("China", country.getCountryname());
Assert.assertEquals("CN", country.getCountrycode());
} finally {
sqlSession.close();
}
}
@Test
public void testBetween() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.where(Sqls.custom().andBetween("id", 34, 35))
.build();
List<Country> countries = mapper.selectByExample(example);
Country country35 = countries.get(0);
Assert.assertEquals(Integer.valueOf(35), country35.getId());
Assert.assertEquals("China", country35.getCountryname());
Assert.assertEquals("CN", country35.getCountrycode());
Country country34 = countries.get(1);
Assert.assertEquals(Integer.valueOf(34), country34.getId());
Assert.assertEquals("Chile", country34.getCountryname());
Assert.assertEquals("CL", country34.getCountrycode());
} finally {
sqlSession.close();
}
}
@Test
public void testIn() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.where(Sqls.custom().andIn("id", new ArrayList<Integer>(Arrays.asList(35, 183))))
.build();
List<Country> countries = mapper.selectByExample(example);
Country country35 = countries.get(1);
Assert.assertEquals(Integer.valueOf(35), country35.getId());
Assert.assertEquals("China", country35.getCountryname());
Assert.assertEquals("CN", country35.getCountrycode());
Country country183 = countries.get(0);
Assert.assertEquals(Integer.valueOf(183), country183.getId());
Assert.assertEquals("Zambia", country183.getCountryname());
Assert.assertEquals("ZM", country183.getCountrycode());
} finally {
sqlSession.close();
}
}
/*
* @description: 单个where组合查询测试
* 直接把example的构造放到selectByExample()函数里
* */
@Test
public void testWhereCompound0() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
List<Country> countries = mapper.selectByExample(
Example.builder(Country.class)
.where(Sqls.custom()
.andEqualTo("countryname", "China")
.andEqualTo("id", 35)
.orIn("id", new ArrayList<Integer>(Arrays.asList(35, 183)))
.orLike("countryname","Ye%")
)
.build());
Country country35 = countries.get(2);
Assert.assertEquals(Integer.valueOf(35), country35.getId());
Assert.assertEquals("China", country35.getCountryname());
Assert.assertEquals("CN", country35.getCountrycode());
Country country183 = countries.get(0);
Assert.assertEquals(Integer.valueOf(183), country183.getId());
Assert.assertEquals("Zambia", country183.getCountryname());
Assert.assertEquals("ZM", country183.getCountrycode());
Country country179 = countries.get(1);
Assert.assertEquals(Integer.valueOf(179), country179.getId());
Assert.assertEquals("Yemen", country179.getCountryname());
Assert.assertEquals("YE", country179.getCountrycode());
} finally {
sqlSession.close();
}
}
/*
* @description: 单个where组合查询测试
* */
@Test
public void testWhereCompound1() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.where(Sqls.custom()
.andBetween("id", 35, 50)
.orLessThan("id", 40)
.orIsNull("countryname")
)
.build();
List<Country> countries = mapper.selectByExample(example);
Assert.assertEquals(50, countries.size());
} finally {
sqlSession.close();
}
}
/*
* @description: 多个where连接的查询语句测试
* */
@Test
public void testWhereAndWhereCompound() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.where(Sqls.custom()
.andEqualTo("countryname", "China")
.andEqualTo("id", 35)
)
.andWhere(Sqls.custom()
.andEqualTo("id", 183)
)
.build();
List<Country> countries = mapper.selectByExample(example);
Assert.assertEquals(0, countries.size());
} finally {
sqlSession.close();
}
}
/*
* @description: 多个where连接的查询语句测试
* */
@Test
public void testWhereOrWhereCompound() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.where(Sqls.custom()
.andEqualTo("countryname", "China")
.andEqualTo("id", 35)
)
.orWhere(Sqls.custom()
.andEqualTo("id", 183)
)
.build();
List<Country> countries = mapper.selectByExample(example);
Assert.assertEquals(2, countries.size());
} finally {
sqlSession.close();
}
}
/*
* @description: 多个where连接的查询语句测试
* */
@Test
public void testMultiWhereCompound() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.selectDistinct()
.where(Sqls.custom()
.andEqualTo("countryname", "China")
.andEqualTo("id", 35)
)
.orWhere(Sqls.custom()
.andBetween("countryname", 'C', 'H')
.andNotLike("countryname", "Co%")
)
.andWhere(Sqls.custom()
.andLessThan("id", "100")
.orGreaterThan("id", "55")
)
.orWhere(Sqls.custom()
.andEqualTo("countryname", "Cook Is.")
)
.orderByAsc("id", "countryname")
.orderByDesc("countrycode")
.forUpdate()
.build();
List<Country> countries = mapper.selectByExample(example);
Assert.assertEquals(35, countries.size());
} finally {
sqlSession.close();
}
}
/*
* @description: 测试order by
* orderBy()默认为Asc(升序),与orderByAsc()一样
* */
@Test
public void testOrderBy() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Example example = Example.builder(Country.class)
.where(Sqls.custom().andBetween("id", 50, 55))
.orderBy("id").orderByAsc("countryname").orderByDesc("countrycode")
.build();
List<Country> countries = mapper.selectByExample(example);
for (Country country :countries) {
System.out.println(country.getId() + " " + country.getCountryname() + " " + country.getCountrycode());
}
Assert.assertEquals(6, countries.size());
} finally {
sqlSession.close();
}
}
}
通用Mapper新特性:ExampleBuilder 2017年12月18日的更多相关文章
- 12月18日风险投资速递:Facebook收购实时体育数据提供商Sport Stream
国内公司 1.手游公司成都掌沃无限获得近千万元天使投资 成都掌沃无限成立于2013年,是一家新成立的手机游戏开发商,创始人及CEO张涛拥有超过10年的游戏行业从业经验和连续创业经历,其首款游戏产品为& ...
- 2016年12月18日 星期日 --出埃及记 Exodus 21:13
2016年12月18日 星期日 --出埃及记 Exodus 21:13 However, if he does not do it intentionally, but God lets it hap ...
- 北京Uber优步司机奖励政策(12月18日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 深度学习DeepLearning技术实战(12月18日---21日)
12月线上课程报名中 深度学习DeepLearning(Python)实战培训班 时间地点: 2020 年 12 月 18 日-2020 年 12 月 21日 (第一天报到 授课三天:提前环境部署 电 ...
- 2017年12月17日 ASP.NET 12个表单元素&&简单控件/复合控件
12个表单元素可以分为三大类 第一类:文本类 <input type = "text" /> //普通文本框 <input type = "passwo ...
- 2017年12月24日 JS跟Jquery基础
js基础 alert();confirm(); 基础语法:与C#一致数据类型及类型转换var (string,decimal)parseInt()parseFloat();isNaN(); 运算符:数 ...
- 2017年12月16日 ASP.NET基本用法
ASP.NET初级添加 利用css代码跟ASP.NET还有Javascript原生,LinQ来写增跟展示数据 首先介绍一个非常好用的控件,灵活并且循环展示数据库里面的数据 <asp:Repeat ...
- 2017年12月14日 LinQ高级查&&Asp.net WebForm Asp.net MVC
LinQ的高级查询用法 开头:StartsWith()结尾:EndsWith()模糊:Contains() 个数:Count最大值:Max(r => r.price)最小值:Min(r => ...
- 2017年12月13日 LinQ用法基本的增删改查
LinQ是什么? LinQ是语言集成的查询,是用于C#跟Vb的扩展语言 LinQ的用法 新建一个App_Code文件夹,在文件夹下添加一个数据LinQ to SQL类,可以直接直接点击服务器管理器然后 ...
随机推荐
- Java转型
集合转型 通过中间类型List List<String> strs=new ArrayList<>(); List list=(List)strs; List<Objec ...
- IntelliJ IDEA无法更新maven索引
maven索引的作用时在添加dependency的时候能有自动提示,不影响dependency的下载: 解决办法: 1.http://ju.outofmemory.cn/entry/359450 2. ...
- MPU和CPU有什么区别?
MPU(或称MP,微处理器)和CPU(中央处理器)同为处理器,但范畴不同. 计算机(即电脑)分为巨型机,大型机,中型机,小型机和微型计算机5类.这5类计算机的运算核心统称为CPU,而MPU只是微型计算 ...
- Shooting Contest 射击比赛 [POJ1719] [CEOI1997] [一题多解]
Description(下有中文题意) Welcome to the Annual Byteland Shooting Contest. Each competitor will shoot to a ...
- mysql找安装路经,更改密码
1:如果安装了mysql但是启动不了,可以到C:/Windows/System32的cmd.exe,用管理员身份打开,然后输入net strat mysql即可 2:忘记安装路经 sele ...
- list(列表) python
1.list(列表): list是处理一组有序项目的数据结构 list(列表)是python中使用最频繁的数据类型 list中什么类型的数据都可以存放(如:类.自己.函数......): list(列 ...
- Spring mvc前台后台传值
前台向后台传值: ①同名参数传递:form表单中提交input,Controller方法入参中,直接以同名参数获取 ②不同名参数传递:from表单提交input,Controller方法入参中需要使用 ...
- Git 和 Repo常用命令
这篇博客总结的也不错: git常用及进阶命令总结 Git与Repo入门 一.初始環境配置 git config --global user.name "John Doe"git c ...
- 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能
springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...
- Mac下不用重复输入ssh-key的密码
重装系统,复用以前的SSH key,发现每次调用这个Key都要输入Key的密码,很繁琐,以前不是这样的哦. 更新代码.SSH服务器总是提示: Enter passphrase for .../id_r ...