Skip to content

Commit

Permalink
Merge pull request #215 from BingChunMoLi/develop
Browse files Browse the repository at this point in the history
配置文件迁移数据库
  • Loading branch information
BingChunMoLi authored Mar 12, 2024
2 parents db66fb5 + 4bbdf97 commit c340e39
Show file tree
Hide file tree
Showing 23 changed files with 362 additions and 138 deletions.
26 changes: 2 additions & 24 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
<version>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand All @@ -17,8 +17,6 @@

<properties>
<java.version>17</java.version>
<mybatis.version>3.5.15</mybatis.version>
<mybatis-spring.version>3.0.3</mybatis-spring.version>
<mybatis-plus.version>3.5.5</mybatis-plus.version>
<docker-plugin.version>1.4.13</docker-plugin.version>
<hutool.version>5.8.26</hutool.version>
Expand All @@ -28,7 +26,7 @@
<ip-info.version>2.2.0</ip-info.version>
<knife4j.version>4.5.0</knife4j.version>
<tencent-cloud-api.version>3.1.954</tencent-cloud-api.version>
<kotlin.version>1.9.22</kotlin.version>
<kotlin.version>1.9.23</kotlin.version>
<firebase-admin.version>9.2.0</firebase-admin.version>
</properties>

Expand Down Expand Up @@ -61,26 +59,6 @@
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>${mybatis-plus.version}</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/bingchunmoli/api/config/ApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

/**
* ApiConfig
* @author bingchunmoli
Expand Down Expand Up @@ -69,7 +67,7 @@ public class ApiConfig {
/**
* 定时任务歌单id
*/
private List<String> playListId;
private String playListId;

/**
* 歌单用的cookies,
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/com/bingchunmoli/api/config/SystemController.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package com.bingchunmoli.api.config;

import com.bingchunmoli.api.bean.ResultVO;
import com.bingchunmoli.api.config.service.ConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/system")
public class SystemController {
private final ApiConfig apiConfig;

private final ConfigService configService;
/**
* 查询当前的配置项
*
Expand All @@ -34,6 +31,7 @@ public ResultVO<ApiConfig> systemConfig() {
*/
@PostMapping
public ResultVO<Boolean> updateSystemConfig(@RequestBody ApiConfig apiConfig) {
return ResultVO.ok(null);
configService.updateApiConfig(apiConfig);
return ResultVO.ok(true);
}
}
12 changes: 4 additions & 8 deletions src/main/java/com/bingchunmoli/api/config/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@

import com.bingchunmoli.api.bean.ResultVO;
import com.bingchunmoli.api.config.bean.User;
import com.bingchunmoli.api.config.service.UserService;
import com.bingchunmoli.api.exception.system.ApiSystemException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
private final UserService userService;
Expand All @@ -36,7 +32,7 @@ public ResultVO<Boolean> isInit() {
return ResultVO.ok(userService.count() == 0L);
}

@PutMapping
@PutMapping("/register")
public ResultVO<Boolean> register(@Valid @RequestBody User user) {
if (userService.register(user)) {
request.getSession().setAttribute("user", user.getId());
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/bingchunmoli/api/config/bean/Config.java
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 src/main/java/com/bingchunmoli/api/config/mapper/ConfigMapper.java
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> {

}




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);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bingchunmoli.api.config;
package com.bingchunmoli.api.config.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.bingchunmoli.api.config.bean.User;
Expand Down
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);
}

}
}
}




Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.bingchunmoli.api.config;
package com.bingchunmoli.api.config.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bingchunmoli.api.config.bean.User;
import com.bingchunmoli.api.config.mapper.UserMapper;
import com.bingchunmoli.api.config.service.UserService;
import com.bingchunmoli.api.exception.system.ApiSystemException;
import com.bingchunmoli.api.exception.system.ApiUserNonFoundException;
import lombok.RequiredArgsConstructor;
Expand All @@ -28,12 +29,16 @@ public Boolean login(User user) {
Optional<User> optional = getOneOpt(Wrappers.lambdaQuery(new User()).eq(User::getName, user.getName()));
optional.orElseThrow(() -> new ApiUserNonFoundException("non user"));
String password = optional.map(User::getPassword).orElseThrow(() -> new ApiSystemException("non password"));
return passwordEncoder.matches(user.getPassword(), password);
boolean matche = passwordEncoder.matches(user.getPassword(), password);
if (matche) {
user.setId(optional.map(User::getId).orElseThrow());
}
return matche;
}

@Override
public synchronized boolean register(User user) {
if (count() > 1) {
if (count() > 0) {
throw new ApiSystemException("错误的初始化用户");
}
user.setPassword(passwordEncoder.encode(user.getPassword()));
Expand Down
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;
}
}
Loading

0 comments on commit c340e39

Please sign in to comment.