Ver código fonte

我举起了个出现两次获取序列的栗子.

nieqiuqiu 5 anos atrás
pai
commit
7a758cb9a6

+ 28 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2KeyGeneratorTest.java

@@ -0,0 +1,28 @@
+package com.baomidou.mybatisplus.test.h2;
+
+import com.baomidou.mybatisplus.test.h2.keygenerator.mapper.KeyGeneratorMapper;
+import com.baomidou.mybatisplus.test.h2.keygenerator.model.KeyGeneratorModel;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+@ExtendWith(SpringExtension.class)
+@ContextConfiguration(locations = {"classpath:h2/spring-keygenerator-test-h2.xml"})
+class H2KeyGeneratorTest {
+
+    @Autowired
+    private KeyGeneratorMapper keyGeneratorMapper;
+
+    @Test
+    void test(){
+        KeyGeneratorModel keyGeneratorModel = new KeyGeneratorModel();
+        keyGeneratorModel.setName("我举起了个栗子");
+        keyGeneratorMapper.insert(keyGeneratorModel);
+    }
+
+}

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

@@ -41,6 +41,8 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
 @EnableTransactionManagement
 public class DBConfig {
 
+    private String locationPattern = "classpath:/h2/*.sql";
+
     @Bean
     public DataSource dataSource(){
         SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
@@ -69,7 +71,7 @@ public class DBConfig {
         ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
         resourceDatabasePopulator.setContinueOnError(false);
         resourceDatabasePopulator.addScripts(
-            new PathMatchingResourcePatternResolver().getResources("classpath:/h2/*.sql")
+            new PathMatchingResourcePatternResolver().getResources(locationPattern)
         );
         return resourceDatabasePopulator;
     }
@@ -79,4 +81,11 @@ public class DBConfig {
         return new JdbcTemplate(ds);
     }
 
+    public String getLocationPattern() {
+        return locationPattern;
+    }
+
+    public void setLocationPattern(String locationPattern) {
+        this.locationPattern = locationPattern;
+    }
 }

+ 2 - 4
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/config/MybatisPlusConfig.java

@@ -103,11 +103,9 @@ public class MybatisPlusConfig {
         });
         sqlExplainInterceptor.setSqlParserList(sqlParserList);
         OptimisticLockerInterceptor optLock = new OptimisticLockerInterceptor();
-        sqlSessionFactory.setPlugins(new Interceptor[]{
-            pagination,
+        sqlSessionFactory.setPlugins(pagination,
             optLock,
-            sqlExplainInterceptor
-        });
+            sqlExplainInterceptor);
         globalConfig.setMetaObjectHandler(new H2MetaObjectHandler());
         globalConfig.setSqlInjector(new DefaultSqlInjector() {
 

+ 63 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/keygenerator/KeyGeneratorConfig.java

@@ -0,0 +1,63 @@
+/*
+ * 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;
+
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
+import com.baomidou.mybatisplus.core.config.GlobalConfig;
+import com.baomidou.mybatisplus.extension.incrementer.H2KeyGenerator;
+import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
+import com.baomidou.mybatisplus.extension.plugins.SqlExplainInterceptor;
+import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
+import org.apache.ibatis.session.ExecutorType;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.apache.ibatis.type.EnumOrdinalTypeHandler;
+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;
+
+@Configuration
+@MapperScan("com.baomidou.mybatisplus.test.h2.keygenerator.mapper")
+public class KeyGeneratorConfig {
+
+    @Bean("mybatisSqlSession")
+    public SqlSessionFactory sqlSessionFactory(DataSource dataSource, GlobalConfig globalConfig) throws Exception {
+        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
+        sqlSessionFactory.setDataSource(dataSource);
+        MybatisConfiguration configuration = new MybatisConfiguration();
+        configuration.setJdbcTypeForNull(JdbcType.NULL);
+        configuration.setMapUnderscoreToCamelCase(true);
+        configuration.setDefaultExecutorType(ExecutorType.REUSE);
+        configuration.setDefaultEnumTypeHandler(EnumOrdinalTypeHandler.class);
+        sqlSessionFactory.setConfiguration(configuration);
+        PaginationInterceptor pagination = new PaginationInterceptor();
+        SqlExplainInterceptor sqlExplainInterceptor = new SqlExplainInterceptor();
+        sqlSessionFactory.setPlugins(
+            pagination,
+            sqlExplainInterceptor);
+        sqlSessionFactory.setGlobalConfig(globalConfig);
+        return sqlSessionFactory.getObject();
+    }
+
+    @Bean
+    public GlobalConfig globalConfiguration() {
+        GlobalConfig conf = new GlobalConfig();
+        conf.setDbConfig(new GlobalConfig.DbConfig().setKeyGenerator(new H2KeyGenerator()));
+        return conf;
+    }
+}

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

@@ -0,0 +1,8 @@
+package com.baomidou.mybatisplus.test.h2.keygenerator.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.test.h2.keygenerator.model.KeyGeneratorModel;
+
+public interface KeyGeneratorMapper extends BaseMapper<KeyGeneratorModel> {
+
+}

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

@@ -0,0 +1,16 @@
+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 KeyGeneratorModel {
+
+    @TableId(type = IdType.INPUT)
+    private Long id;
+
+    private String name;
+
+}

+ 15 - 0
mybatis-plus/src/test/resources/h2/spring-keygenerator-test-h2.xml

@@ -0,0 +1,15 @@
+<?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-3.0.xsd">
+
+    <context:component-scan base-package="com.baomidou.mybatisplus.test.h2.keygenerator"/>
+
+    <bean class="com.baomidou.mybatisplus.test.h2.config.DBConfig">
+        <property name="locationPattern" value="classpath:/keygenerator/*.sql"/>
+    </bean>
+    <bean class="com.baomidou.mybatisplus.test.h2.keygenerator.KeyGeneratorConfig"/>
+
+</beans>

+ 11 - 0
mybatis-plus/src/test/resources/keygenerator/init.ddl.sql

@@ -0,0 +1,11 @@
+CREATE TABLE IF NOT EXISTS  key_generator_model (
+	id BIGINT(20) NOT NULL AUTO_INCREMENT,
+	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;
+