Browse Source

完善测试,发现bug

miemie 7 years ago
parent
commit
7fb6829e52

+ 12 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/base/entity/TestData.java

@@ -4,6 +4,11 @@ import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
 
+import com.baomidou.mybatisplus.annotation.FieldFill;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableLogic;
+import com.baomidou.mybatisplus.annotation.TableName;
+
 import lombok.Data;
 import lombok.experimental.Accessors;
 
@@ -13,6 +18,7 @@ import lombok.experimental.Accessors;
  */
 @Data
 @Accessors(chain = true)
+@TableName(value = "tb_test_data")
 public class TestData {
 
     private Long id;
@@ -24,4 +30,10 @@ public class TestData {
     private LocalTime testTime;
     private LocalDateTime testDateTime;
     private LocalDateTime testTimestamp;
+    @TableField(fill = FieldFill.INSERT)
+    private LocalDateTime createDatetime;
+    @TableField(fill = FieldFill.UPDATE)
+    private LocalDateTime updateDatetime;
+    @TableLogic
+    private Boolean deleted;
 }

+ 65 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlMetaObjectHandler.java

@@ -0,0 +1,65 @@
+package com.baomidou.mybatisplus.test.mysql;
+/*
+ * 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.
+ */
+
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+
+import org.apache.ibatis.reflection.MetaObject;
+
+import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
+
+
+/**
+ * <p>
+ * 测试,自定义元对象字段填充控制器,实现公共字段自动写入
+ * </p>
+ *
+ * @author hubin
+ * @since 2017-06-25
+ */
+public class MysqlMetaObjectHandler extends 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);
+            this.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("updateDatetime", LocalDateTime.now(), metaObject);
+        this.setFieldValByName("updateDatetime", LocalDateTime.now(), metaObject);
+    }
+}
+

+ 22 - 11
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlTestDataMapperTest.java

@@ -36,7 +36,7 @@ import com.baomidou.mybatisplus.test.base.mapper.TestDataMapper;
 public class MysqlTestDataMapperTest {
 
     @Resource
-    protected TestDataMapper testDataMapper;
+    protected TestDataMapper mapper;
 
     @Test
     public void insertForeach() {
@@ -44,21 +44,32 @@ public class MysqlTestDataMapperTest {
         LocalDate nowDate = nowDateTime.toLocalDate();
         LocalTime nowTime = nowDateTime.toLocalTime();
         for (int i = 0; i < 20; i++) {
-            testDataMapper.insert(new TestData().setTestInt(i).setTestStr(String.format("第%s条数据", 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));
         }
     }
 
+    @Test
+    public void updateByIdTest() {
+        mapper.updateById(new TestData().setId(1014132604940615682L).setTestInt(1111111111));
+    }
+
+    @Test
+    public void updateTest() {
+        mapper.update(new TestData().setTestInt(222222222), new UpdateWrapper<TestData>()
+            .set("test_str", "我佛慈悲2").eq("id",1014132605058056193L));//todo 发现一个bug
+    }
+
     @Test
     public void selectById() {
-        System.out.println(testDataMapper.selectById(1L));
+        System.out.println(mapper.selectById(1L));
     }
 
     @Test
     public void commonSelectList() {
-        println(testDataMapper.selectList(new QueryWrapper<TestData>()
+        println(mapper.selectList(new QueryWrapper<TestData>()
             .eq("id", 1L)
             .like("test_str", 1)
             .between("test_double", 1L, 2L)));
@@ -66,7 +77,7 @@ public class MysqlTestDataMapperTest {
 
     @Test
     public void specialSelectList() {
-        println(testDataMapper.selectList(new QueryWrapper<TestData>().lambda()
+        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)
@@ -80,7 +91,7 @@ public class MysqlTestDataMapperTest {
 
     @Test
     public void update() {
-        testDataMapper.update(new TestData().setId(1L).setTestStr("123123"),
+        mapper.update(new TestData().setId(1L).setTestStr("123123"),
             new UpdateWrapper<TestData>().eq("id", 1L));
     }
 
@@ -89,14 +100,14 @@ public class MysqlTestDataMapperTest {
         Map<String, Object> map = new HashMap<>();
         map.put("id", 1L);
         map.put("test_int", 1);
-        println(testDataMapper.selectByMap(map));
+        println(mapper.selectByMap(map));
     }
 
     @Test
     public void selectPage() {
         IPage<TestData> page = new Page<>();
         page.setSize(5).setCurrent(1);
-        IPage<TestData> dataPage = testDataMapper.selectPage(page, new QueryWrapper<TestData>().lambda()
+        IPage<TestData> dataPage = mapper.selectPage(page, new QueryWrapper<TestData>().lambda()
             .eq(TestData::getTestInt, 1));
         Assert.assertSame(dataPage, page);
         System.out.println(dataPage.getTotal());
@@ -106,7 +117,7 @@ public class MysqlTestDataMapperTest {
 
     @Test
     public void testIn() {
-        println(testDataMapper.selectList(new QueryWrapper<TestData>()
+        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
@@ -118,7 +129,7 @@ public class MysqlTestDataMapperTest {
 
     @Test
     public void testExists() {
-        println(testDataMapper.selectList(new QueryWrapper<TestData>()
+        println(mapper.selectList(new QueryWrapper<TestData>()
             .exists("select * from test_data")//ok
             .or()
             .notExists("select * from test_data")//ok
@@ -128,7 +139,7 @@ public class MysqlTestDataMapperTest {
 
     @Test
     public void testApply() {
-        println(testDataMapper.selectList(new QueryWrapper<TestData>()
+        println(mapper.selectList(new QueryWrapper<TestData>()
             .apply("test_int = 1")
         ));
     }

+ 2 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/config/DBConfig.java

@@ -23,10 +23,10 @@ public class DBConfig {
     public DataSource dataSource() throws SQLException {
         SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
         dataSource.setDriver(new Driver());
-        dataSource.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=UTF-8");
+        dataSource.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=UTF-8");
         dataSource.setDriverClass(com.mysql.jdbc.Driver.class);
         dataSource.setUsername("root");
-        dataSource.setPassword("123456");
+        dataSource.setPassword("123123");
         return dataSource;
     }
 

+ 17 - 9
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/config/MybatisPlusConfig.java

@@ -11,9 +11,11 @@ import org.springframework.context.annotation.Configuration;
 
 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.PaginationInterceptor;
 import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
 import com.baomidou.mybatisplus.test.h2.H2MetaObjectHandler;
+import com.baomidou.mybatisplus.test.mysql.MysqlMetaObjectHandler;
 
 /**
  * <p>
@@ -30,16 +32,18 @@ public class MybatisPlusConfig {
     @Bean("mybatisSqlSession")
     public SqlSessionFactory sqlSessionFactory(DataSource dataSource, GlobalConfig globalConfig) throws Exception {
         MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
+        /* 数据源 */
         sqlSessionFactory.setDataSource(dataSource);
-//        sqlSessionFactory.setConfigLocation(resourceLoader.getResource("classpath:mybatis-config.xml"));
-//        sqlSessionFactory.setTypeAliasesPackage("com.baomidou.mybatisplus.test.h2.entity.persistent");
+        /* entity扫描,mybatis的Alias功能 */
+        sqlSessionFactory.setTypeAliasesPackage("com.baomidou.mybatisplus.test.base.entity");
         MybatisConfiguration configuration = new MybatisConfiguration();
-//        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
-//        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
         configuration.setJdbcTypeForNull(JdbcType.NULL);
+        /* 驼峰转下划线 */
         configuration.setMapUnderscoreToCamelCase(true);
+        /* 分页插件 */
         PaginationInterceptor pagination = new PaginationInterceptor();
         configuration.addInterceptor(pagination);
+
         sqlSessionFactory.setConfiguration(configuration);
 //        pagination.setLocalPage(true);
 //        OptimisticLockerInterceptor optLock = new OptimisticLockerInterceptor();
@@ -48,7 +52,7 @@ public class MybatisPlusConfig {
 //            optLock,
 //            new PerformanceInterceptor()
 //        });
-        globalConfig.setMetaObjectHandler(new H2MetaObjectHandler());
+        globalConfig.setMetaObjectHandler(new MysqlMetaObjectHandler());
         sqlSessionFactory.setGlobalConfig(globalConfig);
         return sqlSessionFactory.getObject();
     }
@@ -56,10 +60,14 @@ public class MybatisPlusConfig {
     @Bean
     public GlobalConfig globalConfig() {
         GlobalConfig conf = new GlobalConfig();
-//        LogicSqlInjector logicSqlInjector = new LogicSqlInjector();
-//        conf.setLogicDeleteValue("-1");
-//        conf.setLogicNotDeleteValue("1");
-        conf.setDbConfig(new GlobalConfig.DbConfig().setIdType(IdType.ID_WORKER));
+        conf.setDbConfig(new GlobalConfig.DbConfig());
+        /* 逻辑删除注入器 */
+        LogicSqlInjector logicSqlInjector = new LogicSqlInjector();
+        conf.setSqlInjector(logicSqlInjector);
+        conf.setDbConfig(new GlobalConfig.DbConfig()
+            .setIdType(IdType.ID_WORKER)
+            .setLogicDeleteValue("1")
+            .setLogicNotDeleteValue("0"));
         return conf;
     }
 }

+ 16 - 36
mybatis-plus/src/test/resources/mysql/initData.sql

@@ -1,36 +1,16 @@
-CREATE TABLE IF NOT EXISTS test_data (
-    id             BIGINT primary key,
-    test_int       integer,
-    test_str       varchar(50),
-    test_double    double,
-    test_boolean   tinyint(1),
-    test_date      date,
-    test_time      time,
-    test_date_time datetime,
-    test_timestamp timestamp
-)ENGINE = innodb DEFAULT CHARSET = utf8;
-
-insert into test_data
-values (1, 1, '1', 1.1, 1, '2008-08-08', '12:12:12', '2008-08-08 12:12:12', '2008-08-08 12:12:12');
-insert into test_data
-values (2, 1, '1', 1.1, 1, '2008-08-08', '12:12:12', '2008-08-08 12:12:12', '2008-08-08 12:12:12');
-insert into test_data
-values (3, 1, '1', 1.1, 1, '2008-08-08', '12:12:12', '2008-08-08 12:12:12', '2008-08-08 12:12:12');
-insert into test_data
-values (4, 1, '1', 1.1, 1, '2008-08-08', '12:12:12', '2008-08-08 12:12:12', '2008-08-08 12:12:12');
-insert into test_data
-values (5, 1, '1', 1.1, 1, '2008-08-08', '12:12:12', '2008-08-08 12:12:12', '2008-08-08 12:12:12');
-insert into test_data
-values (6, 1, '1', 1.1, 1, '2008-08-08', '12:12:12', '2008-08-08 12:12:12', '2008-08-08 12:12:12');
-insert into test_data
-values (7, 1, '1', 1.1, 1, '2008-08-08', '12:12:12', '2008-08-08 12:12:12', '2008-08-08 12:12:12');
-insert into test_data
-values (8, 1, '1', 1.1, 1, '2008-08-08', '12:12:12', '2008-08-08 12:12:12', '2008-08-08 12:12:12');
-insert into test_data
-values (9, 1, '1', 1.1, 1, '2008-08-08', '12:12:12', '2008-08-08 12:12:12', '2008-08-08 12:12:12');
-insert into test_data
-values (10, 1, '1', 1.1, 1, '2008-08-08', '12:12:12', '2008-08-08 12:12:12', '2008-08-08 12:12:12');
-insert into test_data
-values (11, 1, '1', 1.1, 1, '2008-08-08', '12:12:12', '2008-08-08 12:12:12', '2008-08-08 12:12:12');
-insert into test_data
-values (12, 1, '1', 1.1, 1, '2008-08-08', '12:12:12', '2008-08-08 12:12:12', '2008-08-08 12:12:12');
+CREATE TABLE IF NOT EXISTS tb_test_data (
+  id              BIGINT primary key,
+  test_int        integer,
+  test_str        varchar(50),
+  test_double     double,
+  test_boolean    tinyint(1),
+  test_date       date,
+  test_time       time,
+  test_date_time  datetime,
+  test_timestamp  timestamp,
+  create_datetime datetime,
+  update_datetime datetime,
+  deleted         tinyint(1) default 0
+)
+  ENGINE = innodb
+  DEFAULT CHARSET = utf8;