test:(代码进行了从构分开来写了)
parent
ae0974554a
commit
2ca7afc0a7
|
@ -1,23 +0,0 @@
|
|||
package com.muyu;
|
||||
|
||||
import com.muyu.common.security.annotation.EnableCustomConfig;
|
||||
import com.muyu.common.security.annotation.EnableMyFeignClients;
|
||||
import com.muyu.common.swagger.annotation.EnableCustomSwagger2;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @ClassDescription:
|
||||
* @JdkVersion: 17
|
||||
* @Author: zhangxu
|
||||
* @Created: 2024/6/3 21:00
|
||||
*/
|
||||
@EnableCustomConfig
|
||||
@EnableCustomSwagger2
|
||||
@EnableMyFeignClients
|
||||
@SpringBootApplication
|
||||
public class CloudManyDataSourceApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CloudManyDataSourceApplication.class);
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 9205
|
||||
|
||||
# Spring
|
||||
spring:
|
||||
application:
|
||||
# 应用名称
|
||||
name: muyu-many-datasource
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 115.159.67.205:8848
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 115.159.67.205:8848
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
shared-configs:
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
logging:
|
||||
level:
|
||||
com.muyu.mapper: DEBUG
|
|
@ -3,6 +3,7 @@ package com.muyu.domain.datasources;
|
|||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.muyu.common.core.web.domain.BaseEntity;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
@ -18,28 +19,31 @@ import lombok.experimental.SuperBuilder;
|
|||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Datasource {
|
||||
public class Datasource extends BaseEntity {
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
*编号id
|
||||
* */
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
/**
|
||||
*数据库ip
|
||||
*/
|
||||
* */
|
||||
private String ip;
|
||||
/**
|
||||
*数据库
|
||||
*/
|
||||
@TableField("`database`")
|
||||
* */
|
||||
@TableField("`datasource`")
|
||||
private String database;
|
||||
/**
|
||||
*用户名
|
||||
*/
|
||||
* */
|
||||
private String username;
|
||||
/**
|
||||
*密码
|
||||
*/
|
||||
* */
|
||||
private String password;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package com.muyu.config.contents;
|
||||
|
||||
import com.muyu.common.security.utils.SecurityUtils;
|
||||
import com.muyu.common.system.remote.RemoteUserService;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
|
||||
/**
|
||||
* @ClassDescription:
|
||||
* @JdkVersion: 17
|
||||
* @Author: zhangxu
|
||||
* @Created: 2024/6/6 21:13
|
||||
*/
|
||||
public class DataSourceAsp {
|
||||
|
||||
@Pointcut("execution(public * com.zhiLian.vehicle.controller.*Controller.*(..))")
|
||||
public void pointcut() {
|
||||
}
|
||||
@Lazy
|
||||
@Autowired
|
||||
private RemoteUserService remoteUserService;
|
||||
|
||||
|
||||
@Before("pointcut()")
|
||||
public void beforeMethod(){
|
||||
Long userid = SecurityUtils.getLoginUser().getUserid();
|
||||
remoteUserService.
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -3,6 +3,7 @@ package com.muyu.networking;
|
|||
import com.muyu.common.security.annotation.EnableCustomConfig;
|
||||
import com.muyu.common.security.annotation.EnableMyFeignClients;
|
||||
import com.muyu.common.swagger.annotation.EnableCustomSwagger2;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
@ -17,6 +18,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
|
|||
@EnableCustomSwagger2
|
||||
@EnableMyFeignClients
|
||||
@SpringBootApplication
|
||||
@MapperScan("com.muyu.networking.mapper")
|
||||
public class NetworkingApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(NetworkingApplication.class, args);
|
||||
|
|
|
@ -40,7 +40,7 @@ public class EnterpriseController extends BaseController
|
|||
/**
|
||||
* 查询【请填写功能名称】列表
|
||||
*/
|
||||
@RequiresPermissions("system:enterprise:list")
|
||||
|
||||
@GetMapping("/list")
|
||||
public Result<TableDataInfo<Enterprise>> list(Enterprise enterprise)
|
||||
{
|
||||
|
@ -52,7 +52,7 @@ public class EnterpriseController extends BaseController
|
|||
/**
|
||||
* 导出【请填写功能名称】列表
|
||||
*/
|
||||
@RequiresPermissions("system:enterprise:export")
|
||||
|
||||
@Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
|
||||
@PostMapping("/export")
|
||||
public void export(HttpServletResponse response, Enterprise enterprise)
|
||||
|
@ -65,7 +65,7 @@ public class EnterpriseController extends BaseController
|
|||
/**
|
||||
* 获取【请填写功能名称】详细信息
|
||||
*/
|
||||
@RequiresPermissions("system:enterprise:query")
|
||||
|
||||
@GetMapping(value = "/{id}")
|
||||
public Result getInfo(@PathVariable("id") Long id)
|
||||
{
|
||||
|
@ -75,7 +75,7 @@ public class EnterpriseController extends BaseController
|
|||
/**
|
||||
* 新增【请填写功能名称】
|
||||
*/
|
||||
@RequiresPermissions("system:enterprise:add")
|
||||
|
||||
@Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
|
||||
@PostMapping
|
||||
public Result add(@RequestBody Enterprise enterprise)
|
||||
|
@ -86,7 +86,7 @@ public class EnterpriseController extends BaseController
|
|||
/**
|
||||
* 修改【请填写功能名称】
|
||||
*/
|
||||
@RequiresPermissions("system:enterprise:edit")
|
||||
|
||||
@Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
|
||||
@PutMapping
|
||||
public Result edit(@RequestBody Enterprise enterprise)
|
||||
|
@ -99,7 +99,7 @@ public class EnterpriseController extends BaseController
|
|||
/**
|
||||
* 删除【请填写功能名称】
|
||||
*/
|
||||
@RequiresPermissions("system:enterprise:remove")
|
||||
|
||||
@Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
|
||||
@DeleteMapping("/{ids}")
|
||||
public Result remove(@PathVariable Long[] ids)
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.muyu.networking.mapper;
|
|||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.muyu.domain.Enterprise;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -11,6 +12,7 @@ import java.util.List;
|
|||
* @author ruoyi
|
||||
* @date 2024-05-25
|
||||
*/
|
||||
|
||||
public interface EnterpriseMapper extends BaseMapper<Enterprise>
|
||||
{
|
||||
/**
|
||||
|
|
|
@ -1,57 +1,57 @@
|
|||
package com.muyu.networking.service.impl;
|
||||
|
||||
|
||||
import com.muyu.domain.datasources.Datasource;
|
||||
import com.muyu.networking.config.DynamicRoutingDataSource;
|
||||
import com.muyu.networking.context.DataSourceContextHolder;
|
||||
import com.muyu.networking.mapper.DataSourceMapper;
|
||||
import com.muyu.networking.service.DataSourceService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @ClassName
|
||||
* @Author
|
||||
* @Date
|
||||
*/
|
||||
@Service
|
||||
public class DataSourceServiceImpl implements DataSourceService {
|
||||
|
||||
@Autowired
|
||||
private DataSourceMapper dataSourceMapper;
|
||||
|
||||
@Autowired
|
||||
private DynamicRoutingDataSource dynamicRoutingDataSource;
|
||||
|
||||
@Override
|
||||
public void toDefaultDS() {
|
||||
//切到默认数据源
|
||||
DataSourceContextHolder.removeDataSource(); // 删除数据源
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean changeDS(Long datasourceId) {
|
||||
//切到默认数据源
|
||||
DataSourceContextHolder.removeDataSource(); // 删除数据源
|
||||
//找到所有的配置
|
||||
List<Datasource> dataSourceList = dataSourceMapper.selectList(null); // 数据源对象
|
||||
|
||||
if(!dataSourceList.isEmpty()){
|
||||
for (Datasource dataSource : dataSourceList) { // 数据源对象
|
||||
if(dataSource.getId().equals(datasourceId)){ // 编号
|
||||
System.out.println("已找到数据源,datasourceId是:" + dataSource.getId()); // 编号
|
||||
//判断连接是否存在,不存在就创建
|
||||
dynamicRoutingDataSource.checkCreateDataSource(dataSource); // 检查数据源是否已经创建
|
||||
//切换数据源
|
||||
DataSourceContextHolder.setDataSource(dataSource.getId()); // 编号 --> 切换数据源
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//package com.muyu.networking.service.impl;
|
||||
//
|
||||
//
|
||||
//import com.muyu.domain.datasources.Datasource;
|
||||
//import com.muyu.networking.config.DynamicRoutingDataSource;
|
||||
//import com.muyu.networking.context.DataSourceContextHolder;
|
||||
//import com.muyu.networking.mapper.DataSourceMapper;
|
||||
//import com.muyu.networking.service.DataSourceService;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.stereotype.Service;
|
||||
//
|
||||
//import java.util.List;
|
||||
//
|
||||
///**
|
||||
// * @ClassName
|
||||
// * @Author
|
||||
// * @Date
|
||||
// */
|
||||
//@Service
|
||||
//public class DataSourceServiceImpl implements DataSourceService {
|
||||
//
|
||||
// @Autowired
|
||||
// private DataSourceMapper dataSourceMapper;
|
||||
//
|
||||
// @Autowired
|
||||
// private DynamicRoutingDataSource dynamicRoutingDataSource;
|
||||
//
|
||||
// @Override
|
||||
// public void toDefaultDS() {
|
||||
// //切到默认数据源
|
||||
// DataSourceContextHolder.removeDataSource(); // 删除数据源
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public boolean changeDS(Long datasourceId) {
|
||||
// //切到默认数据源
|
||||
// DataSourceContextHolder.removeDataSource(); // 删除数据源
|
||||
// //找到所有的配置
|
||||
// List<Datasource> dataSourceList = dataSourceMapper.selectList(null); // 数据源对象
|
||||
//
|
||||
// if(!dataSourceList.isEmpty()){
|
||||
// for (Datasource dataSource : dataSourceList) { // 数据源对象
|
||||
// if(dataSource.getId().equals(datasourceId)){ // 编号
|
||||
// System.out.println("已找到数据源,datasourceId是:" + dataSource.getId()); // 编号
|
||||
// //判断连接是否存在,不存在就创建
|
||||
// dynamicRoutingDataSource.checkCreateDataSource(dataSource); // 检查数据源是否已经创建
|
||||
// //切换数据源
|
||||
// DataSourceContextHolder.setDataSource(dataSource.getId()); // 编号 --> 切换数据源
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
//}
|
||||
//
|
||||
|
|
|
@ -7,6 +7,7 @@ package com.muyu.networking.service.impl;
|
|||
|
||||
|
||||
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
|
@ -16,6 +17,8 @@ import com.muyu.common.system.domain.SysUser;
|
|||
import com.muyu.common.system.remote.RemoteUserService;
|
||||
import com.muyu.domain.Enterprise;
|
||||
|
||||
import com.muyu.domain.datasources.Datasource;
|
||||
|
||||
import com.muyu.networking.mapper.EnterpriseMapper;
|
||||
import com.muyu.networking.service.EnterpriseService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -35,6 +38,7 @@ import java.util.UUID;
|
|||
* @date 2024-05-25
|
||||
*/
|
||||
@Service
|
||||
@DS("networking")
|
||||
public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper,Enterprise> implements EnterpriseService
|
||||
{
|
||||
@Autowired
|
||||
|
@ -99,10 +103,20 @@ public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper,Enterpr
|
|||
SysUser sysUser = SysUser.builder().userName(
|
||||
enterprise.getEnterpriseName())
|
||||
.password("admin")
|
||||
.roleId(2L)
|
||||
.entId(enterprise.getId())
|
||||
.nickName(enterprise.getEnterpriseName()).
|
||||
userType(String.valueOf(enterprise.getId()))
|
||||
.build();
|
||||
// Datasource datasource = new Datasource();
|
||||
// Datasource root = Datasource.builder()
|
||||
// .ip("115.159.67.205")
|
||||
// .password("sal75-z")
|
||||
// .database(sysUser.getUserName())
|
||||
// .username("root")
|
||||
// .build();
|
||||
// DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource();
|
||||
// dynamicRoutingDataSource.createDataSource(root);
|
||||
Result add = remoteUserService.add(sysUser);
|
||||
return enterpriseMapper.insertEnterprise(enterprise);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.muyu.networking.config;
|
||||
|
||||
package com.muyu.vehicle.config;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
|
@ -1,11 +1,12 @@
|
|||
package com.muyu.networking.config;
|
||||
package com.muyu.vehicle.config;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.alibaba.druid.stat.DruidDataSourceStatManager;
|
||||
|
||||
import com.muyu.domain.datasources.Datasource;
|
||||
import com.muyu.networking.context.DataSourceContextHolder;
|
||||
|
||||
import com.muyu.vehicle.context.DataSourceContextHolder;
|
||||
import lombok.Data;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||
|
@ -55,26 +56,34 @@ public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
|
|||
this.customDataSources = param; // 存储我们注册的数据源
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查数据源是否已经创建
|
||||
* @param dataSource 数据源
|
||||
* 检查是否已存在指定ID的数据源,如果存在则验证其连接是否有效,如果无效则重新创建数据源;如果不存在则直接创建数据源。
|
||||
* @param dataSource 需要检查或创建的数据源对象。
|
||||
*/
|
||||
public void checkCreateDataSource(Datasource dataSource){ // 数据源对象
|
||||
Long datasourceId = dataSource.getId(); // 编号
|
||||
Map<Object, Object> map = this.customDataSources; // 存储我们注册的数据源
|
||||
public void checkCreateDataSource(Datasource dataSource){
|
||||
// 获取数据源ID
|
||||
Long datasourceId = dataSource.getId();
|
||||
// 获取已注册的数据源映射
|
||||
Map<Object, Object> map = this.customDataSources;
|
||||
// 检查是否已存在对应ID的数据源
|
||||
if(map.containsKey(datasourceId)){
|
||||
// 尝试获取并验证数据源连接
|
||||
// 这里检查一下之前创建的数据源,现在是否连接正常
|
||||
DruidDataSource druidDataSource = (DruidDataSource) map.get(datasourceId);
|
||||
boolean flag = true;
|
||||
DruidPooledConnection connection = null;
|
||||
try {
|
||||
// 尝试获取数据源连接
|
||||
connection = druidDataSource.getConnection();
|
||||
} catch (SQLException throwable) {
|
||||
//抛异常了说明连接失效吗,则删除现有连接
|
||||
// 如果获取连接失败,则记录错误信息并标记需要重新创建数据源
|
||||
log.error(throwable.getMessage());
|
||||
flag = false;
|
||||
// 删除已失效的数据源
|
||||
delDataSources(datasourceId); // 删除数据源
|
||||
}finally {
|
||||
// 确保关闭连接
|
||||
//如果连接正常记得关闭
|
||||
if(null != connection){
|
||||
try {
|
||||
|
@ -84,52 +93,78 @@ public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
|
|||
}
|
||||
}
|
||||
}
|
||||
// 如果标记为需要重新创建数据源,则进行创建
|
||||
if(!flag){
|
||||
createDataSource(dataSource); // 创建数据源
|
||||
}
|
||||
}else {
|
||||
// 如果不存在对应ID的数据源,则直接创建
|
||||
createDataSource(dataSource); // 创建数据源
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建数据源
|
||||
* @param dataSource 数据源
|
||||
* 创建数据源。
|
||||
* 使用Druid数据源工厂类来配置并创建数据源实例。
|
||||
* 此方法主要用于初始化一个特定数据库配置的数据源,并将其添加到自定义数据源的映射中。
|
||||
*
|
||||
* @param dataSource 数据源配置对象,包含数据库连接所需的全部信息。
|
||||
*/
|
||||
private void createDataSource(Datasource dataSource) { // 数据源对象
|
||||
public void createDataSource(Datasource dataSource) { // 数据源对象
|
||||
try {
|
||||
// 构建数据库连接URL
|
||||
String url="jdbc:mysql://"+dataSource.getIp(); // 数据库ip
|
||||
String driverType="com.mysql.cj.jdbc.Driver";
|
||||
url+="/"+dataSource.getDatabase()+"?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC&useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true"; // 数据库
|
||||
// 加载MySQL驱动类
|
||||
Class.forName(driverType);
|
||||
// 尝试建立与数据库的连接
|
||||
Connection connection = DriverManager.getConnection(url, dataSource.getUsername(), dataSource.getPassword()); // 用户名 | 密码
|
||||
// 如果连接成功,则关闭连接;如果连接失败,则记录错误日志
|
||||
if(connection==null){
|
||||
log.error("数据源配置有错误,DataSource:{}",dataSource);
|
||||
}else{
|
||||
connection.close();
|
||||
}
|
||||
// 创建DruidDataSource实例用于配置和管理数据库连接
|
||||
DruidDataSource druidDataSource = new DruidDataSource();
|
||||
// 设置数据源名称
|
||||
druidDataSource.setName(dataSource.getId().toString()); // 编号
|
||||
// 设置驱动类名
|
||||
druidDataSource.setDriverClassName(driverType);
|
||||
// 设置数据库连接URL
|
||||
druidDataSource.setUrl(url);
|
||||
// 设置数据库用户名
|
||||
druidDataSource.setUsername(dataSource.getUsername()); // 用户名
|
||||
// 设置数据库密码
|
||||
druidDataSource.setPassword(dataSource.getPassword()); // 密码
|
||||
// 配置连接池的最大活动连接数
|
||||
druidDataSource.setMaxActive(20);
|
||||
// 配置连接池的最小空闲连接数
|
||||
druidDataSource.setMinIdle(5);
|
||||
// 设置连接的最大等待时间
|
||||
// 获取连接最大等待时间,单位毫秒
|
||||
druidDataSource.setMaxWait(6000);
|
||||
// 设置用于检查连接有效性的SQL查询
|
||||
String validationQuery = "select 1";
|
||||
// 开启在获取连接时执行有效性检查的特性
|
||||
// 申请连接时执行validationQuery检测连接是否有效,防止取到的连接不可用
|
||||
druidDataSource.setTestOnBorrow(true);
|
||||
druidDataSource.setValidationQuery(validationQuery);
|
||||
// 初始化Druid数据源
|
||||
druidDataSource.init();
|
||||
// 将创建的数据源添加到自定义数据源映射中
|
||||
this.customDataSources.put(dataSource.getId(),druidDataSource); // 编号
|
||||
// 更新父类的TargetDataSources以包含新的数据源
|
||||
// 将map赋值给父类的TargetDataSources
|
||||
setTargetDataSources(this.customDataSources); // 存储我们注册的数据源
|
||||
// 标记属性已设置,以触发数据源的初始化
|
||||
// 将TargetDataSources中的连接信息放入resolvedDataSources管理
|
||||
super.afterPropertiesSet();
|
||||
|
||||
} catch (Exception e) {
|
||||
// 记录数据源创建过程中的任何异常
|
||||
log.error("数据源创建失败",e);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.networking.context;
|
||||
package com.muyu.vehicle.context;
|
||||
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
import lombok.extern.log4j.Log4j2;
|
|
@ -0,0 +1,99 @@
|
|||
package com.muyu.vehicle.controller;
|
||||
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.common.core.utils.poi.ExcelUtil;
|
||||
import com.muyu.common.core.web.controller.BaseController;
|
||||
import com.muyu.common.core.web.page.TableDataInfo;
|
||||
import com.muyu.common.log.annotation.Log;
|
||||
import com.muyu.common.log.enums.BusinessType;
|
||||
import com.muyu.domain.Information;
|
||||
import com.muyu.vehicle.service.IInformationService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* 【请填写功能名称】Controller
|
||||
*
|
||||
* @author ruoyi
|
||||
* @date 2024-05-27
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/information")
|
||||
public class InformationController extends BaseController
|
||||
{
|
||||
@Autowired
|
||||
private IInformationService informationService;
|
||||
|
||||
/**
|
||||
* 查询【请填写功能名称】列表
|
||||
*/
|
||||
|
||||
@GetMapping("/list")
|
||||
public Result<TableDataInfo<Information>> list(Information information)
|
||||
{
|
||||
startPage();
|
||||
List<Information> list = informationService.selectInformationList(information);
|
||||
return getDataTable(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出【请填写功能名称】列表
|
||||
*/
|
||||
|
||||
@Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
|
||||
@PostMapping("/export")
|
||||
public void export(HttpServletResponse response, Information information)
|
||||
{
|
||||
List<Information> list = informationService.selectInformationList(information);
|
||||
ExcelUtil<Information> util = new ExcelUtil<Information>(Information.class);
|
||||
util.exportExcel(response, list, "【请填写功能名称】数据");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取【请填写功能名称】详细信息
|
||||
*/
|
||||
|
||||
@GetMapping(value = "/{id}")
|
||||
public Result getInfo(@PathVariable("id") Long id)
|
||||
{
|
||||
return success(informationService.selectInformationById(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增【请填写功能名称】
|
||||
*/
|
||||
|
||||
@Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
|
||||
@PostMapping
|
||||
public Result add(@RequestBody Information information)
|
||||
{
|
||||
return toAjax(informationService.insertInformation(information));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改【请填写功能名称】
|
||||
*/
|
||||
|
||||
@Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
|
||||
@PutMapping
|
||||
public Result edit(@RequestBody Information information)
|
||||
{
|
||||
return toAjax(informationService.updateInformation(information));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除【请填写功能名称】
|
||||
*/
|
||||
|
||||
@Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
|
||||
@DeleteMapping("/{ids}")
|
||||
public Result remove(@PathVariable Long[] ids)
|
||||
{
|
||||
return toAjax(informationService.deleteInformationByIds(ids));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.muyu.vehicle.mapper;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.muyu.domain.datasources.Datasource;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @ClassName DataSourceMapper
|
||||
* @Author
|
||||
* @Date 2024/6/3 20:53
|
||||
*/
|
||||
@Mapper
|
||||
public interface DataSourceMapper extends BaseMapper<Datasource> { // 数据源对象
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
package com.muyu.vehicle.mapper;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.muyu.domain.Information;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 【请填写功能名称】Mapper接口
|
||||
*
|
||||
* @author ruoyi
|
||||
* @date 2024-05-27
|
||||
*/
|
||||
public interface InformationMapper extends BaseMapper<Information>
|
||||
{
|
||||
/**
|
||||
* 查询【请填写功能名称】
|
||||
*
|
||||
* @param id 【请填写功能名称】主键
|
||||
* @return 【请填写功能名称】
|
||||
*/
|
||||
public Information selectInformationById(Long id);
|
||||
|
||||
/**
|
||||
* 查询【请填写功能名称】列表
|
||||
*
|
||||
* @param information 【请填写功能名称】
|
||||
* @return 【请填写功能名称】集合
|
||||
*/
|
||||
public List<Information> selectInformationList(Information information);
|
||||
|
||||
/**
|
||||
* 新增【请填写功能名称】
|
||||
*
|
||||
* @param information 【请填写功能名称】
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertInformation(Information information);
|
||||
|
||||
/**
|
||||
* 修改【请填写功能名称】
|
||||
*
|
||||
* @param information 【请填写功能名称】
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateInformation(Information information);
|
||||
|
||||
/**
|
||||
* 删除【请填写功能名称】
|
||||
*
|
||||
* @param id 【请填写功能名称】主键
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteInformationById(Long id);
|
||||
|
||||
/**
|
||||
* 批量删除【请填写功能名称】
|
||||
*
|
||||
* @param ids 需要删除的数据主键集合
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteInformationByIds(Long[] ids);
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.muyu.vehicle.service;
|
||||
|
||||
/** 多数据源
|
||||
* @ClassName
|
||||
* @Author
|
||||
* @Date 2024/4/28 18:46
|
||||
*/
|
||||
public interface DataSourceService {
|
||||
|
||||
void toDefaultDS();
|
||||
|
||||
boolean changeDS(Long datasourceId);
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
package com.muyu.vehicle.service;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.muyu.domain.Information;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 【请填写功能名称】Service接口
|
||||
*
|
||||
* @author ruoyi
|
||||
* @date 2024-05-27
|
||||
*/
|
||||
public interface IInformationService extends IService<Information>
|
||||
{
|
||||
/**
|
||||
* 查询【请填写功能名称】
|
||||
*
|
||||
* @param id 【请填写功能名称】主键
|
||||
* @return 【请填写功能名称】
|
||||
*/
|
||||
public Information selectInformationById(Long id);
|
||||
|
||||
/**
|
||||
* 查询【请填写功能名称】列表
|
||||
*
|
||||
* @param information 【请填写功能名称】
|
||||
* @return 【请填写功能名称】集合
|
||||
*/
|
||||
public List<Information> selectInformationList(Information information);
|
||||
|
||||
/**
|
||||
* 新增【请填写功能名称】
|
||||
*
|
||||
* @param information 【请填写功能名称】
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertInformation(Information information);
|
||||
|
||||
/**
|
||||
* 修改【请填写功能名称】
|
||||
*
|
||||
* @param information 【请填写功能名称】
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateInformation(Information information);
|
||||
|
||||
/**
|
||||
* 批量删除【请填写功能名称】
|
||||
*
|
||||
* @param ids 需要删除的【请填写功能名称】主键集合
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteInformationByIds(Long[] ids);
|
||||
|
||||
/**
|
||||
* 删除【请填写功能名称】信息
|
||||
*
|
||||
* @param id 【请填写功能名称】主键
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteInformationById(Long id);
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package com.muyu.vehicle.service.Impl;
|
||||
|
||||
|
||||
|
||||
import com.muyu.domain.datasources.Datasource;
|
||||
|
||||
import com.muyu.vehicle.config.DynamicRoutingDataSource;
|
||||
import com.muyu.vehicle.context.DataSourceContextHolder;
|
||||
import com.muyu.vehicle.mapper.DataSourceMapper;
|
||||
import com.muyu.vehicle.service.DataSourceService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @ClassName
|
||||
* @Author
|
||||
* @Date
|
||||
*/
|
||||
@Service
|
||||
public class DataSourceServiceImpl implements DataSourceService {
|
||||
|
||||
@Autowired
|
||||
private DataSourceMapper dataSourceMapper;
|
||||
|
||||
@Autowired
|
||||
private DynamicRoutingDataSource dynamicRoutingDataSource;
|
||||
|
||||
@Override
|
||||
public void toDefaultDS() {
|
||||
//切到默认数据源
|
||||
DataSourceContextHolder.removeDataSource(); // 删除数据源
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean changeDS(Long datasourceId) {
|
||||
//切到默认数据源
|
||||
DataSourceContextHolder.removeDataSource(); // 删除数据源
|
||||
//找到所有的配置
|
||||
List<Datasource> dataSourceList = dataSourceMapper.selectList(null); // 数据源对象
|
||||
|
||||
if(!dataSourceList.isEmpty()){
|
||||
for (Datasource dataSource : dataSourceList) { // 数据源对象
|
||||
if(dataSource.getId().equals(datasourceId)){ // 编号
|
||||
System.out.println("已找到数据源,datasourceId是:" + dataSource.getId()); // 编号
|
||||
//判断连接是否存在,不存在就创建
|
||||
dynamicRoutingDataSource.checkCreateDataSource(dataSource); // 检查数据源是否已经创建
|
||||
//切换数据源
|
||||
DataSourceContextHolder.setDataSource(dataSource.getId()); // 编号 --> 切换数据源
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,105 @@
|
|||
package com.muyu.vehicle.service.Impl;
|
||||
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.muyu.common.core.utils.DateUtils;
|
||||
import com.muyu.domain.Information;
|
||||
import com.muyu.vehicle.mapper.InformationMapper;
|
||||
import com.muyu.vehicle.service.IInformationService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* 【请填写功能名称】Service业务层处理
|
||||
*
|
||||
* @author ruoyi
|
||||
* @date 2024-05-27
|
||||
*/
|
||||
@Service
|
||||
public class InformationServiceImpl extends ServiceImpl<InformationMapper, Information> implements IInformationService
|
||||
{
|
||||
@Autowired
|
||||
private InformationMapper informationMapper;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 查询【请填写功能名称】
|
||||
*
|
||||
* @param id 【请填写功能名称】主键
|
||||
* @return 【请填写功能名称】
|
||||
*/
|
||||
@Override
|
||||
public Information selectInformationById(Long id)
|
||||
{
|
||||
return informationMapper.selectInformationById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询【请填写功能名称】列表
|
||||
*
|
||||
* @param information 【请填写功能名称】
|
||||
* @return 【请填写功能名称】
|
||||
*/
|
||||
@Override
|
||||
public List<Information> selectInformationList(Information information)
|
||||
{
|
||||
|
||||
|
||||
return informationMapper.selectInformationList(information);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增【请填写功能名称】
|
||||
*
|
||||
* @param information 【请填写功能名称】
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int insertInformation(Information information)
|
||||
{
|
||||
information.setCreateTime(DateUtils.getNowDate());
|
||||
return informationMapper.insertInformation(information);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改【请填写功能名称】
|
||||
*
|
||||
* @param information 【请填写功能名称】
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int updateInformation(Information information)
|
||||
{
|
||||
information.setUpdateTime(DateUtils.getNowDate());
|
||||
return informationMapper.updateInformation(information);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除【请填写功能名称】
|
||||
*
|
||||
* @param ids 需要删除的【请填写功能名称】主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteInformationByIds(Long[] ids)
|
||||
{
|
||||
return informationMapper.deleteInformationByIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除【请填写功能名称】信息
|
||||
*
|
||||
* @param id 【请填写功能名称】主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteInformationById(Long id)
|
||||
{
|
||||
return informationMapper.deleteInformationById(id);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.muyu.networking.mapper.InformationMapper">
|
||||
|
||||
<resultMap type="com.muyu.domain.Information" id="InformationResult">
|
||||
<result property="id" column="id" />
|
||||
<result property="number" column="number" />
|
||||
<result property="typeId" column="type_id" />
|
||||
<result property="electronicId" column="electronic_id" />
|
||||
<result property="motor" column="motor" />
|
||||
<result property="battery" column="battery" />
|
||||
<result property="motorNumber" column="motor_number" />
|
||||
<result property="batteryNumber" column="battery_number" />
|
||||
<result property="enterpriseId" column="enterprise_id" />
|
||||
<result property="remark" column="remark" />
|
||||
<result property="createBy" column="create_by" />
|
||||
<result property="createTime" column="create_time" />
|
||||
<result property="updateTime" column="update_time" />
|
||||
</resultMap>
|
||||
|
||||
<sql id="selectInformationVo">
|
||||
select id, number, type_id, electronic_id, motor, battery, motor_number, battery_number, enterprise_id, remark, create_by, create_time, update_time from information
|
||||
</sql>
|
||||
|
||||
<select id="selectInformationList" parameterType="com.muyu.domain.Information" resultMap="InformationResult">
|
||||
<include refid="selectInformationVo"/>
|
||||
<where>
|
||||
<if test="number != null and number != ''"> and number = #{number}</if>
|
||||
<if test="typeId != null "> and type_id = #{typeId}</if>
|
||||
<if test="electronicId != null "> and electronic_id = #{electronicId}</if>
|
||||
<if test="motor != null and motor != ''"> and motor = #{motor}</if>
|
||||
<if test="battery != null and battery != ''"> and battery = #{battery}</if>
|
||||
<if test="motorNumber != null and motorNumber != ''"> and motor_number = #{motorNumber}</if>
|
||||
<if test="batteryNumber != null and batteryNumber != ''"> and battery_number = #{batteryNumber}</if>
|
||||
<if test="enterpriseId != null "> and enterprise_id = #{enterpriseId}</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<select id="selectInformationById" parameterType="Long" resultMap="InformationResult">
|
||||
<include refid="selectInformationVo"/>
|
||||
where id = #{id}
|
||||
</select>
|
||||
|
||||
<insert id="insertInformation" parameterType="com.muyu.domain.Information" useGeneratedKeys="true" keyProperty="id">
|
||||
insert into information
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="number != null">number,</if>
|
||||
<if test="typeId != null">type_id,</if>
|
||||
<if test="electronicId != null">electronic_id,</if>
|
||||
<if test="motor != null">motor,</if>
|
||||
<if test="battery != null">battery,</if>
|
||||
<if test="motorNumber != null">motor_number,</if>
|
||||
<if test="batteryNumber != null">battery_number,</if>
|
||||
<if test="enterpriseId != null">enterprise_id,</if>
|
||||
<if test="remark != null">remark,</if>
|
||||
<if test="createBy != null">create_by,</if>
|
||||
<if test="createTime != null">create_time,</if>
|
||||
<if test="updateTime != null">update_time,</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="number != null">#{number},</if>
|
||||
<if test="typeId != null">#{typeId},</if>
|
||||
<if test="electronicId != null">#{electronicId},</if>
|
||||
<if test="motor != null">#{motor},</if>
|
||||
<if test="battery != null">#{battery},</if>
|
||||
<if test="motorNumber != null">#{motorNumber},</if>
|
||||
<if test="batteryNumber != null">#{batteryNumber},</if>
|
||||
<if test="enterpriseId != null">#{enterpriseId},</if>
|
||||
<if test="remark != null">#{remark},</if>
|
||||
<if test="createBy != null">#{createBy},</if>
|
||||
<if test="createTime != null">#{createTime},</if>
|
||||
<if test="updateTime != null">#{updateTime},</if>
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<update id="updateInformation" parameterType="com.muyu.domain.Information">
|
||||
update information
|
||||
<trim prefix="SET" suffixOverrides=",">
|
||||
<if test="number != null">number = #{number},</if>
|
||||
<if test="typeId != null">type_id = #{typeId},</if>
|
||||
<if test="electronicId != null">electronic_id = #{electronicId},</if>
|
||||
<if test="motor != null">motor = #{motor},</if>
|
||||
<if test="battery != null">battery = #{battery},</if>
|
||||
<if test="motorNumber != null">motor_number = #{motorNumber},</if>
|
||||
<if test="batteryNumber != null">battery_number = #{batteryNumber},</if>
|
||||
<if test="enterpriseId != null">enterprise_id = #{enterpriseId},</if>
|
||||
<if test="remark != null">remark = #{remark},</if>
|
||||
<if test="createBy != null">create_by = #{createBy},</if>
|
||||
<if test="createTime != null">create_time = #{createTime},</if>
|
||||
<if test="updateTime != null">update_time = #{updateTime},</if>
|
||||
</trim>
|
||||
where id = #{id}
|
||||
</update>
|
||||
|
||||
<delete id="deleteInformationById" parameterType="Long">
|
||||
delete from information where id = #{id}
|
||||
</delete>
|
||||
|
||||
<delete id="deleteInformationByIds" parameterType="String">
|
||||
delete from information where id in
|
||||
<foreach item="id" collection="array" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
</delete>
|
||||
</mapper>
|
Loading…
Reference in New Issue