Explorar o código

测试mysql自增

hubin %!s(int64=3) %!d(string=hai) anos
pai
achega
5e5c8bb4d2

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

@@ -22,15 +22,13 @@ import com.baomidou.mybatisplus.test.mysql.entity.MysqlData;
 import com.baomidou.mybatisplus.test.mysql.entity.ResultMapEntity;
 import com.baomidou.mybatisplus.test.mysql.enums.TestEnum;
 import com.baomidou.mybatisplus.test.mysql.mapper.MysqlDataMapper;
-import com.baomidou.mybatisplus.test.mysql.mapper.children.CommonDataChildrenMapper;
-import com.baomidou.mybatisplus.test.mysql.mapper.children.CommonLogicDataChildrenMapper;
-import com.baomidou.mybatisplus.test.mysql.mapper.commons.CommonDataCopyMapper;
 import com.baomidou.mybatisplus.test.mysql.mapper.commons.CommonDataMapper;
 import com.baomidou.mybatisplus.test.mysql.mapper.commons.CommonLogicDataMapper;
 import com.baomidou.mybatisplus.test.mysql.mapper.commons.ResultMapEntityMapper;
 import org.apache.ibatis.mapping.MappedStatement;
 import org.apache.ibatis.session.Configuration;
 import org.apache.ibatis.session.SqlSessionFactory;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.MethodOrderer;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.TestMethodOrder;
@@ -66,14 +64,8 @@ class MysqlTestDataMapperTest {
     private static final int fail = 0;
     @Resource(name = "commonDataMapper")
     protected CommonDataMapper commonDataMapper;
-    @Resource(name = "commonDataChildrenMapper")
-    protected CommonDataChildrenMapper commonDataChildrenMapper;
     @Resource(name = "commonLogicDataMapper")
     protected CommonLogicDataMapper commonLogicDataMapper;
-    @Resource(name = "commonLogicDataChildrenMapper")
-    protected CommonLogicDataChildrenMapper commonLogicDataChildrenMapper;
-    @Resource
-    protected CommonDataCopyMapper commonDataCopyMapper;
     @Resource
     protected ResultMapEntityMapper resultMapEntityMapper;
     @Autowired
@@ -105,6 +97,19 @@ class MysqlTestDataMapperTest {
         }
     }
 
+    @Test
+    void a011_insertIdAuto() {
+        commonDataMapper.delete(null);
+        for (int i = 0; i < 20; i++) {
+            String str = String.format("第%s条数据", i);
+            commonDataMapper.insert(new CommonData().setTestInt(i).setTestStr(str)
+                    .setId(Long.valueOf(i))
+                .setTestEnum(TestEnum.ONE));
+        }
+        Assertions.assertEquals(20, commonDataMapper.selectCount(null));
+        commonDataMapper.delete(null);
+    }
+
     @Test
     void a02_insertBatch() {
         int size = 9;

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

@@ -42,7 +42,7 @@ public class DBConfig {
         dataSource.setDriverClass(com.mysql.cj.jdbc.Driver.class);
         dataSource.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=UTF-8");
         dataSource.setUsername("root");
-        dataSource.setPassword("123456");
+        dataSource.setPassword("");
         return dataSource;
     }
 

+ 2 - 4
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/entity/CommonData.java

@@ -15,9 +15,7 @@
  */
 package com.baomidou.mybatisplus.test.mysql.entity;
 
-import com.baomidou.mybatisplus.annotation.FieldFill;
-import com.baomidou.mybatisplus.annotation.TableField;
-import com.baomidou.mybatisplus.annotation.Version;
+import com.baomidou.mybatisplus.annotation.*;
 import com.baomidou.mybatisplus.extension.activerecord.Model;
 import com.baomidou.mybatisplus.test.mysql.enums.TestEnum;
 import lombok.Data;
@@ -46,7 +44,7 @@ public class CommonData extends Model<CommonData> {
      * serialVersionUID
      */
     private static final long serialVersionUID = 5651603508613034190L;
-
+    @TableId(type = IdType.AUTO)
     private Long id;
     @TableField(jdbcType = JdbcType.INTEGER)
     private Integer testInt;

+ 0 - 32
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/mapper/children/CommonDataChildrenMapper.java

@@ -1,32 +0,0 @@
-/*
- * Copyright (c) 2011-2020, baomidou (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>
- * https://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.mysql.mapper.children;
-
-import com.baomidou.mybatisplus.test.mysql.entity.CommonData;
-import com.baomidou.mybatisplus.test.mysql.mapper.commons.CommonDataMapper;
-import org.apache.ibatis.annotations.Select;
-
-import java.util.Optional;
-
-/**
- * @author miemie
- * @since 2019-04-11
- */
-public interface CommonDataChildrenMapper extends CommonDataMapper {
-
-    @Select("select * from common_data where id = #{id}")
-    Optional<CommonData> getByIdChildren(Long id);
-}

+ 0 - 35
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/mapper/children/CommonLogicDataChildrenMapper.java

@@ -1,35 +0,0 @@
-/*
- * Copyright (c) 2011-2020, baomidou (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>
- * https://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.mysql.mapper.children;
-
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.baomidou.mybatisplus.core.toolkit.Constants;
-import com.baomidou.mybatisplus.test.mysql.entity.CommonLogicData;
-import com.baomidou.mybatisplus.test.mysql.mapper.commons.CommonLogicDataMapper;
-import org.apache.ibatis.annotations.Param;
-import org.apache.ibatis.annotations.Select;
-
-import java.util.List;
-
-/**
- * @author miemie
- * @since 2018-07-06
- */
-public interface CommonLogicDataChildrenMapper extends CommonLogicDataMapper {
-
-    @Select("select id,test_int from common_logic_data ${ew.customSqlSegment}")
-    List<CommonLogicData> getByWrapper(@Param(Constants.WRAPPER) LambdaQueryWrapper wrapper);
-}

+ 1 - 1
mybatis-plus/src/test/resources/mysql/test_data.ddl.sql

@@ -5,7 +5,7 @@ drop table if exists result_map_entity;
 
 CREATE TABLE common_data
 (
-    id         BIGINT primary key,
+    id         BIGINT primary key NOT NULL AUTO_INCREMENT,
     test_int   integer,
     test_str   varchar(50),
     c_time     datetime,