适配postgres数据库
parent
16b7e4a7ef
commit
53199b32f6
|
@ -1,6 +1,8 @@
|
||||||
package com.genersoft.iot.vmp.conf;
|
package com.genersoft.iot.vmp.conf;
|
||||||
|
|
||||||
import org.apache.ibatis.logging.stdout.StdOutImpl;
|
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.apache.ibatis.session.SqlSessionFactory;
|
||||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -9,6 +11,7 @@ import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置mybatis
|
* 配置mybatis
|
||||||
|
@ -21,7 +24,29 @@ public class MybatisConfig {
|
||||||
private UserSetting userSetting;
|
private UserSetting userSetting;
|
||||||
|
|
||||||
@Bean
|
@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();
|
final SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
|
||||||
sqlSessionFactory.setDataSource(dataSource);
|
sqlSessionFactory.setDataSource(dataSource);
|
||||||
org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration();
|
org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration();
|
||||||
|
@ -30,6 +55,7 @@ public class MybatisConfig {
|
||||||
}
|
}
|
||||||
config.setMapUnderscoreToCamelCase(true);
|
config.setMapUnderscoreToCamelCase(true);
|
||||||
sqlSessionFactory.setConfiguration(config);
|
sqlSessionFactory.setConfiguration(config);
|
||||||
|
sqlSessionFactory.setDatabaseIdProvider(databaseIdProvider);
|
||||||
return sqlSessionFactory.getObject();
|
return sqlSessionFactory.getObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,8 @@ import java.util.List;
|
||||||
@Repository
|
@Repository
|
||||||
public interface UserApiKeyMapper {
|
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" +
|
@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})")
|
"(#{userId}, #{app}, #{apiKey}, #{expiredAt}, #{remark}, #{enable}, #{createTime}, #{updateTime})")
|
||||||
int add(UserApiKey userApiKey);
|
int add(UserApiKey userApiKey);
|
||||||
|
|
|
@ -38,7 +38,7 @@ public class UserApiKey implements Serializable {
|
||||||
* 过期时间(null=永不过期)
|
* 过期时间(null=永不过期)
|
||||||
*/
|
*/
|
||||||
@Schema(description = "过期时间(null=永不过期)")
|
@Schema(description = "过期时间(null=永不过期)")
|
||||||
private String expiredAt;
|
private long expiredAt;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 备注信息
|
* 备注信息
|
||||||
|
@ -101,11 +101,11 @@ public class UserApiKey implements Serializable {
|
||||||
this.apiKey = apiKey;
|
this.apiKey = apiKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getExpiredAt() {
|
public long getExpiredAt() {
|
||||||
return expiredAt;
|
return expiredAt;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setExpiredAt(String expiredAt) {
|
public void setExpiredAt(long expiredAt) {
|
||||||
this.expiredAt = expiredAt;
|
this.expiredAt = expiredAt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -75,7 +75,7 @@ public class UserApiKeyController {
|
||||||
userApiKey.setApp(app);
|
userApiKey.setApp(app);
|
||||||
userApiKey.setApiKey(null);
|
userApiKey.setApiKey(null);
|
||||||
userApiKey.setRemark(remark);
|
userApiKey.setRemark(remark);
|
||||||
userApiKey.setExpiredAt(expiresAt);
|
userApiKey.setExpiredAt(expirationTime != null ? expirationTime : 0);
|
||||||
userApiKey.setEnable(enable != null ? enable : false);
|
userApiKey.setEnable(enable != null ? enable : false);
|
||||||
userApiKey.setCreateTime(DateUtil.getNow());
|
userApiKey.setCreateTime(DateUtil.getNow());
|
||||||
userApiKey.setUpdateTime(DateUtil.getNow());
|
userApiKey.setUpdateTime(DateUtil.getNow());
|
||||||
|
@ -182,8 +182,8 @@ public class UserApiKeyController {
|
||||||
throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户不存在");
|
throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户不存在");
|
||||||
}
|
}
|
||||||
Long expirationTime = null;
|
Long expirationTime = null;
|
||||||
if (userApiKey.getExpiredAt() != null) {
|
if (userApiKey.getExpiredAt() > 0) {
|
||||||
long timestamp = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestampMs(userApiKey.getExpiredAt());
|
long timestamp = userApiKey.getExpiredAt();
|
||||||
expirationTime = (timestamp - System.currentTimeMillis()) / (60 * 1000);
|
expirationTime = (timestamp - System.currentTimeMillis()) / (60 * 1000);
|
||||||
if (expirationTime < 0) {
|
if (expirationTime < 0) {
|
||||||
throw new ControllerException(ErrorCode.ERROR400.getCode(), "ApiKey已失效");
|
throw new ControllerException(ErrorCode.ERROR400.getCode(), "ApiKey已失效");
|
||||||
|
|
Loading…
Reference in New Issue