瀏覽代碼

支持 PG 数据字段大写 ID 自增 fixed issues/I4T0YJ

hubin 2 年之前
父節點
當前提交
1bec5e1c26

+ 3 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Insert.java

@@ -21,6 +21,7 @@ import com.baomidou.mybatisplus.core.injector.AbstractMethod;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.baomidou.mybatisplus.core.toolkit.sql.SqlInjectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
 import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
 import org.apache.ibatis.executor.keygen.KeyGenerator;
@@ -64,7 +65,8 @@ public class Insert extends AbstractMethod {
                 /* 自增主键 */
                 keyGenerator = Jdbc3KeyGenerator.INSTANCE;
                 keyProperty = tableInfo.getKeyProperty();
-                keyColumn = tableInfo.getKeyColumn();
+                // 去除转义符
+                keyColumn = SqlInjectionUtils.removeEscapeCharacter(tableInfo.getKeyColumn());
             } else if (null != tableInfo.getKeySequence()) {
                 keyGenerator = TableInfoHelper.genKeyGenerator(methodName, tableInfo, builderAssistant);
                 keyProperty = tableInfo.getKeyProperty();

+ 10 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfoHelper.java

@@ -428,6 +428,12 @@ public class TableInfoHelper {
             logger.warn(String.format("This primary key of \"%s\" is primitive !不建议如此请使用包装类 in Class: \"%s\"",
                 property, tableInfo.getEntityType().getName()));
         }
+        if (StringUtils.isEmpty(tableId.value())) {
+            String columnFormat = dbConfig.getColumnFormat();
+            if (StringUtils.isNotBlank(columnFormat)) {
+                column = String.format(columnFormat, column);
+            }
+        }
         tableInfo.setKeyRelated(checkRelated(underCamel, property, column))
             .setKeyColumn(column)
             .setKeyProperty(property)
@@ -459,6 +465,10 @@ public class TableInfoHelper {
                 logger.warn(String.format("This primary key of \"%s\" is primitive !不建议如此请使用包装类 in Class: \"%s\"",
                     property, tableInfo.getEntityType().getName()));
             }
+            String columnFormat = dbConfig.getColumnFormat();
+            if (StringUtils.isNotBlank(columnFormat)) {
+                column = String.format(columnFormat, column);
+            }
             tableInfo.setKeyRelated(checkRelated(tableInfo.isUnderCamel(), property, column))
                 .setIdType(dbConfig.getIdType())
                 .setKeyColumn(column)

+ 11 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/SqlInjectionUtils.java

@@ -46,4 +46,15 @@ public class SqlInjectionUtils {
         // 处理是否包含SQL注释字符 || 检查是否包含SQL注入敏感字符
         return SQL_COMMENT_PATTERN.matcher(value).find() || SQL_SYNTAX_PATTERN.matcher(value).find();
     }
+
+    /**
+     * 刪除字段转义符单引号双引号
+     *
+     * @param text 待处理字段
+     * @return
+     */
+    public static String removeEscapeCharacter(String text) {
+        Objects.nonNull(text);
+        return text.replaceAll("\"", "").replaceAll("'", "");
+    }
 }

+ 1 - 0
mybatis-plus/build.gradle

@@ -21,5 +21,6 @@ dependencies {
     testImplementation "${lib.'spring-jdbc'}"
     testImplementation "javax.annotation:javax.annotation-api:1.3.2"
     testImplementation "${lib.cglib}"
+    testImplementation "${lib.postgresql}"
 //    testCompile ('org.apache.phoenix:phoenix-core:5.0.0-HBase-2.0')
 }

+ 31 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgresql/PostgresqlTest.java

@@ -0,0 +1,31 @@
+package com.baomidou.mybatisplus.test.postgresql;
+
+import com.baomidou.mybatisplus.test.postgresql.entity.Pgtable;
+import com.baomidou.mybatisplus.test.postgresql.mapper.PgtableMappper;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+@ExtendWith(SpringExtension.class)
+@ContextConfiguration(locations = {"classpath:postgresql/spring-test-postgresql.xml"})
+public class PostgresqlTest {
+
+    @Resource
+    private PgtableMappper mapper;
+
+    @Test
+    void test() {
+        Pgtable pgtable = Pgtable.builder().compName("test").age(10).build();
+        Assertions.assertTrue(mapper.insert(pgtable) > 0);
+        System.out.println(pgtable.getId());
+
+        List<Pgtable> pgtableList = mapper.selectList(null);
+        Assertions.assertNotNull(pgtableList);
+        Assertions.assertTrue(pgtableList.size() > 0);
+    }
+}

+ 59 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgresql/config/PostgresqlConfig.java

@@ -0,0 +1,59 @@
+package com.baomidou.mybatisplus.test.postgresql.config;
+
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
+import com.baomidou.mybatisplus.core.config.GlobalConfig;
+import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
+import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
+import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.apache.ibatis.type.JdbcType;
+import org.mybatis.spring.annotation.MapperScan;
+import org.postgresql.Driver;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.jdbc.datasource.SimpleDriverDataSource;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
+
+import javax.sql.DataSource;
+
+@Configuration
+@SuppressWarnings("unchecked")
+@EnableTransactionManagement
+@MapperScan("com.baomidou.mybatisplus.test.postgresql.mapper")
+public class PostgresqlConfig {
+
+    @Bean("dataSource")
+    public DataSource dataSource() throws ClassNotFoundException {
+        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
+        dataSource.setDriver(new Driver());
+        dataSource.setUrl("jdbc:postgresql://localhost:5432/test");
+        dataSource.setUsername("postgres");
+        dataSource.setPassword("123456");
+        return dataSource;
+    }
+
+
+    @Bean("mybatisSqlSession")
+    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
+        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
+        /* 数据源 */
+        sqlSessionFactory.setDataSource(dataSource);
+        /* 扫描 typeHandler */
+        MybatisConfiguration configuration = new MybatisConfiguration();
+        configuration.setJdbcTypeForNull(JdbcType.NULL);
+        /* 驼峰转下划线 */
+        configuration.setMapUnderscoreToCamelCase(true);
+        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
+        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
+        sqlSessionFactory.setPlugins(mybatisPlusInterceptor);
+        sqlSessionFactory.setConfiguration(configuration);
+        GlobalConfig globalConfig = new GlobalConfig();
+        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
+        // TODO 这里申明所有字段为大写
+        dbConfig.setCapitalMode(true);
+        dbConfig.setColumnFormat("\"%s\"");
+        globalConfig.setDbConfig(dbConfig);
+        sqlSessionFactory.setGlobalConfig(globalConfig);
+        return sqlSessionFactory.getObject();
+    }
+}

+ 18 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgresql/entity/Pgtable.java

@@ -0,0 +1,18 @@
+package com.baomidou.mybatisplus.test.postgresql.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import lombok.Builder;
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+@Builder
+public class Pgtable {
+    @TableId(type = IdType.AUTO)
+    private Long id;
+    private String compName;
+    private Integer age;
+
+}

+ 8 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgresql/mapper/PgtableMappper.java

@@ -0,0 +1,8 @@
+package com.baomidou.mybatisplus.test.postgresql.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.test.postgresql.entity.Pgtable;
+
+public interface PgtableMappper extends BaseMapper<Pgtable> {
+
+}

+ 9 - 0
mybatis-plus/src/test/resources/postgresql/spring-test-postgresql.xml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns="http://www.springframework.org/schema/beans"
+       xmlns:context="http://www.springframework.org/schema/context"
+       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.postgresql.config"/>
+</beans>