leesam 1 рік тому
батько
коміт
53199b32f6

+ 27 - 1
src/main/java/com/genersoft/iot/vmp/conf/MybatisConfig.java

@@ -1,6 +1,8 @@
 package com.genersoft.iot.vmp.conf;
 
 import org.apache.ibatis.logging.stdout.StdOutImpl;
+import org.apache.ibatis.mapping.DatabaseIdProvider;
+import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.mybatis.spring.SqlSessionFactoryBean;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -9,6 +11,7 @@ import org.springframework.context.annotation.Configuration;
 import org.springframework.core.annotation.Order;
 
 import javax.sql.DataSource;
+import java.util.Properties;
 
 /**
  * 配置mybatis
@@ -21,7 +24,29 @@ public class MybatisConfig {
     private UserSetting userSetting;
 
     @Bean
-    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
+    public DatabaseIdProvider databaseIdProvider() {
+        VendorDatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
+        Properties properties = new Properties();
+        properties.setProperty("Oracle", "oracle");
+        properties.setProperty("MySQL", "mysql");
+        properties.setProperty("DB2", "db2");
+        properties.setProperty("Derby", "derby");
+        properties.setProperty("H2", "h2");
+        properties.setProperty("HSQL", "hsql");
+        properties.setProperty("Informix", "informix");
+        properties.setProperty("MS-SQL", "ms-sql");
+        properties.setProperty("PostgreSQL", "postgresql");
+        properties.setProperty("Sybase", "sybase");
+        properties.setProperty("Hana", "hana");
+        properties.setProperty("DM", "dm");
+        properties.setProperty("KingbaseES", "kingbase");
+        properties.setProperty("KingBase8", "kingbase");
+        databaseIdProvider.setProperties(properties);
+        return databaseIdProvider;
+    }
+
+    @Bean
+    public SqlSessionFactory sqlSessionFactory(DataSource dataSource, DatabaseIdProvider databaseIdProvider) throws Exception {
        final SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
         sqlSessionFactory.setDataSource(dataSource);
         org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration();
@@ -30,6 +55,7 @@ public class MybatisConfig {
         }
         config.setMapUnderscoreToCamelCase(true);
         sqlSessionFactory.setConfiguration(config);
+        sqlSessionFactory.setDatabaseIdProvider(databaseIdProvider);
         return sqlSessionFactory.getObject();
     }
 

+ 2 - 1
src/main/java/com/genersoft/iot/vmp/storager/dao/UserApiKeyMapper.java

@@ -10,7 +10,8 @@ import java.util.List;
 @Repository
 public interface UserApiKeyMapper {
 
-    @SelectKey(statement = "SELECT LAST_INSERT_ID() AS id", keyProperty = "id", before = false, resultType = Integer.class)
+    @SelectKey(databaseId = "postgresql", statement = "SELECT currval('wvp_user_api_key_id_seq'::regclass) AS id", keyProperty = "id", before = false, resultType = Integer.class)
+    @SelectKey(databaseId = "mysql", statement = "SELECT LAST_INSERT_ID() AS id", keyProperty = "id", before = false, resultType = Integer.class)
     @Insert("INSERT INTO wvp_user_api_key (user_id, app, api_key, expired_at, remark, enable, create_time, update_time) VALUES" +
             "(#{userId}, #{app}, #{apiKey}, #{expiredAt}, #{remark}, #{enable}, #{createTime}, #{updateTime})")
     int add(UserApiKey userApiKey);

+ 3 - 3
src/main/java/com/genersoft/iot/vmp/storager/dao/dto/UserApiKey.java

@@ -38,7 +38,7 @@ public class UserApiKey implements Serializable {
      * 过期时间(null=永不过期)
      */
     @Schema(description = "过期时间(null=永不过期)")
-    private String expiredAt;
+    private long expiredAt;
 
     /**
      * 备注信息
@@ -101,11 +101,11 @@ public class UserApiKey implements Serializable {
         this.apiKey = apiKey;
     }
 
-    public String getExpiredAt() {
+    public long getExpiredAt() {
         return expiredAt;
     }
 
-    public void setExpiredAt(String expiredAt) {
+    public void setExpiredAt(long expiredAt) {
         this.expiredAt = expiredAt;
     }
 

+ 3 - 3
src/main/java/com/genersoft/iot/vmp/vmanager/user/UserApiKeyController.java

@@ -75,7 +75,7 @@ public class UserApiKeyController {
         userApiKey.setApp(app);
         userApiKey.setApiKey(null);
         userApiKey.setRemark(remark);
-        userApiKey.setExpiredAt(expiresAt);
+        userApiKey.setExpiredAt(expirationTime != null ? expirationTime : 0);
         userApiKey.setEnable(enable != null ? enable : false);
         userApiKey.setCreateTime(DateUtil.getNow());
         userApiKey.setUpdateTime(DateUtil.getNow());
@@ -182,8 +182,8 @@ public class UserApiKeyController {
             throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户不存在");
         }
         Long expirationTime = null;
-        if (userApiKey.getExpiredAt() != null) {
-            long timestamp = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestampMs(userApiKey.getExpiredAt());
+        if (userApiKey.getExpiredAt() > 0) {
+            long timestamp = userApiKey.getExpiredAt();
             expirationTime = (timestamp - System.currentTimeMillis()) / (60 * 1000);
             if (expirationTime < 0) {
                 throw new ControllerException(ErrorCode.ERROR400.getCode(), "ApiKey已失效");