瀏覽代碼

pg 测试

miemie 6 年之前
父節點
當前提交
4d94e64dd8

+ 1 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/base/entity/CommonData.java

@@ -22,6 +22,7 @@ import java.time.LocalDateTime;
 public class CommonData {
 
     private Long id;
+    @TableField(el = "testInt, jdbcType=INTEGER")
     private Integer testInt;
     private String testStr;
     @TableField(value = "c_time", fill = FieldFill.INSERT)

+ 4 - 9
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/base/entity/pg/PgData.java

@@ -1,12 +1,9 @@
 package com.baomidou.mybatisplus.test.base.entity.pg;
 
-import com.baomidou.mybatisplus.annotation.FieldFill;
 import com.baomidou.mybatisplus.annotation.TableField;
 import lombok.Data;
 import lombok.experimental.Accessors;
 
-import java.time.LocalDateTime;
-
 /**
  * @author miemie
  * @since 2018-08-06
@@ -16,10 +13,8 @@ import java.time.LocalDateTime;
 public class PgData {
 
     private Long id;
-    @TableField(value = "age", el = "dataAge, jdbcType=INTEGER")
-    private Integer dataAge;
-    @TableField(value = "c_datetime", fill = FieldFill.INSERT)
-    private LocalDateTime createDatetime;
-    @TableField(value = "u_datetime", fill = FieldFill.UPDATE)
-    private LocalDateTime updateDatetime;
+    @TableField("pgInt")
+    private Integer pgInt;
+    private Integer pgInt2;
+    private Integer group;
 }

+ 6 - 9
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlTestDataMapperTest.java

@@ -38,8 +38,8 @@ import java.util.Map;
  * @since 2018-06-05
  */
 @RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration(locations = {"classpath:mysql/spring-test-mysql.xml"})
 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
+@ContextConfiguration(locations = {"classpath:mysql/spring-test-mysql.xml"})
 public class MysqlTestDataMapperTest {
 
     @Resource
@@ -190,8 +190,7 @@ public class MysqlTestDataMapperTest {
 
     @Test
     public void o_selectPage() {
-        IPage<CommonData> page = new Page<>();
-        page.setSize(5).setCurrent(1);
+        IPage<CommonData> page = new Page<>(1, 5);
         IPage<CommonData> dataPage = commonMapper.selectPage(page, null);
         Assert.assertSame(dataPage, page);
         Assert.assertNotEquals(0L, dataPage.getTotal());
@@ -199,18 +198,16 @@ public class MysqlTestDataMapperTest {
         Assert.assertTrue(CollectionUtils.isNotEmpty(dataPage.getRecords()));
         System.out.println(JSON.toJSONString(dataPage));
 
-        IPage<CommonData> logicPage = new Page<>();
-        page.setSize(5).setCurrent(1);
-        IPage<CommonData> logicDataPage = commonMapper.selectPage(logicPage, null);
+        IPage<CommonLogicData> logicPage = new Page<>(1, 5);
+        IPage<CommonLogicData> logicDataPage = commonLogicMapper.selectPage(logicPage, null);
         Assert.assertSame(logicDataPage, logicPage);
         Assert.assertNotEquals(0L, logicDataPage.getTotal());
         Assert.assertNotEquals(0, logicDataPage.getRecords().size());
         Assert.assertTrue(CollectionUtils.isNotEmpty(logicDataPage.getRecords()));
         System.out.println(JSON.toJSONString(logicDataPage));
 
-        IPage<CommonData> mysqlPage = new Page<>();
-        page.setSize(5).setCurrent(1);
-        IPage<CommonData> mysqlDataPage = commonMapper.selectPage(mysqlPage, null);
+        IPage<MysqlData> mysqlPage = new Page<>(1, 5);
+        IPage<MysqlData> mysqlDataPage = mysqlMapper.selectPage(mysqlPage, null);
         Assert.assertSame(mysqlDataPage, mysqlPage);
         Assert.assertNotEquals(0L, mysqlDataPage.getTotal());
         Assert.assertNotEquals(0, mysqlDataPage.getRecords().size());

+ 210 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgres/PostgresTestDataMapperTest.java

@@ -1,9 +1,33 @@
 package com.baomidou.mybatisplus.test.postgres;
 
+import com.alibaba.fastjson.JSON;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+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.entity.pg.PgData;
+import com.baomidou.mybatisplus.test.base.mapper.commons.CommonDataMapper;
+import com.baomidou.mybatisplus.test.base.mapper.commons.CommonLogicDataMapper;
+import com.baomidou.mybatisplus.test.base.mapper.pg.PgDataMapper;
+import com.baomidou.mybatisplus.test.postgres.config.PostgresDb;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+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.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 /**
  * <p>
  * Mybatis Plus mysql Junit Test
@@ -13,7 +37,193 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  * @since 2018-06-05
  */
 @RunWith(SpringJUnit4ClassRunner.class)
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
 @ContextConfiguration(locations = {"classpath:postgres/spring-test-postgres.xml"})
 public class PostgresTestDataMapperTest {
 
+    @Resource
+    private CommonDataMapper commonMapper;
+    @Resource
+    private CommonLogicDataMapper commonLogicMapper;
+    @Resource
+    private PgDataMapper pgMapper;
+
+    @BeforeClass
+    public static void init() throws Exception {
+        PostgresDb.initPgData();
+    }
+
+    @Test
+    public void a_insertForeach() {
+        for (int i = 1; i < 20; i++) {
+            Long id = (long) i;
+            commonMapper.insert(new CommonData().setTestInt(i).setTestStr(String.format("第%s条数据", i)).setId(id));
+            commonLogicMapper.insert(new CommonLogicData().setTestInt(i).setTestStr(String.format("第%s条数据", i)).setId(id));
+            pgMapper.insert(new PgData().setGroup(i).setId(id));
+        }
+    }
+
+    @Test
+    public void b_deleteById() {
+        Assert.assertEquals(1, commonMapper.deleteById(1L));
+        Assert.assertEquals(1, commonLogicMapper.deleteById(1L));
+        Assert.assertEquals(1, pgMapper.deleteById(1L));
+    }
+
+    @Test
+    public void c_deleteByMap() {
+        Map<String, Object> map = new HashMap<>();
+        map.put("id", 2L);
+        map.put("test_int", 5);
+        Assert.assertEquals(0, commonMapper.deleteByMap(map));
+        Assert.assertEquals(0, commonLogicMapper.deleteByMap(map));
+        Map<String, Object> map2 = new HashMap<>();
+        map2.put("id", 2L);
+        map2.put("group", 5);
+        map2.put("pgInt", 5);
+        Assert.assertEquals(0, pgMapper.deleteByMap(map2));
+    }
+
+    @Test
+    public void d_delete() {
+        Assert.assertEquals(1, commonMapper.delete(new QueryWrapper<CommonData>().lambda()
+            .eq(CommonData::getId, 2L)
+            .eq(CommonData::getTestInt, 2)));
+        Assert.assertEquals(1, commonLogicMapper.delete(new QueryWrapper<CommonLogicData>().lambda()
+            .eq(CommonLogicData::getId, 2L)
+            .eq(CommonLogicData::getTestInt, 2)));
+        Assert.assertEquals(1, pgMapper.delete(new QueryWrapper<PgData>().lambda()
+            .eq(PgData::getId, 2L)
+            .eq(PgData::getGroup, 2).eq(PgData::getPgInt, 2)));
+    }
+
+    @Test
+    public void e_deleteBatchIds() {
+        List<Long> ids = Arrays.asList(3L, 4L);
+        Assert.assertEquals(2, commonMapper.deleteBatchIds(ids));
+        Assert.assertEquals(2, commonLogicMapper.deleteBatchIds(ids));
+        Assert.assertEquals(2, pgMapper.deleteBatchIds(ids));
+    }
+
+    @Test
+    public void f_updateById() {
+        Assert.assertEquals(1, commonMapper.updateById(new CommonData().setId(5L).setTestInt(555)));
+        Assert.assertEquals(1, commonLogicMapper.updateById(new CommonLogicData().setId(5L).setTestInt(555)));
+        Assert.assertEquals(1, pgMapper.updateById(new PgData().setId(5L).setGroup(555).setPgInt(555)));
+    }
+
+    @Test
+    public void g_optimisticUpdateById() {
+        Assert.assertEquals(1, commonMapper.updateById(new CommonData().setId(5L).setTestInt(556)
+            .setVersion(0)));
+    }
+
+    @Test
+    public void h_update() {
+        Assert.assertEquals(1, commonMapper.update(
+            new CommonData().setTestInt(666),
+            new UpdateWrapper<CommonData>().lambda().eq(CommonData::getId, 6L)
+                .eq(CommonData::getTestInt, 6)));
+        Assert.assertEquals(1, commonLogicMapper.update(
+            new CommonLogicData().setTestInt(666),
+            new UpdateWrapper<CommonLogicData>().lambda().eq(CommonLogicData::getId, 6L)
+                .eq(CommonLogicData::getTestInt, 6)));
+        Assert.assertEquals(1, pgMapper.update(
+            new PgData().setGroup(666).setPgInt(555),
+            new UpdateWrapper<PgData>().lambda().eq(PgData::getId, 6L)
+                .eq(PgData::getGroup, 6).eq(PgData::getPgInt, 6)));
+    }
+
+    @Test
+    public void i_getAllNoTenant() {
+        commonMapper.getAllNoTenant();
+    }
+
+    @Test
+    public void j_selectById() {
+        long id = 6L;
+        Assert.assertNotNull(commonMapper.selectById(id));
+        Assert.assertNotNull(commonLogicMapper.selectById(id));
+        Assert.assertNotNull(pgMapper.selectById(id));
+    }
+
+    @Test
+    public void k_selectBatchIds() {
+        List<Long> ids = Arrays.asList(7L, 8L);
+        Assert.assertTrue(CollectionUtils.isNotEmpty(commonMapper.selectBatchIds(ids)));
+        Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectBatchIds(ids)));
+        Assert.assertTrue(CollectionUtils.isNotEmpty(pgMapper.selectBatchIds(ids)));
+    }
+
+    @Test
+    public void l_selectByMap() {
+        Map<String, Object> map = new HashMap<>();
+        map.put("id", 9L);
+        map.put("test_int", 9);
+        Assert.assertTrue(CollectionUtils.isNotEmpty(commonMapper.selectByMap(map)));
+        Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectByMap(map)));
+        Map<String, Object> map2 = new HashMap<>();
+        map2.put("id", 9L);
+        map2.put("group", 9);
+        map2.put("pgInt", 9);
+        Assert.assertTrue(CollectionUtils.isNotEmpty(pgMapper.selectByMap(map2)));
+    }
+
+    @Test
+    public void m_selectOne() {
+        Assert.assertNotNull(commonMapper.selectOne(new QueryWrapper<CommonData>().lambda()
+            .eq(CommonData::getId, 10L).eq(CommonData::getTestInt, 10)));
+        Assert.assertNotNull(commonLogicMapper.selectOne(new QueryWrapper<CommonLogicData>().lambda()
+            .eq(CommonLogicData::getId, 10L).eq(CommonLogicData::getTestInt, 10)));
+        Assert.assertNotNull(pgMapper.selectOne(new QueryWrapper<PgData>().lambda()
+            .eq(PgData::getId, 10L).eq(PgData::getGroup, 10).eq(PgData::getPgInt, 10)));
+    }
+
+    @Test
+    public void n_selectList() {
+        Assert.assertTrue(CollectionUtils.isNotEmpty(commonMapper.selectList(new QueryWrapper<CommonData>()
+            .lambda().eq(CommonData::getTestInt, 10))));
+        Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectList(new QueryWrapper<CommonLogicData>()
+            .lambda().eq(CommonLogicData::getId, 10L).eq(CommonLogicData::getTestInt, 10))));
+        Assert.assertTrue(CollectionUtils.isNotEmpty(pgMapper.selectList(new QueryWrapper<PgData>()
+            .lambda().eq(PgData::getId, 10L).eq(PgData::getGroup, 10).eq(PgData::getPgInt, 10))));
+    }
+
+    @Test
+    public void o_selectPage() {
+        IPage<CommonData> page = new Page<>(1, 5);
+        IPage<CommonData> dataPage = commonMapper.selectPage(page, null);
+        Assert.assertSame(dataPage, page);
+        Assert.assertNotEquals(0L, dataPage.getTotal());
+        Assert.assertNotEquals(0, dataPage.getRecords().size());
+        Assert.assertTrue(CollectionUtils.isNotEmpty(dataPage.getRecords()));
+        System.out.println(JSON.toJSONString(dataPage));
+
+        IPage<CommonLogicData> logicPage = new Page<>(1, 5);
+        IPage<CommonLogicData> logicDataPage = commonLogicMapper.selectPage(logicPage, null);
+        Assert.assertSame(logicDataPage, logicPage);
+        Assert.assertNotEquals(0L, logicDataPage.getTotal());
+        Assert.assertNotEquals(0, logicDataPage.getRecords().size());
+        Assert.assertTrue(CollectionUtils.isNotEmpty(logicDataPage.getRecords()));
+        System.out.println(JSON.toJSONString(logicDataPage));
+
+        IPage<PgData> pgPage = new Page<>(1, 5);
+        page.setSize(5).setCurrent(1);
+        IPage<PgData> pgDataPage = pgMapper.selectPage(pgPage, null);
+        Assert.assertSame(pgDataPage, pgPage);
+        Assert.assertNotEquals(0L, pgDataPage.getTotal());
+        Assert.assertNotEquals(0, pgDataPage.getRecords().size());
+        Assert.assertTrue(CollectionUtils.isNotEmpty(pgDataPage.getRecords()));
+        System.out.println(JSON.toJSONString(pgDataPage));
+    }
+
+    @Test
+    public void p_testApply() {
+        Assert.assertTrue(CollectionUtils.isNotEmpty(commonMapper.selectList(new QueryWrapper<CommonData>()
+            .apply("test_int = 12"))));
+        Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectList(new QueryWrapper<CommonLogicData>()
+            .apply("test_int = 12"))));
+        Assert.assertTrue(CollectionUtils.isNotEmpty(pgMapper.selectList(new QueryWrapper<PgData>()
+            .apply("group = 12").apply("pgInt = 12"))));
+    }
 }

+ 1 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgres/config/PostgresDb.java

@@ -13,7 +13,7 @@ import java.sql.Statement;
 
 public class PostgresDb extends BaseDb {
 
-    public static void initMysqlData() throws SQLException, IOException {
+    public static void initPgData() throws SQLException, IOException {
         ApplicationContext context = new ClassPathXmlApplicationContext("classpath:postgres/spring-test-postgres.xml");
         DataSource ds = context.getBean("dataSource", DataSource.class);
         try (Connection conn = ds.getConnection()) {

+ 1 - 2
mybatis-plus/src/test/resources/postgres/spring-test-postgres.xml

@@ -5,6 +5,5 @@
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 
-    <context:component-scan
-        base-package="com.baomidou.mybatisplus.test.postgres.config"/>
+    <context:component-scan base-package="com.baomidou.mybatisplus.test.postgres.config"/>
 </beans>

+ 24 - 32
mybatis-plus/src/test/resources/postgres/test_data.ddl.sql

@@ -1,37 +1,29 @@
-CREATE TABLE IF NOT EXISTS tb_test_data_logic (
-    id              BIGINT primary key,
-    test_int        integer,
-    test_str        varchar(50),
-    test_double     double precision,
-    test_boolean    smallint,
-    test_date       date,
-    test_time       time,
-    test_date_time  timestamp,
-    test_timestamp  timestamp,
-    create_datetime timestamp,
-    update_datetime timestamp,
-    deleted         smallint default 0,
-    version         integer  default 0
+drop table if exists common_data;
+drop table if exists common_logic_data;
+drop table if exists pg_data;
+CREATE TABLE common_data (
+    id        BIGINT primary key,
+    test_int  integer,
+    test_str  varchar(50),
+    c_time    timestamp,
+    u_time    timestamp,
+    version   integer default 0,
+    tenant_id bigint
 );
 
-CREATE TABLE IF NOT EXISTS tb_test_data (
-    id              BIGINT primary key,
-    test_int        integer,
-    test_str        varchar(50),
-    test_double     double precision,
-    test_boolean    smallint,
-    test_date       date,
-    test_time       time,
-    test_date_time  timestamp,
-    test_timestamp  timestamp,
-    create_datetime timestamp,
-    update_datetime timestamp,
-    version         integer default 0
+CREATE TABLE common_logic_data (
+    id       BIGINT primary key,
+    test_int integer,
+    test_str varchar(50),
+    c_time   timestamp,
+    u_time   timestamp,
+    deleted  smallint default 0,
+    version  integer  default 0
 );
 
-CREATE TABLE IF NOT EXISTS tb_only_pg_test_data (
-    id         BIGINT primary key,
-    age        integer,
-    c_datetime timestamp,
-    u_datetime timestamp
+CREATE TABLE pg_data (
+    id      BIGINT primary key,
+    "pgInt" integer,
+    pg_int2 integer,
+    "group" integer
 );