集成MP + 数据源注入
parent
2c844c6df5
commit
58a14e31c2
@ -0,0 +1,19 @@
|
|||||||
|
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
|
||||||
|
* @description TODO
|
||||||
|
**/
|
||||||
|
@RestController("/dataSourceManager")
|
||||||
|
public class DataSourceManagerController {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,32 +0,0 @@
|
|||||||
package com.lyr.common.utils;
|
|
||||||
|
|
||||||
import com.github.pagehelper.PageHelper;
|
|
||||||
import com.lyr.common.core.page.PageDomain;
|
|
||||||
import com.lyr.common.core.page.TableSupport;
|
|
||||||
import com.lyr.common.utils.sql.SqlUtil;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 分页工具类
|
|
||||||
*
|
|
||||||
* @author ruoyi
|
|
||||||
*/
|
|
||||||
public class PageUtils extends PageHelper {
|
|
||||||
/**
|
|
||||||
* 设置请求分页数据
|
|
||||||
*/
|
|
||||||
public static void startPage() {
|
|
||||||
PageDomain pageDomain = TableSupport.buildPageRequest();
|
|
||||||
Integer pageNum = pageDomain.getPageNum();
|
|
||||||
Integer pageSize = pageDomain.getPageSize();
|
|
||||||
String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
|
|
||||||
Boolean reasonable = pageDomain.getReasonable();
|
|
||||||
PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 清理分页的线程变量
|
|
||||||
*/
|
|
||||||
public static void clearPage() {
|
|
||||||
PageHelper.clearPage();
|
|
||||||
}
|
|
||||||
}
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,57 @@
|
|||||||
|
package com.lyr.framework.manager;
|
||||||
|
|
||||||
|
import com.lyr.framework.datasource.DynamicDataSource;
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author liyc
|
||||||
|
* @date 2024/10/30
|
||||||
|
* @description TODO
|
||||||
|
**/
|
||||||
|
@Component
|
||||||
|
public class DataSourceManagerService implements ApplicationContextAware {
|
||||||
|
|
||||||
|
private ApplicationContext applicationContext;
|
||||||
|
@Resource
|
||||||
|
private DynamicDataSource dynamicDataSource;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void print() {
|
||||||
|
dynamicDataSource.print();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void registerDataSources(String beanName, DataSource dataSource) {
|
||||||
|
DataSource resolvedDefaultDataSource = dynamicDataSource.getResolvedDefaultDataSource();
|
||||||
|
dynamicDataSource.setDefaultTargetDataSource(resolvedDefaultDataSource);
|
||||||
|
dynamicDataSource.addTargetDataSource(beanName, dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add() {
|
||||||
|
/* // 构建数据源
|
||||||
|
DruidDataSource dataSource = new DruidDataSource();
|
||||||
|
dataSource.setUrl("jdbc:sqlserver://192.168.3.28:1433;DatabaseName=BiDataDeYue;encrypt=true;trustServerCertificate=true;");
|
||||||
|
dataSource.setUsername("sa");
|
||||||
|
dataSource.setPassword("mssql_whdZeB");
|
||||||
|
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
|
||||||
|
try {
|
||||||
|
DataSource resolvedDefaultDataSource = dynamicDataSource.getResolvedDefaultDataSource();
|
||||||
|
dynamicDataSource.setDefaultTargetDataSource(resolvedDefaultDataSource);
|
||||||
|
dynamicDataSource.addTargetDataSource("deYueMssql", dataSource);
|
||||||
|
} catch (BeansException e) {
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,40 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<groupId>com.lyr</groupId>
|
||||||
|
<artifactId>sync</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>sync-gather</artifactId>
|
||||||
|
|
||||||
|
<description>
|
||||||
|
数据采集模块
|
||||||
|
</description>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- 通用工具-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.lyr</groupId>
|
||||||
|
<artifactId>sync-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- 框架 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.lyr</groupId>
|
||||||
|
<artifactId>sync-framework</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.lyr.gather.controller;
|
||||||
|
|
||||||
|
import com.lyr.gather.service.DataSourceConfigService;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author liyc
|
||||||
|
* @date 2024/10/30
|
||||||
|
* @description TODO
|
||||||
|
**/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/gather")
|
||||||
|
public class DataSourceConfigController {
|
||||||
|
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private DataSourceConfigService dataSourceConfigService;
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/print")
|
||||||
|
public void print() {
|
||||||
|
dataSourceConfigService.print();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/add")
|
||||||
|
public void add() {
|
||||||
|
dataSourceConfigService.add();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.lyr.gather.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.lyr.gather.domain.vo.DataSourceConfig;
|
||||||
|
|
||||||
|
public interface DataSourceConfigMapper extends BaseMapper<DataSourceConfig> {
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.lyr.gather.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.lyr.gather.domain.vo.DataSourceConfig;
|
||||||
|
|
||||||
|
public interface DataSourceConfigService extends IService<DataSourceConfig> {
|
||||||
|
|
||||||
|
void print();
|
||||||
|
|
||||||
|
void add();
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
package com.lyr.gather.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.lyr.framework.manager.DataSourceManagerService;
|
||||||
|
import com.lyr.gather.domain.vo.DataSourceConfig;
|
||||||
|
import com.lyr.gather.mapper.DataSourceConfigMapper;
|
||||||
|
import com.lyr.gather.service.DataSourceConfigService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class DataSourceConfigServiceImpl extends ServiceImpl<DataSourceConfigMapper, DataSourceConfig> implements DataSourceConfigService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private DataSourceManagerService dataSourceManagerService;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
private void registered() {
|
||||||
|
List<DataSourceConfig> configList = this.lambdaQuery().eq(DataSourceConfig::getIsEnabled, 1).list();
|
||||||
|
configList.stream().forEach(e -> {
|
||||||
|
DruidDataSource dataSource = new DruidDataSource();
|
||||||
|
dataSource.setDriverClassName(e.getDriverClassName());
|
||||||
|
dataSource.setUrl(e.getUrl());
|
||||||
|
dataSource.setUsername(e.getUsername());
|
||||||
|
dataSource.setPassword(e.getPassword());
|
||||||
|
dataSourceManagerService.registerDataSources(e.getBeanName(), dataSource);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void print() {
|
||||||
|
dataSourceManagerService.print();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add() {
|
||||||
|
DruidDataSource dataSource = new DruidDataSource();
|
||||||
|
dataSource.setUrl("jdbc:sqlserver://192.168.3.28:1433;DatabaseName=BiDataDeYue;encrypt=true;trustServerCertificate=true;");
|
||||||
|
dataSource.setUsername("sa");
|
||||||
|
dataSource.setPassword("mssql_whdZeB");
|
||||||
|
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
|
||||||
|
dataSourceManagerService.registerDataSources("biDataDeYue", dataSource);
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue