فهرست منبع

Merge pull request #749 from WEIZIBIN/feature-CountDistinct

提供baseMapper.selectCount()方法对distinct的支持
miemieYaho 6 سال پیش
والد
کامیت
a3cbebb499

+ 1 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/enums/SqlMethod.java

@@ -65,7 +65,7 @@ public enum SqlMethod {
     SELECT_BY_MAP("selectByMap", "根据columnMap 查询一条数据", "<script>\nSELECT %s FROM %s %s\n</script>"),
     SELECT_BATCH_BY_IDS("selectBatchIds", "根据ID集合,批量查询数据", "<script>\nSELECT %s FROM %s WHERE %s IN (%s)\n</script>"),
     SELECT_ONE("selectOne", "查询满足条件一条数据", "<script>\nSELECT %s FROM %s %s\n</script>"),
-    SELECT_COUNT("selectCount", "查询满足条件总记录数", "<script>\nSELECT COUNT(1) FROM %s %s\n</script>"),
+    SELECT_COUNT("selectCount", "查询满足条件总记录数", "<script>\nSELECT COUNT(%s) FROM %s %s\n</script>"),
     SELECT_LIST("selectList", "查询满足条件所有数据", "<script>\nSELECT %s FROM %s %s\n</script>"),
     SELECT_PAGE("selectPage", "查询满足条件所有数据(并翻页)", "<script>\nSELECT %s FROM %s %s\n</script>"),
     SELECT_MAPS("selectMaps", "查询满足条件所有数据", "<script>\nSELECT %s FROM %s %s\n</script>"),

+ 12 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java

@@ -116,6 +116,18 @@ public abstract class AbstractMethod implements Constants {
             SqlScriptUtils.unSafeParam(Q_WRAPPER_SQL_SELECT), selectColumns);
     }
 
+    /**
+     * <p>
+     * SQL 查询记录行数
+     * </p>
+     *
+     * @return count sql 脚本
+     */
+    protected String sqlCount() {
+        return SqlScriptUtils.convertChoose(String.format("%s != null and %s != null", WRAPPER, Q_WRAPPER_SQL_SELECT),
+            SqlScriptUtils.unSafeParam(Q_WRAPPER_SQL_SELECT), ONE);
+    }
+
     /**
      * <p>
      * SQL 设置selectObj sql select

+ 1 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectCount.java

@@ -34,7 +34,7 @@ public class SelectCount extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_COUNT;
-        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlWhereEntityWrapper(true, tableInfo));
+        String sql = String.format(sqlMethod.getSql(), this.sqlCount(), tableInfo.getTableName(), this.sqlWhereEntityWrapper(true, tableInfo));
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, Integer.class, null);
     }

+ 1 - 1
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicSelectCount.java

@@ -34,7 +34,7 @@ public class LogicSelectCount extends AbstractLogicMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_COUNT;
-        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo));
+        String sql = String.format(sqlMethod.getSql(), this.sqlCount(), tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo));
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, Integer.class, null);
     }

+ 141 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/SelectCountDistinctTest.java

@@ -0,0 +1,141 @@
+package com.baomidou.mybatisplus.test.mysql;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.test.base.entity.CommonData;
+import com.baomidou.mybatisplus.test.base.entity.CommonLogicData;
+import com.baomidou.mybatisplus.test.base.mapper.commons.CommonDataMapper;
+import com.baomidou.mybatisplus.test.base.mapper.commons.CommonLogicDataMapper;
+import com.baomidou.mybatisplus.test.mysql.config.MysqlDb;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.MethodSorters;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import javax.annotation.Resource;
+import java.sql.SQLException;
+import java.util.List;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+@ContextConfiguration(locations = {"classpath:mysql/spring-test-mysql.xml"})
+public class SelectCountDistinctTest {
+
+    @Resource
+    private CommonLogicDataMapper commonLogicMapper;
+    @Resource
+    private CommonDataMapper commonDataMapper;
+
+    @Before
+    public void init() throws SQLException {
+        MysqlDb.initMysqlData();
+        insertLogic();
+        insertCommon();
+        System.out.println("init table and data success");
+    }
+
+    private void insertLogic() {
+        commonLogicMapper.insert(new CommonLogicData().setTestInt(25).setTestStr("test"));
+        commonLogicMapper.insert(new CommonLogicData().setTestInt(25).setTestStr("test"));
+    }
+
+    private void insertCommon() {
+        commonDataMapper.insert(new CommonData().setTestInt(25).setTestStr("test"));
+        commonDataMapper.insert(new CommonData().setTestInt(25).setTestStr("test"));
+    }
+
+    @Test
+    public void testCountDistinct() {
+        QueryWrapper<CommonData> distinct = new QueryWrapper<>();
+        distinct.select("distinct test_int");
+        distinct.eq("test_int", 25).or().eq("test_str", "test");
+        int count = commonDataMapper.selectCount(distinct);
+        Assert.assertEquals(1, count);
+    }
+
+    @Test
+    public void testCountDistinctTwoColumn() {
+        QueryWrapper<CommonData> distinct = new QueryWrapper<>();
+        distinct.select("distinct test_int, test_str");
+        distinct.eq("test_int", 25).or().eq("test_str", "test");
+        int count = commonDataMapper.selectCount(distinct);
+        Assert.assertEquals(1, count);
+    }
+
+    @Test
+    public void testLogicCountDistinct() {
+        QueryWrapper<CommonLogicData> distinct = new QueryWrapper<>();
+        distinct.select("distinct test_int");
+        distinct.eq("test_int", 25).or().eq("test_str", "test");
+        int count = commonLogicMapper.selectCount(distinct);
+        Assert.assertEquals(1, count);
+    }
+
+    @Test
+    public void testLogicSelectList() {
+        QueryWrapper<CommonLogicData> commonQuery = new QueryWrapper<>();
+        List<CommonLogicData> commonLogicDataList = commonLogicMapper.selectList(commonQuery);
+        CommonLogicData commonLogicData = commonLogicDataList.get(0);
+        Assert.assertEquals(25, commonLogicData.getTestInt().intValue());
+        Assert.assertEquals("test", commonLogicData.getTestStr());
+    }
+
+    @Test
+    public void testLogicCountDistinctUseLambda() {
+        LambdaQueryWrapper<CommonLogicData> lambdaQueryWrapper =
+            new QueryWrapper<CommonLogicData>().select("distinct test_int").lambda();
+        int count = commonLogicMapper.selectCount(lambdaQueryWrapper);
+        Assert.assertEquals(1, count);
+    }
+
+    @Test
+    public void testCountDistinctUseLambda() {
+        LambdaQueryWrapper<CommonData> lambdaQueryWrapper =
+            new QueryWrapper<CommonData>().select("distinct test_int, test_str").lambda();
+        int count = commonDataMapper.selectCount(lambdaQueryWrapper);
+        Assert.assertEquals(1, count);
+    }
+
+    @Test
+    public void testLogicSelectCountWithoutDistinct() {
+        QueryWrapper<CommonLogicData> queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq("test_int", 25).or().eq("test_str", "test");
+        int count = commonLogicMapper.selectCount(queryWrapper);
+        Assert.assertEquals(2, count);
+    }
+
+    @Test
+    public void testCountDistinctWithoutDistinct() {
+        QueryWrapper<CommonData> queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq("test_int", 25).or().eq("test_str", "test");
+        int count = commonDataMapper.selectCount(queryWrapper);
+        Assert.assertEquals(2, count);
+    }
+
+    @Test
+    public void testSelectPageWithoutDistinct() {
+        QueryWrapper<CommonData> queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq("test_int", 25).or().eq("test_str", "test");
+        IPage<CommonData> page = commonDataMapper.selectPage(new Page<CommonData>(1, 10), queryWrapper);
+        Assert.assertEquals(2, page.getTotal());
+        Assert.assertNotNull(page.getRecords().get(0));
+        Assert.assertNotNull(page.getRecords().get(1));
+    }
+
+    @Test
+    public void testSelectPageWithDistinct() {
+        QueryWrapper<CommonData> queryWrapper = new QueryWrapper<>();
+        queryWrapper.select("distinct test_int, test_str");
+        queryWrapper.eq("test_int", 25).or().eq("test_str", "test");
+        IPage<CommonData> page = commonDataMapper.selectPage(new Page<CommonData>(1, 10), queryWrapper);
+        Assert.assertEquals(1, page.getTotal());
+        Assert.assertNotNull(page.getRecords().get(0));
+    }
+
+}