瀏覽代碼

覆盖序列生成单元测试.

nieqiuqiu 5 年之前
父節點
當前提交
726795f1f1

+ 1 - 0
build.gradle

@@ -124,6 +124,7 @@ subprojects {
         exclude "**/TableNameParser.java"
         mapping "java", "SLASHSTAR_STYLE"
         mapping "kt", "SLASHSTAR_STYLE"
+        ignoreFailures = true
     }
 
     repositories {

+ 15 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/IdWorker.java

@@ -15,6 +15,7 @@
  */
 package com.baomidou.mybatisplus.core.toolkit;
 
+import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.incrementer.DefaultIdGenerator;
 import com.baomidou.mybatisplus.core.incrementer.IdGenerator;
 
@@ -43,11 +44,23 @@ public class IdWorker {
      */
     public static final DateTimeFormatter MILLISECOND = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
 
+    /**
+     * 获取唯一ID
+     *
+     * @return id
+     * @deprecated 3.1.2 spring应用可以通过@IdGenerator,非spring应用请自行控制IdGenerator的实例化
+     */
     @Deprecated
     public static long getId() {
         return ID_GENERATOR.nextId(new Object());
     }
 
+    /**
+     * 获取唯一ID
+     *
+     * @return id
+     * @deprecated 3.1.2 spring应用可以通过@IdGenerator,非spring应用请自行控制IdGenerator的实例化
+     */
     @Deprecated
     public static String getIdStr() {
         return String.valueOf(ID_GENERATOR.nextId(new Object()));
@@ -87,6 +100,8 @@ public class IdWorker {
      * 自定义id 生成方式
      *
      * @param idGenerator id 生成器
+     * @see GlobalConfig#setIdGenerator(IdGenerator)
+     * @deprecated 3.1.2
      */
     @Deprecated
     public static void setIdGenerator(IdGenerator idGenerator) {

+ 32 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2KeyGeneratorTest.java

@@ -1,7 +1,13 @@
 package com.baomidou.mybatisplus.test.h2;
 
+import com.baomidou.mybatisplus.test.h2.keygenerator.mapper.ExtendKeyGeneratorMapper;
 import com.baomidou.mybatisplus.test.h2.keygenerator.mapper.KeyGeneratorMapper;
+import com.baomidou.mybatisplus.test.h2.keygenerator.mapper.LongKeyGeneratorMapper;
+import com.baomidou.mybatisplus.test.h2.keygenerator.mapper.StringKeyGeneratorMapper;
+import com.baomidou.mybatisplus.test.h2.keygenerator.model.ExtendKeyGeneratorModel;
 import com.baomidou.mybatisplus.test.h2.keygenerator.model.KeyGeneratorModel;
+import com.baomidou.mybatisplus.test.h2.keygenerator.model.LongKeyGeneratorModel;
+import com.baomidou.mybatisplus.test.h2.keygenerator.model.StringKeyGeneratorModel;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.MethodOrderer;
 import org.junit.jupiter.api.Test;
@@ -19,12 +25,36 @@ class H2KeyGeneratorTest {
     @Autowired
     private KeyGeneratorMapper keyGeneratorMapper;
 
+    @Autowired
+    private LongKeyGeneratorMapper longKeyGeneratorMapper;
+
+    @Autowired
+    private StringKeyGeneratorMapper stringKeyGeneratorMapper;
+
+    @Autowired
+    private ExtendKeyGeneratorMapper extendKeyGeneratorMapper;
+
     @Test
     void test(){
         KeyGeneratorModel keyGeneratorModel = new KeyGeneratorModel();
-        keyGeneratorModel.setName("我举起了个栗子");
+        keyGeneratorModel.setName("我举起了咩咩");
         keyGeneratorMapper.insert(keyGeneratorModel);
-        Assertions.assertNotNull(keyGeneratorModel.getId());
+        Assertions.assertNotNull(keyGeneratorModel.getUid());
+
+        LongKeyGeneratorModel longKeyGeneratorModel = new LongKeyGeneratorModel();
+        longKeyGeneratorModel.setName("我举起了个栗子");
+        longKeyGeneratorMapper.insert(longKeyGeneratorModel);
+        Assertions.assertNotNull(longKeyGeneratorModel.getId());
+
+        StringKeyGeneratorModel stringKeyGeneratorModel = new StringKeyGeneratorModel();
+        stringKeyGeneratorModel.setName("我举起了个锤子");
+        stringKeyGeneratorMapper.insert(stringKeyGeneratorModel);
+        Assertions.assertNotNull(stringKeyGeneratorModel.getId());
+
+        ExtendKeyGeneratorModel extendKeyGeneratorModel = new ExtendKeyGeneratorModel();
+        extendKeyGeneratorModel.setName("我举起了句号");
+        extendKeyGeneratorMapper.insert(extendKeyGeneratorModel);
+        Assertions.assertNotNull(extendKeyGeneratorModel.getUid());
     }
 
 }

+ 23 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/keygenerator/mapper/ExtendKeyGeneratorMapper.java

@@ -0,0 +1,23 @@
+/*
+ * 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.h2.keygenerator.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.test.h2.keygenerator.model.ExtendKeyGeneratorModel;
+
+public interface ExtendKeyGeneratorMapper extends BaseMapper<ExtendKeyGeneratorModel> {
+
+}

+ 15 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/keygenerator/mapper/KeyGeneratorMapper.java

@@ -1,3 +1,18 @@
+/*
+ * 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.h2.keygenerator.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;

+ 23 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/keygenerator/mapper/LongKeyGeneratorMapper.java

@@ -0,0 +1,23 @@
+/*
+ * 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.h2.keygenerator.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.test.h2.keygenerator.model.LongKeyGeneratorModel;
+
+public interface LongKeyGeneratorMapper extends BaseMapper<LongKeyGeneratorModel> {
+
+}

+ 23 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/keygenerator/mapper/StringKeyGeneratorMapper.java

@@ -0,0 +1,23 @@
+/*
+ * 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.h2.keygenerator.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.test.h2.keygenerator.model.StringKeyGeneratorModel;
+
+public interface StringKeyGeneratorMapper extends BaseMapper<StringKeyGeneratorModel> {
+
+}

+ 23 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/keygenerator/model/BaseMode.java

@@ -0,0 +1,23 @@
+/*
+ * 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.h2.keygenerator.model;
+
+import com.baomidou.mybatisplus.annotation.KeySequence;
+
+@KeySequence(value = "key_generator_model_seq")
+class BaseMode {
+
+}

+ 33 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/keygenerator/model/ExtendKeyGeneratorModel.java

@@ -0,0 +1,33 @@
+/*
+ * 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.h2.keygenerator.model;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+@Data
+@TableName(value = "key_generator_model")
+@EqualsAndHashCode(callSuper = true)
+public class ExtendKeyGeneratorModel extends BaseMode {
+
+    @TableId(type = IdType.INPUT, value = "id")
+    private Long uid;
+
+    private String name;
+}

+ 21 - 3
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/keygenerator/model/KeyGeneratorModel.java

@@ -1,6 +1,24 @@
+/*
+ * 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.h2.keygenerator.model;
 
-import com.baomidou.mybatisplus.annotation.*;
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.KeySequence;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
 import lombok.Data;
 
 @Data
@@ -8,8 +26,8 @@ import lombok.Data;
 @KeySequence(value = "key_generator_model_seq")
 public class KeyGeneratorModel {
 
-    @TableId(type = IdType.INPUT)
-    private Long id;
+    @TableId(type = IdType.INPUT, value = "id")
+    private Long uid;
 
     private String name;
 

+ 31 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/keygenerator/model/LongKeyGeneratorModel.java

@@ -0,0 +1,31 @@
+/*
+ * 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.h2.keygenerator.model;
+
+import com.baomidou.mybatisplus.annotation.*;
+import lombok.Data;
+
+@Data
+@TableName(value = "key_generator_model")
+@KeySequence(value = "key_generator_model_seq")
+public class LongKeyGeneratorModel {
+
+    @TableId(type = IdType.INPUT)
+    private Long id;
+
+    private String name;
+
+}

+ 34 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/keygenerator/model/StringKeyGeneratorModel.java

@@ -0,0 +1,34 @@
+/*
+ * 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.h2.keygenerator.model;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.KeySequence;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+@Data
+@TableName(value = "key_generator_model")
+@KeySequence(value = "key_generator_model_seq")
+public class StringKeyGeneratorModel {
+
+    @TableId(type = IdType.INPUT)
+    private String id;
+
+    private String name;
+
+}

+ 1 - 5
mybatis-plus/src/test/resources/keygenerator/init.ddl.sql

@@ -1,11 +1,7 @@
 CREATE TABLE IF NOT EXISTS  key_generator_model (
-	id BIGINT(20) NOT NULL AUTO_INCREMENT,
+	id BIGINT(20) NOT NULL,
 	name VARCHAR(30) NULL DEFAULT NULL ,
-	grade INT(3) NULL,
-	gender VARCHAR(10) NULL,
-	age INT(11) NULL DEFAULT NULL ,
 	PRIMARY KEY (id)
 );
 
 CREATE SEQUENCE key_generator_model_seq START WITH 1 INCREMENT BY 10;
-