适配postgres数据库

pull/1389/head
leesam 2024-04-10 22:16:56 +08:00
parent 16b7e4a7ef
commit 53199b32f6
4 changed files with 35 additions and 8 deletions

View File

@ -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();
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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已失效");