-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #215 from BingChunMoLi/develop
配置文件迁移数据库
- Loading branch information
Showing
23 changed files
with
362 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/bingchunmoli/api/config/bean/Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.bingchunmoli.api.config.bean; | ||
|
||
import com.baomidou.mybatisplus.annotation.*; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
import java.io.Serializable; | ||
import java.time.LocalDateTime; | ||
|
||
/** | ||
* | ||
*/ | ||
@Data | ||
@Builder | ||
@TableName(value = "`config`") | ||
public class Config implements Serializable { | ||
@TableField(exist = false) | ||
private static final long serialVersionUID = 1L; | ||
/** | ||
* | ||
*/ | ||
@TableId(value = "id", type = IdType.AUTO) | ||
private Integer id; | ||
/** | ||
* 配置的key | ||
*/ | ||
@TableField(value = "`key`") | ||
private String key; | ||
/** | ||
* 配置的值 | ||
*/ | ||
@TableField(value = "`value`") | ||
private String value; | ||
/** | ||
* 创建时间 | ||
*/ | ||
@TableField(value = "create_time", fill = FieldFill.INSERT) | ||
private LocalDateTime createTime; | ||
/** | ||
* 更新时间 | ||
*/ | ||
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE) | ||
private LocalDateTime updateTime; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/bingchunmoli/api/config/mapper/ConfigMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.bingchunmoli.api.config.mapper; | ||
|
||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; | ||
import com.bingchunmoli.api.config.bean.Config; | ||
|
||
/** | ||
* @author MoLi | ||
*/ | ||
public interface ConfigMapper extends BaseMapper<Config> { | ||
|
||
} | ||
|
||
|
||
|
||
|
13 changes: 13 additions & 0 deletions
13
src/main/java/com/bingchunmoli/api/config/service/ConfigService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.bingchunmoli.api.config.service; | ||
|
||
import com.baomidou.mybatisplus.extension.service.IService; | ||
import com.bingchunmoli.api.config.ApiConfig; | ||
import com.bingchunmoli.api.config.bean.Config; | ||
|
||
/** | ||
* @author MoLi | ||
*/ | ||
public interface ConfigService extends IService<Config> { | ||
|
||
void updateApiConfig(ApiConfig apiConfig); | ||
} |
2 changes: 1 addition & 1 deletion
2
.../bingchunmoli/api/config/UserService.java → ...nmoli/api/config/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/com/bingchunmoli/api/config/service/impl/ConfigServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.bingchunmoli.api.config.service.impl; | ||
|
||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | ||
import com.bingchunmoli.api.config.ApiConfig; | ||
import com.bingchunmoli.api.config.bean.Config; | ||
import com.bingchunmoli.api.config.mapper.ConfigMapper; | ||
import com.bingchunmoli.api.config.service.ConfigService; | ||
import com.bingchunmoli.api.exception.ApiException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.time.LocalDateTime; | ||
import java.util.Locale; | ||
import java.util.Objects; | ||
|
||
/** | ||
* @author MoLi | ||
*/ | ||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements ConfigService { | ||
private final ApiConfig apiConfig; | ||
|
||
@Override | ||
public void updateApiConfig(ApiConfig apiConfig) { | ||
Class<? extends ApiConfig> apiConfigClass = apiConfig.getClass(); | ||
for (Field field : apiConfigClass.getDeclaredFields()) { | ||
// field.setAccessible(true); | ||
String fieldName = field.getName(); | ||
String upperCaseFieldName = fieldName.substring(0, 1).toUpperCase(Locale.ROOT) + fieldName.substring(1); | ||
try { | ||
Method getMethod = apiConfigClass.getMethod("get" + upperCaseFieldName); | ||
Object paramValue = getMethod.invoke(apiConfig); | ||
Object systemValue = getMethod.invoke(this.apiConfig); | ||
if (!Objects.equals(paramValue, systemValue)) { | ||
Method setMethod = apiConfigClass.getMethod("set" + upperCaseFieldName, String.class); | ||
setMethod.invoke(this.apiConfig, paramValue); | ||
Config config = getOneOpt(new LambdaQueryWrapper<Config>().eq(Config::getKey, fieldName)) | ||
.orElse(Config.builder() | ||
.key(fieldName) | ||
.value((String) paramValue) | ||
.updateTime(LocalDateTime.now()) | ||
.build()); | ||
saveOrUpdate(config); | ||
} | ||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { | ||
throw new ApiException(e); | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/com/bingchunmoli/api/init/impl/InitConfigServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.bingchunmoli.api.init.impl; | ||
|
||
import com.bingchunmoli.api.config.ApiConfig; | ||
import com.bingchunmoli.api.config.bean.Config; | ||
import com.bingchunmoli.api.config.service.ConfigService; | ||
import com.bingchunmoli.api.init.InitService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
/** | ||
* @author moli | ||
*/ | ||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class InitConfigServiceImpl implements InitService { | ||
private final ConfigService configService; | ||
private final ApiConfig apiConfig; | ||
|
||
@Override | ||
public void init() { | ||
long count = configService.count(); | ||
if (count == 0) { | ||
doInit(); | ||
} | ||
} | ||
|
||
private void doInit() { | ||
Class<? extends ApiConfig> apiConfigClass = apiConfig.getClass(); | ||
List<Config> list = new ArrayList<>(15); | ||
for (Field field : apiConfigClass.getFields()) { | ||
String fieldName = field.getName(); | ||
String fieldNameUpperCase = fieldName.substring(0, 1).toUpperCase(Locale.ROOT) + fieldName.substring(1); | ||
try { | ||
Method method = apiConfigClass.getMethod("get" + fieldNameUpperCase); | ||
String value = (String) method.invoke(apiConfig); | ||
list.add(Config.builder() | ||
.key(fieldName) | ||
.value(value) | ||
.updateTime(LocalDateTime.now()) | ||
.build()); | ||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
configService.saveBatch(list); | ||
} | ||
|
||
@Override | ||
public Integer getOrder() { | ||
return 7; | ||
} | ||
} |
Oops, something went wrong.