Browse Source

测试用例+优化抛出异常的过程

miemie 7 years ago
parent
commit
d8e2ad9160

+ 12 - 17
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/GlobalConfigUtils.java

@@ -16,24 +16,22 @@
  */
 package com.baomidou.mybatisplus.core.toolkit;
 
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-
-import org.apache.ibatis.logging.Log;
-import org.apache.ibatis.logging.LogFactory;
-import org.apache.ibatis.session.Configuration;
-import org.apache.ibatis.session.SqlSession;
-import org.apache.ibatis.session.SqlSessionFactory;
-
 import com.baomidou.mybatisplus.annotation.DbType;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
-import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
 import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
 import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
 import com.baomidou.mybatisplus.core.injector.ISqlInjector;
+import org.apache.ibatis.logging.Log;
+import org.apache.ibatis.logging.LogFactory;
+import org.apache.ibatis.session.Configuration;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * <p>
@@ -90,9 +88,8 @@ public class GlobalConfigUtils {
      * @param mybatisGlobalConfig 全局配置
      */
     public static void setGlobalConfig(Configuration configuration, GlobalConfig mybatisGlobalConfig) {
-        if (configuration == null || mybatisGlobalConfig == null) {
-            throw new MybatisPlusException("Error: Could not setGlobalConfig");
-        }
+        Assert.isTrue(configuration != null && mybatisGlobalConfig != null,
+            "Error: Could not setGlobalConfig !");
         // 设置全局设置
         GLOBAL_CONFIG.put(configuration.toString(), mybatisGlobalConfig);
     }
@@ -105,9 +102,7 @@ public class GlobalConfigUtils {
      * @param configuration Mybatis 容器配置对象
      */
     public static GlobalConfig getGlobalConfig(Configuration configuration) {
-        if (configuration == null) {
-            throw new MybatisPlusException("Error: You need Initialize MybatisConfiguration !");
-        }
+        Assert.notNull(configuration, "Error: You need Initialize MybatisConfiguration !");
         return getGlobalConfig(configuration.toString());
     }
 

+ 1 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/service/ILogicTestDataService.java → mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/base/service/ILogicTestDataService.java

@@ -1,4 +1,4 @@
-package com.baomidou.mybatisplus.test.mysql.service;
+package com.baomidou.mybatisplus.test.base.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.baomidou.mybatisplus.test.base.entity.LogicTestData;

+ 2 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/service/impl/LogicTestDataServiceImpl.java → mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/base/service/impl/LogicTestDataServiceImpl.java

@@ -1,9 +1,9 @@
-package com.baomidou.mybatisplus.test.mysql.service.impl;
+package com.baomidou.mybatisplus.test.base.service.impl;
 
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.baomidou.mybatisplus.test.base.entity.LogicTestData;
 import com.baomidou.mybatisplus.test.base.mapper.LogicTestDataMapper;
-import com.baomidou.mybatisplus.test.mysql.service.ILogicTestDataService;
+import com.baomidou.mybatisplus.test.base.service.ILogicTestDataService;
 import org.springframework.stereotype.Service;
 
 

+ 19 - 21
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlTestDataMapperTest.java

@@ -1,25 +1,5 @@
 package com.baomidou.mybatisplus.test.mysql;
 
-import java.io.IOException;
-import java.math.BigDecimal;
-import java.sql.SQLException;
-import java.time.LocalDate;
-import java.time.LocalDateTime;
-import java.time.LocalTime;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.annotation.Resource;
-
-import org.junit.Assert;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.springframework.test.context.ContextConfiguration;
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
 import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
@@ -29,8 +9,26 @@ import com.baomidou.mybatisplus.test.base.entity.LogicTestData;
 import com.baomidou.mybatisplus.test.base.entity.TestData;
 import com.baomidou.mybatisplus.test.base.mapper.LogicTestDataMapper;
 import com.baomidou.mybatisplus.test.base.mapper.TestDataMapper;
+import com.baomidou.mybatisplus.test.base.service.ILogicTestDataService;
 import com.baomidou.mybatisplus.test.mysql.config.MysqlDb;
-import com.baomidou.mybatisplus.test.mysql.service.ILogicTestDataService;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import javax.annotation.Resource;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.sql.SQLException;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /**
  * <p>

+ 61 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgres/PostgresMetaObjectHandler.java

@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.test.postgres;
+
+import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
+import org.apache.ibatis.reflection.MetaObject;
+
+import java.time.LocalDateTime;
+
+/**
+ * <p>
+ * 测试,自定义元对象字段填充控制器,实现公共字段自动写入
+ * </p>
+ *
+ * @author hubin
+ * @since 2017-06-25
+ */
+public class PostgresMetaObjectHandler implements MetaObjectHandler {
+
+    /**
+     * 测试 user 表 name 字段为空自动填充
+     */
+    @Override
+    public void insertFill(MetaObject metaObject) {
+        System.out.println("*************************");
+        System.out.println("insert fill");
+        System.out.println("*************************");
+        // 测试下划线
+        Object createDatetime = this.getFieldValByName("createDatetime", metaObject);
+        System.out.println("createDatetime=" + createDatetime);
+        if (createDatetime == null) {
+            //测试实体没有的字段,配置在公共填充,不应该set到实体里面
+            this.setFieldValByName("createDatetime1", LocalDateTime.now(), metaObject)
+                .setFieldValByName("createDatetime", LocalDateTime.now(), metaObject);
+        }
+    }
+
+    @Override
+    public void updateFill(MetaObject metaObject) {
+        System.out.println("*************************");
+        System.out.println("update fill");
+        System.out.println("*************************");
+        //测试实体没有的字段,配置在公共填充,不应该set到实体里面
+        this.setFieldValByName("updateDatetime1", LocalDateTime.now(), metaObject)
+            .setFieldValByName("updateDatetime", LocalDateTime.now(), metaObject);
+    }
+}
+

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

@@ -0,0 +1,297 @@
+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.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.test.base.entity.LogicTestData;
+import com.baomidou.mybatisplus.test.base.entity.TestData;
+import com.baomidou.mybatisplus.test.base.mapper.LogicTestDataMapper;
+import com.baomidou.mybatisplus.test.base.mapper.TestDataMapper;
+import com.baomidou.mybatisplus.test.base.service.ILogicTestDataService;
+import com.baomidou.mybatisplus.test.mysql.config.MysqlDb;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import javax.annotation.Resource;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.sql.SQLException;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * <p>
+ * Mybatis Plus mysql Junit Test
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-06-05
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(locations = {"classpath:postgres/spring-test-postgres.xml"})
+public class PostgresTestDataMapperTest {
+
+    @Resource
+    private TestDataMapper mapper;
+    @Resource
+    private LogicTestDataMapper logicMapper;
+
+    @Resource
+    private ILogicTestDataService logicTestDataService;
+
+    @BeforeClass
+    public static void init() throws IOException, SQLException {
+        MysqlDb.initMysqlData();
+    }
+
+    @Test
+    public void insertForeach() {
+        LocalDateTime nowDateTime = LocalDateTime.now();
+        LocalDate nowDate = nowDateTime.toLocalDate();
+        LocalTime nowTime = nowDateTime.toLocalTime();
+        for (int i = 0; i < 20; i++) {
+            mapper.insert(new TestData().setTestInt(i).setTestStr(String.format("第%s条数据", i))
+                .setTestDouble(BigDecimal.valueOf(3.3).multiply(BigDecimal.valueOf(i)).doubleValue())
+                .setTestBoolean((i + 3) % 2 == 0).setTestDate(nowDate)
+                .setTestTime(nowTime).setTestDateTime(nowDateTime));
+            logicMapper.insert(new LogicTestData().setTestInt(i).setTestStr(String.format("第%s条数据", i))
+                .setTestDouble(BigDecimal.valueOf(3.3).multiply(BigDecimal.valueOf(i)).doubleValue())
+                .setTestBoolean((i + 3) % 2 == 0).setTestDate(nowDate)
+                .setTestTime(nowTime).setTestDateTime(nowDateTime));
+        }
+    }
+
+    @Test
+    public void deleteById() {
+        mapper.deleteById(1014132604940615682L);
+        logicMapper.deleteById(1014132604940615682L);
+    }
+
+    @Test
+    public void deleteByMap() {
+        Map<String, Object> map = new HashMap<>();
+        map.put("id", 1014361515785568258L);
+        map.put("test_int", 5);
+        mapper.deleteByMap(map);
+        logicMapper.deleteByMap(map);
+    }
+
+    @Test
+    public void delete() {
+        mapper.delete(new QueryWrapper<TestData>().lambda()
+            .eq(TestData::getId, 1014132604940615682L)
+            .eq(TestData::getTestInt, 1));
+        logicMapper.delete(new QueryWrapper<LogicTestData>().lambda()
+            .eq(LogicTestData::getId, 1014132604940615682L)
+            .eq(LogicTestData::getTestInt, 1));
+    }
+
+    @Test
+    public void deleteBatchIds() {
+        List<Long> ids = Arrays.asList(1014132604940615682L, 1014132604940615652L);
+        mapper.deleteBatchIds(ids);
+        logicMapper.deleteBatchIds(ids);
+    }
+
+    @Test
+    public void updateTimeIssue() {
+//        mapper.updateById(new TestData().setId(1014132604940615682L).setTestInt(1111111111));
+        LogicTestData et = new LogicTestData()
+            .setId(1019248035919613954L)
+            .setTestInt(9991122)
+            .setVersion(19);
+        System.out.println("====1=====>>>" + JSON.toJSONString(et, true));
+        boolean r = logicTestDataService.saveOrUpdate(et);
+        System.out.println("====2-1==r==>>>" + r);
+        System.out.println("====2-2=====>>>" + JSON.toJSONString(et, true));
+    }
+
+    @Test
+    public void optimisticUpdateById() {
+//        mapper.updateById(new TestData().setId(1014132604940615682L).setTestInt(1111111111));
+        LogicTestData et = new LogicTestData()
+            .setId(1019248035919613954L)
+            .setTestInt(999)
+            .setVersion(17);
+        System.out.println("====1=====>>>" + JSON.toJSONString(et, true));
+        int r = logicMapper.updateById(et);
+        System.out.println("====2-1==r==>>>" + r);
+        System.out.println("====2-2=====>>>" + JSON.toJSONString(et, true));
+    }
+
+    @Test
+    public void updateById() {
+        mapper.updateById(new TestData().setId(1014132604940615682L).setTestInt(1111111111));
+        logicMapper.updateById(new LogicTestData().setId(1014132604940615682L).setTestInt(1111111111));
+    }
+
+    @Test
+    public void update() {
+        // type 1
+        mapper.update(new TestData(), null);
+        logicMapper.update(new LogicTestData(), null);
+        // type 2
+        mapper.update(new TestData(), new UpdateWrapper<TestData>()
+            .set("test_int", 5));
+        logicMapper.update(new LogicTestData(), new UpdateWrapper<LogicTestData>()
+            .set("test_int", 5));
+//        // type 3
+        mapper.update(new TestData(), new UpdateWrapper<TestData>()
+            .set("test_int", 5).eq("id", 1014361515554881538L));
+        logicMapper.update(new LogicTestData(), new UpdateWrapper<LogicTestData>()
+            .set("test_int", 5).eq("id", 1014361515554881538L));
+//        // type 4
+        mapper.update(new TestData(), new UpdateWrapper<TestData>()
+            .eq("id", 1014361515554881538L));
+        logicMapper.update(new LogicTestData(), new UpdateWrapper<LogicTestData>()
+            .eq("id", 1014361515554881538L));
+//        // type 5
+        mapper.update(new TestData(), new UpdateWrapper<TestData>()
+            .setEntity(new TestData().setTestInt(1)));
+        logicMapper.update(new LogicTestData(), new UpdateWrapper<LogicTestData>()
+            .setEntity(new LogicTestData().setTestInt(1)));
+//        // type 6
+        mapper.update(new TestData(), new UpdateWrapper<TestData>()
+            .setEntity(new TestData().setTestInt(1))
+            .eq("id", 1014361515554881538L));
+        logicMapper.update(new LogicTestData(), new UpdateWrapper<LogicTestData>()
+            .setEntity(new LogicTestData().setTestInt(1))
+            .eq("id", 1014361515554881538L));
+//        // type 7
+        mapper.update(new TestData(), new UpdateWrapper<TestData>()
+            .setEntity(new TestData().setTestInt(1))
+            .set("test_int", 55555)
+            .eq("id", 1014361515554881538L));
+        logicMapper.update(new LogicTestData(), new UpdateWrapper<LogicTestData>()
+            .setEntity(new LogicTestData().setTestInt(1))
+            .set("test_int", 55555)
+            .eq("id", 1014361515554881538L));
+    }
+
+    @Test
+    public void selectById() {
+        mapper.selectById(1L);
+        logicMapper.selectById(1L);
+    }
+
+    @Test
+    public void selectBatchIds() {
+        List<Long> ids = Arrays.asList(1014132604940615682L, 1014132604940615652L);
+        mapper.selectBatchIds(ids);
+        logicMapper.selectBatchIds(ids);
+    }
+
+    @Test
+    public void selectByMap() {
+        Map<String, Object> map = new HashMap<>();
+        map.put("id", 1L);
+        map.put("test_int", 1);
+        mapper.selectByMap(map);
+        logicMapper.selectByMap(map);
+    }
+
+    @Test
+    public void selectOne() {
+        mapper.selectOne(new QueryWrapper<TestData>().lambda()
+            .eq(TestData::getId, 1L).eq(TestData::getTestInt, 1));
+        logicMapper.selectOne(new QueryWrapper<LogicTestData>().lambda()
+            .eq(LogicTestData::getId, 1L).eq(LogicTestData::getTestInt, 1));
+    }
+
+    @Test
+    public void selectList() {
+        mapper.selectList(new QueryWrapper<TestData>().lambda()
+            .eq(TestData::getId, 1L).eq(TestData::getTestInt, 1));
+        logicMapper.selectList(new QueryWrapper<LogicTestData>().lambda()
+            .eq(LogicTestData::getId, 1L).eq(LogicTestData::getTestInt, 1));
+        logicMapper.selectList(null);
+    }
+
+    @Test
+    public void commonSelectList() {
+        println(mapper.selectList(new QueryWrapper<TestData>()
+            .eq("id", 1L)
+            .like("test_str", 1)
+            .between("test_double", 1L, 2L)));
+    }
+
+    @Test
+    public void specialSelectList() {
+        println(mapper.selectList(new QueryWrapper<TestData>().lambda()
+            .nested(i -> i.eq(TestData::getId, 1L))
+            .or(i -> i.between(TestData::getTestDouble, 1L, 2L))
+            .or(i -> i.eq(TestData::getTestInt, 1)
+                .or().eq(TestData::getTestDate, 1)
+            )
+            .eq(TestData::getTestBoolean, true)
+            .eq(TestData::getTestDate, LocalDate.of(2008, 8, 8))
+            .between(TestData::getTestDate, LocalDate.of(2008, 1, 1),
+                LocalDate.of(2008, 12, 12))));
+    }
+
+    @Test
+    public void selectPage() {
+        IPage<TestData> page = new Page<>();
+        page.setSize(5).setCurrent(1);
+        IPage<TestData> dataPage = mapper.selectPage(page, null);
+        Assert.assertSame(dataPage, page);
+        System.out.println(String.format("total = {%s}", dataPage.getTotal()));
+        System.out.println(String.format("data.size = {%s}", dataPage.getRecords().size()));
+        println(page.getRecords());
+        System.out.println(JSON.toJSONString(page));
+
+        IPage<LogicTestData> logicPage = new Page<>();
+        logicPage.setSize(5).setCurrent(1);
+        IPage<LogicTestData> logicDataPage = logicMapper.selectPage(logicPage, null);
+        Assert.assertSame(logicDataPage, logicPage);
+        System.out.println(String.format("total = {%s}", logicDataPage.getTotal()));
+        System.out.println(String.format("data.size = {%s}", logicDataPage.getRecords().size()));
+        println(logicDataPage.getRecords());
+        System.out.println(JSON.toJSONString(logicDataPage));
+
+    }
+
+    @Test
+    public void testIn() {
+        println(mapper.selectList(new QueryWrapper<TestData>()
+//            .in("test_int", Arrays.asList(1, 2, 3))//ok
+//                .notIn("test_int", Arrays.asList(1, 2, 3)//ok
+//                .in("test_int", 1, 2, 3)//ok
+//                .notIn("test_int", 1, 2, 3)//ok
+                .inSql("test_int", "1,2,3")//ok
+                .notInSql("test_int", "2,3")//ok
+        ));
+    }
+
+    @Test
+    public void testExists() {
+        println(mapper.selectList(new QueryWrapper<TestData>()
+            .exists("select * from test_data")//ok
+            .or()
+            .notExists("select * from test_data")//ok
+        ));
+        /* exists 连着用是可行的 */
+    }
+
+    @Test
+    public void testApply() {
+        println(mapper.selectList(new QueryWrapper<TestData>()
+            .apply("test_int = 1")
+        ));
+    }
+
+    private <T> void println(List<T> list) {
+        list.forEach(System.out::println);
+    }
+}

+ 36 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgres/config/DBConfig.java

@@ -0,0 +1,36 @@
+package com.baomidou.mybatisplus.test.postgres.config;
+
+import org.h2.Driver;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+import org.springframework.jdbc.datasource.SimpleDriverDataSource;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+
+/**
+ * @author miemie
+ * @since 2018/6/7
+ */
+@Configuration
+@EnableTransactionManagement
+public class DBConfig {
+
+    @Bean("dataSource")
+    public DataSource dataSource() throws SQLException {
+        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
+        dataSource.setDriver(new Driver());
+        dataSource.setDriverClass(org.postgresql.Driver.class);
+        dataSource.setUrl("jdbc:postgresql://localhost:5432/mybatis_plus");
+        dataSource.setUsername("postgres");
+        dataSource.setPassword("123123");
+        return dataSource;
+    }
+
+    @Bean
+    public DataSourceTransactionManager transactionManager(DataSource ds) {
+        return new DataSourceTransactionManager(ds);
+    }
+}

+ 63 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgres/config/MybatisPlusConfig.java

@@ -0,0 +1,63 @@
+package com.baomidou.mybatisplus.test.postgres.config;
+
+import com.baomidou.mybatisplus.annotation.DbType;
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
+import com.baomidou.mybatisplus.core.config.GlobalConfig;
+import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
+import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
+import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
+import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
+import com.baomidou.mybatisplus.test.postgres.PostgresMetaObjectHandler;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.apache.ibatis.type.JdbcType;
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import javax.sql.DataSource;
+
+/**
+ * <p>
+ * Mybatis Plus Config
+ * </p>
+ *
+ * @author Caratacus
+ * @since 2017/4/1
+ */
+@Configuration
+@MapperScan("com.baomidou.mybatisplus.test.base.mapper")
+public class MybatisPlusConfig {
+
+    @Bean("mybatisSqlSession")
+    public SqlSessionFactory sqlSessionFactory(DataSource dataSource, GlobalConfig globalConfig) throws Exception {
+        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
+        /* 数据源 */
+        sqlSessionFactory.setDataSource(dataSource);
+        /* entity扫描,mybatis的Alias功能 */
+        sqlSessionFactory.setTypeAliasesPackage("com.baomidou.mybatisplus.test.base.entity");
+        MybatisConfiguration configuration = new MybatisConfiguration();
+        configuration.setJdbcTypeForNull(JdbcType.NULL);
+        /* 驼峰转下划线 */
+        configuration.setMapUnderscoreToCamelCase(true);
+        /* 分页插件 */
+        PaginationInterceptor pagination = new PaginationInterceptor();
+        configuration.addInterceptor(pagination);
+        /* 乐观锁插件 */
+        configuration.addInterceptor(new OptimisticLockerInterceptor());
+        sqlSessionFactory.setConfiguration(configuration);
+        /* 自动填充插件 */
+        globalConfig.setMetaObjectHandler(new PostgresMetaObjectHandler());
+        sqlSessionFactory.setGlobalConfig(globalConfig);
+        return sqlSessionFactory.getObject();
+    }
+
+    @Bean
+    public GlobalConfig globalConfig() {
+        GlobalConfig conf = new GlobalConfig();
+        conf.setDbConfig(new GlobalConfig.DbConfig().setDbType(DbType.POSTGRE_SQL));
+        /* 逻辑删除注入器 */
+        LogicSqlInjector logicSqlInjector = new LogicSqlInjector();
+        conf.setSqlInjector(logicSqlInjector);
+        return conf;
+    }
+}

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

@@ -0,0 +1,35 @@
+package com.baomidou.mybatisplus.test.postgres.config;
+
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.baomidou.mybatisplus.test.base.db.BaseDb;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+import javax.sql.DataSource;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+public class PostgresDb extends BaseDb {
+
+    public static void initMysqlData() 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()) {
+            initData(conn, "/postgres/", "test_data.ddl.sql");
+        }
+    }
+
+    public static void initData(Connection conn, String path, String ddlFileName)
+        throws SQLException, IOException {
+        String createTableSql = readFile(path, ddlFileName);
+        String[] sqls = createTableSql.split(";");
+        Statement stmt = conn.createStatement();
+        for (String sql : sqls) {
+            if (StringUtils.isNotEmpty(sql)) {
+                stmt.execute(sql);
+            }
+        }
+    }
+}

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

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

+ 10 - 0
mybatis-plus/src/test/resources/postgres/spring-test-postgres.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:context="http://www.springframework.org/schema/context"
+       xmlns="http://www.springframework.org/schema/beans"
+       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;com.baomidou.mybatisplus.test.base.service"/>
+</beans>

+ 30 - 0
mybatis-plus/src/test/resources/postgres/test_data.ddl.sql

@@ -0,0 +1,30 @@
+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
+);
+
+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
+);