Compare commits

..

2 Commits

Author SHA1 Message Date
1iyc e471b56281 ather): 重构数据源配置功能
- 移除 DataSourceConfig 中的 beanName 字段,使用 name 字段替代
- 新增 DataSourceConfigDTO 类作为数据源配置的传输对象
- 实现 DataSourceConfigConvert 接口,用于 DTO 和 DruidDataSource 之间的转换
- 更新 DataSourceConfigService 接口,使用 DTO 作为参数- 重构 DataSourceConfigServiceImpl 中的 add 方法,使用 DTO 和转换器
- 更新应用配置,移除全局的 driverClassName 配置- 新增 SQL Server 数据源支持
1 week ago
1iyc 4bd634fa63 动态新增数据源成功 1 week ago

@ -31,6 +31,8 @@
<poi.version>4.1.2</poi.version>
<velocity.version>2.3</velocity.version>
<jwt.version>0.9.1</jwt.version>
<mapstruct.version>1.6.2</mapstruct.version>
<lombok.version>1.18.22</lombok.version>
</properties>
<!-- 依赖声明 -->
@ -196,6 +198,13 @@
<version>${sync.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
@ -216,11 +225,25 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<parameters>true</parameters>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>

@ -43,6 +43,11 @@
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
<!-- 核心模块-->
<dependency>
<groupId>com.lyr</groupId>
@ -67,6 +72,7 @@
<artifactId>sync-gather</artifactId>
</dependency>
</dependencies>

@ -1,11 +1,7 @@
package com.lyr.web.controller.tool;
import com.lyr.framework.manager.DataSourceManagerService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author liyc
* @date 2024/10/30

@ -5,13 +5,12 @@ spring:
port: 6379
database: 1
password: eGDiN3RRhRNzrzjd
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
druid:
# 主库数据源
master:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${spring.redis.host}:3306/sync-data?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: K2bfP8rzG62ZSJGN
@ -19,6 +18,7 @@ spring:
slave:
# 从数据源开关/默认关闭
enabled: false
driverClassName:
url:
username:
password:

@ -5,13 +5,12 @@ spring:
port: 6379
database: 1
password: eGDiN3RRhRNzrzjd
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
druid:
# 主库数据源
master:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${spring.redis.host}:3306/sync-data?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: K2bfP8rzG62ZSJGN
@ -19,6 +18,7 @@ spring:
slave:
# 从数据源开关/默认关闭
enabled: false
driverClassName:
url:
username:
password:

@ -114,6 +114,24 @@
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.2.1-jre</version>
</dependency>
</dependencies>
</project>

@ -16,8 +16,18 @@ import java.lang.annotation.*;
@Documented
@Inherited
public @interface DataSource {
/**
*
*/
DataSourceType value() default DataSourceType.MASTER;
/**
* (使,使value)
*
* @return
*/
String name() default "";
}

@ -0,0 +1,31 @@
package com.lyr.common.core.domain;
import com.google.common.collect.ImmutableMap;
import com.lyr.common.constant.HttpStatus;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.HashMap;
import java.util.Map;
/**
*
*
* @author liyc
*/
@NoArgsConstructor
@AllArgsConstructor
@Data
public class ServiceResult extends HashMap<String, Object> {
private Integer code;
private String message;
private Map data;
public static ServiceResult success(String name) {
ImmutableMap<Object, Object> data1 = ImmutableMap.of();
return new ServiceResult(HttpStatus.SUCCESS, name, data1);
}
}

@ -14,5 +14,11 @@ public enum DataSourceType {
/**
*
*/
SLAVE
SLAVE,
/**
*
*/
EXTERNAL
}

@ -0,0 +1,104 @@
package com.lyr.common.utils;
import com.lyr.common.exception.ServiceException;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.util.CollectionUtils;
import java.util.Collection;
import java.util.List;
/**
* @author Admin
*/
public class AssertUtil {
public static void notEmpty(Long val, String s, Integer... codes) throws ServiceException {
if (val == null) {
Integer code = (codes.length == 0) ? 500 : codes[0];
throw new ServiceException(s, code);
}
}
public static void notEmpty(Collection c, String s, Integer... codes) throws ServiceException {
if (CollectionUtils.isEmpty(c)) {
Integer code = (codes.length == 0) ? 500 : codes[0];
throw new ServiceException(s, code);
}
}
public static void notEmpty(String val, String s, Integer... codes) throws ServiceException {
if (StringUtils.isEmpty(val)) {
Integer code = (codes.length == 0) ? 500 : codes[0];
throw new ServiceException(s, code);
}
}
public static void notEmpty(Object val, String s, Integer... codes) throws ServiceException {
if (val == null) {
Integer code = (codes.length == 0) ? 500 : codes[0];
throw new ServiceException(s, code);
}
}
/**
*
*
* @param obj
* @param s
*/
public static void empty(Object obj, String s, Integer... codes) {
if (null != obj) {
Integer code = (codes.length == 0) ? 500 : codes[0];
throw new ServiceException(s, code);
}
}
public static void empty(Object obj, String s, List<T> t, Integer... codes) {
if (null != obj) {
Integer code = (codes.length == 0) ? 500 : codes[0];
throw new ServiceException(s, code);
}
}
public static void empty(Collection c, String s) {
if (!CollectionUtils.isEmpty(c)) {
throw new ServiceException(s, 500);
}
}
public static void notEqual(boolean val, String s, Integer... codes) {
if (val) {
Integer code = (codes.length == 0) ? 500 : codes[0];
throw new ServiceException(s, code);
}
}
public static void equal(boolean val, String s) {
notEqual(!val, s);
}
public static void isFalse(boolean b, String s, Integer... codes) {
if (b) {
Integer code = (codes.length == 0) ? 500 : codes[0];
throw new ServiceException(s, code);
}
}
public static void isTrue(boolean b, String s, Integer... codes) throws ServiceException {
if (!b) {
Integer code = (codes.length == 0) ? 500 : codes[0];
throw new ServiceException(s, code);
}
}
public static void test() {
throw new ServiceException("222");
}
}

@ -0,0 +1,49 @@
package com.lyr.common.utils;
import org.springframework.context.annotation.Bean;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
/**
* spel
*
* @author liyc
* @date 2024/10/30
* @description TODO
**/
@Component
public class SpElUtil {
@Resource
public ExpressionParser expressionParser;
@Bean
public ExpressionParser expressionParser() {
return new SpelExpressionParser();
}
/**
*
*
* @param method
* @param args
* @param expression
* @return
*/
public String parseExpression(Method method, Object[] args, String expression) {
StandardEvaluationContext context = new StandardEvaluationContext();
Parameter[] parameters = method.getParameters();
for (int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
context.setVariable(parameter.getName(), args[i]);
}
return expressionParser.parseExpression(expression).getValue(context, String.class);
}
}

@ -36,6 +36,15 @@ public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationC
return (T) beanFactory.getBean(name);
}
public static <T> T getBeanOfNull(String name) throws BeansException {
try {
return (T) beanFactory.getBean(name);
} catch (BeansException e) {
}
return null;
}
/**
* requiredType
*

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save