By
wz2cool
更新日期:
项目地址: mybatis-dynamic-query
前言
更新版本到 3.1.7,这次主要是一个小版本的更新,加了三个比较有用的方法,selectFirstByDynamicQuery, selectMaxByDynamicQuery, selectMinByDynamicQuery
更新内容
selectFirstByDynamicQuery
选择第一个, 这个和 selectOne 要区别开来,selectOne 是有且只有个一个,多了要报错,selectFirst 是你有多个也没关系,但是我只取第一个
1 2 3 4 5 6 7 8 9 10 11 12
| @Test public void testSelectFirst() { DynamicQuery<ProductsDO> query = DynamicQuery.createQuery(ProductsDO.class) .and(ProductsDO::getId, greaterThan(2)) .orderBy(ProductsDO::getId, asc()); Optional<ProductsDO> productsDOOptional = productMapper.selectFirstByDynamicQuery(query); Assert.assertTrue(productsDOOptional.isPresent()); Integer expectedValue = 3; Integer actualValue = productsDOOptional.get().getId(); Assert.assertEquals(expectedValue, actualValue); }
|
selectMaxByDynamicQuery
选择最大值,这个就比较好理解了,就是调用 max 函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Test public void testSelectMax() { DynamicQuery<ProductsDO> selectMaxQuery = DynamicQuery.createQuery(ProductsDO.class) .and(ProductsDO::getId, greaterThan(10)); Optional<BigDecimal> maxPriceOptional = productMapper.selectMaxByDynamicQuery(ProductsDO::getListPrice, selectMaxQuery); Assert.assertTrue(maxPriceOptional.isPresent()); BigDecimal actualMaxPrice = maxPriceOptional.get(); DynamicQuery<ProductsDO> selectFirstQuery = DynamicQuery.createQuery(ProductsDO.class) .select(ProductsDO::getListPrice) .and(ProductsDO::getId, greaterThan(10)) .orderBy(ProductsDO::getListPrice, desc()); Optional<ProductsDO> productsDOOptional = productMapper.selectFirstByDynamicQuery(selectFirstQuery); Assert.assertTrue(productsDOOptional.isPresent()); BigDecimal expectedMaxPrice = productsDOOptional.get().getListPrice(); Assert.assertEquals(expectedMaxPrice, actualMaxPrice); }
|
我们也可以看一下日志输出
1 2 3 4 5 6 7 8 9 10
| -- 12 2019-11-20 22:51:57.634 DEBUG 11812 --- [ main] c.g.w.m.m.P.selectMaxByDynamicQuery : ==> SELECT MAX(list_price) FROM products WHERE (id > 10); ------------------------------------------------------------------------------------------------------------------------ -- 13 2019-11-20 22:51:58.508 DEBUG 11812 --- [ main] .g.w.m.m.P.selectRowBoundsByDynamicQuery : ==> SELECT list_price AS list_price FROM products WHERE (id > 10) ORDER BY list_price DESC LIMIT 1;
|
selectMinByDynamicQuery
选择最小值,就是调用 min 函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @Test public void testSelectMin() { DynamicQuery<ProductsDO> selectMinQuery = DynamicQuery.createQuery(ProductsDO.class) .and(ProductsDO::getId, greaterThan(10)); Optional<BigDecimal> minPriceOptional = productMapper.selectMinByDynamicQuery(ProductsDO::getListPrice, selectMinQuery); Assert.assertTrue(minPriceOptional.isPresent()); BigDecimal actualMinPrice = minPriceOptional.get(); DynamicQuery<ProductsDO> selectFirstQuery = DynamicQuery.createQuery(ProductsDO.class) .select(ProductsDO::getListPrice) .and(ProductsDO::getId, greaterThan(10)) .orderBy(ProductsDO::getListPrice, asc()); Optional<ProductsDO> productsDOOptional = productMapper.selectFirstByDynamicQuery(selectFirstQuery); Assert.assertTrue(productsDOOptional.isPresent()); BigDecimal expectedMinPrice = productsDOOptional.get().getListPrice(); Assert.assertEquals(expectedMinPrice, actualMinPrice); }
|
我们也可以看一下日志输出
1 2 3 4 5 6 7 8 9 10
| -- 26 2019-11-20 23:25:32.043 DEBUG 14740 --- [ main] c.g.w.m.m.P.selectMinByDynamicQuery : ==> SELECT MIN(list_price) FROM products WHERE (id > 10); ------------------------------------------------------------------------------------------------------------------------ -- 27 2019-11-20 23:25:32.749 DEBUG 14740 --- [ main] .g.w.m.m.P.selectRowBoundsByDynamicQuery : ==> SELECT list_price AS list_price FROM products WHERE (id > 10) ORDER BY list_price ASC LIMIT 1;
|
小结
忙了 3 个月了,终于有空更新一下… 后面已经在构思动态查询查 elasticsearch。