工厂数据源切换
parent
ded76bd02d
commit
974dd13455
@ -0,0 +1,24 @@
|
||||
package com.op.common.datascope.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 自动设置当前数据源及数据所属的部门
|
||||
*
|
||||
* @ClassName: DataPoolDept
|
||||
* @Description: TODO
|
||||
* @author shichangzhou
|
||||
* @date 2023年4月24日 下午5:47:07
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface PoolDeptAuto {
|
||||
|
||||
// 是否自动设置数据源标识
|
||||
public boolean pool() default true;
|
||||
|
||||
// 是否自动设置部门
|
||||
public boolean dept() default true;
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.op.common.datasource.creator;
|
||||
|
||||
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
||||
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* 动态创建数据源
|
||||
*
|
||||
* @ClassName: DynamicDatasourceCreator
|
||||
* @Description: TODO
|
||||
* @author shichangzhou
|
||||
* @date 2023年4月20日 下午1:15:09
|
||||
*/
|
||||
@Component
|
||||
public class DynamicDatasourceCreator {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(DynamicDatasourceCreator.class);
|
||||
@Resource
|
||||
private DynamicRoutingDataSource dynamicRoutingDataSource;
|
||||
@Resource
|
||||
private DefaultDataSourceCreator defaultDataSourceCreator;
|
||||
|
||||
public DataSource createDynamicDataSource(DataSourceProperty dataSourceProperty) {
|
||||
log.info("Dynamic Datasource init.....");
|
||||
DataSource dataSource = defaultDataSourceCreator.createDataSource(dataSourceProperty);
|
||||
dynamicRoutingDataSource.addDataSource(dataSourceProperty.getPoolName(), dataSource);
|
||||
return dataSource;
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
com.op.common.datasource.creator.DynamicDatasourceCreator
|
@ -0,0 +1,53 @@
|
||||
package com.op.mes.config;
|
||||
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
||||
import com.op.common.core.domain.R;
|
||||
import com.op.common.datasource.creator.DynamicDatasourceCreator;
|
||||
import com.op.system.api.RemoteUserService;
|
||||
import com.op.system.api.domain.SysUser;
|
||||
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 初始化动态数据源
|
||||
*
|
||||
* @ClassName: DynamicDatasource
|
||||
* @Description: TODO
|
||||
* @author shichangzhou
|
||||
* @date 2023年4月19日 下午1:01:30
|
||||
*/
|
||||
@Component
|
||||
public class DynamicDatasource {
|
||||
|
||||
@Resource
|
||||
private DynamicDatasourceCreator dynamicDatasourceCreator;
|
||||
@Autowired
|
||||
private RemoteUserService remoteUserService;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
// 加载sf-cloud库的sys_datasource
|
||||
SysUser sysUser = new SysUser();
|
||||
sysUser.setUserId(1L);
|
||||
R<List<Map<String, String>>> dateSources0 = remoteUserService.getPoolNameList(sysUser);
|
||||
List<Map<String, String>> dateSources = dateSources0.getData();
|
||||
for (Map<String, String> dateSource : dateSources) {
|
||||
DataSourceProperty sdp = new DataSourceProperty();
|
||||
sdp.setUrl(dateSource.get("url"));
|
||||
sdp.setUsername(dateSource.get("userName"));
|
||||
sdp.setPassword(dateSource.get("password"));
|
||||
sdp.setDriverClassName(dateSource.get("driveClassName"));
|
||||
sdp.setPoolName(dateSource.get("poolName"));// 这是数据源的key
|
||||
sdp.setLazy(false);
|
||||
dynamicDatasourceCreator.createDynamicDataSource(sdp);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Binary file not shown.
@ -0,0 +1,47 @@
|
||||
package com.op.system.config;
|
||||
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
||||
import com.op.common.datasource.creator.DynamicDatasourceCreator;
|
||||
import com.op.system.api.domain.SysUser;
|
||||
import com.op.system.service.ISysUserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 初始化动态数据源
|
||||
*
|
||||
* @ClassName: DynamicDatasource
|
||||
* @Description: TODO
|
||||
* @author shichangzhou
|
||||
* @date 2023年4月19日 下午1:01:30
|
||||
*/
|
||||
@Component
|
||||
public class DynamicDatasource {
|
||||
|
||||
@Resource
|
||||
private DynamicDatasourceCreator dynamicDatasourceCreator;
|
||||
@Autowired
|
||||
private ISysUserService sysUserService;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
SysUser sysUser = new SysUser();
|
||||
sysUser.setUserId(1L);
|
||||
List<Map<String, String>> dateSources = sysUserService.getPoolNameList(sysUser);
|
||||
for (Map<String, String> dateSource : dateSources) {
|
||||
DataSourceProperty sdp = new DataSourceProperty();
|
||||
sdp.setUrl(dateSource.get("url"));
|
||||
sdp.setUsername(dateSource.get("userName"));
|
||||
sdp.setPassword(dateSource.get("password"));
|
||||
sdp.setDriverClassName(dateSource.get("driveClassName"));
|
||||
sdp.setPoolName(dateSource.get("poolName"));// 这是数据源的key
|
||||
sdp.setLazy(false);
|
||||
dynamicDatasourceCreator.createDynamicDataSource(sdp);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,155 @@
|
||||
package com.op.system.controller;
|
||||
|
||||
import com.op.common.core.utils.poi.ExcelUtil;
|
||||
import com.op.common.core.web.controller.BaseController;
|
||||
import com.op.common.core.web.domain.AjaxResult;
|
||||
import com.op.common.core.web.page.TableDataInfo;
|
||||
import com.op.common.log.annotation.Log;
|
||||
import com.op.common.log.enums.BusinessType;
|
||||
import com.op.common.security.annotation.RequiresPermissions;
|
||||
import com.op.system.api.domain.SysUser;
|
||||
import com.op.system.domain.SysDatasource;
|
||||
import com.op.system.service.ISysDatasourceService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 工厂数据库Controller
|
||||
*
|
||||
* @author sf
|
||||
* @date 2023-05-18
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/datasource")
|
||||
public class SysDatasourceController extends BaseController {
|
||||
@Autowired
|
||||
private ISysDatasourceService sysDatasourceService;
|
||||
|
||||
/**
|
||||
* 查询工厂数据库列表
|
||||
*/
|
||||
@RequiresPermissions("system:datasource:list")
|
||||
@GetMapping("/list")
|
||||
public TableDataInfo list(SysDatasource sysDatasource) {
|
||||
startPage();
|
||||
List<SysDatasource> list = sysDatasourceService.selectSysDatasourceList(sysDatasource);
|
||||
return getDataTable(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出工厂数据库列表
|
||||
*/
|
||||
@RequiresPermissions("system:datasource:export")
|
||||
@Log(title = "工厂数据库", businessType = BusinessType.EXPORT)
|
||||
@PostMapping("/export")
|
||||
public void export(HttpServletResponse response, SysDatasource sysDatasource) {
|
||||
List<SysDatasource> list = sysDatasourceService.selectSysDatasourceList(sysDatasource);
|
||||
ExcelUtil<SysDatasource> util = new ExcelUtil<SysDatasource>(SysDatasource.class);
|
||||
util.exportExcel(response, list, "工厂数据库数据");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取工厂数据库详细信息
|
||||
*/
|
||||
@RequiresPermissions("system:datasource:query")
|
||||
@GetMapping(value = "/{id}")
|
||||
public AjaxResult getInfo(@PathVariable("id") String id) {
|
||||
return success(sysDatasourceService.selectSysDatasourceById(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增工厂数据库
|
||||
*/
|
||||
@RequiresPermissions("system:datasource:add")
|
||||
@Log(title = "工厂数据库", businessType = BusinessType.INSERT)
|
||||
@PostMapping
|
||||
public AjaxResult add(@RequestBody SysDatasource sysDatasource) {
|
||||
return toAjax(sysDatasourceService.insertSysDatasource(sysDatasource));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改工厂数据库
|
||||
*/
|
||||
@RequiresPermissions("system:datasource:edit")
|
||||
@Log(title = "工厂数据库", businessType = BusinessType.UPDATE)
|
||||
@PutMapping
|
||||
public AjaxResult edit(@RequestBody SysDatasource sysDatasource) {
|
||||
return toAjax(sysDatasourceService.updateSysDatasource(sysDatasource));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除工厂数据库
|
||||
*/
|
||||
@RequiresPermissions("system:datasource:remove")
|
||||
@Log(title = "工厂数据库", businessType = BusinessType.DELETE)
|
||||
@DeleteMapping("/{ids}")
|
||||
public AjaxResult remove(@PathVariable String[] ids) {
|
||||
return toAjax(sysDatasourceService.deleteSysDatasourceByIds(ids));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询已分配用户角色列表
|
||||
*/
|
||||
@RequiresPermissions("system:datasource:edit")
|
||||
@GetMapping("/authUser/allocatedList")
|
||||
public TableDataInfo allocatedList(SysDatasource sysDatasource) {
|
||||
startPage();
|
||||
List<SysDatasource> list = sysDatasourceService.selectAllocatedList(sysDatasource);
|
||||
return getDataTable(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询未分配用户角色列表
|
||||
*/
|
||||
//@RequiresPermissions("system:role:list")
|
||||
@GetMapping("/authUser/unallocatedList")
|
||||
public TableDataInfo unallocatedList(SysUser user) {
|
||||
startPage();
|
||||
List<SysUser> list = sysDatasourceService.selectUnallocatedList(user);
|
||||
return getDataTable(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量选择用户授权
|
||||
*/
|
||||
//@RequiresPermissions("system:role:edit")
|
||||
@Log(title = "数据源管理", businessType = BusinessType.GRANT)
|
||||
@PutMapping("/authUser/selectAll")
|
||||
public AjaxResult selectAuthUserAll(String datasourceId, Long[] userIds) {
|
||||
//sysDatasourceService.checkRoleDataScope(datasourceId);
|
||||
return toAjax(sysDatasourceService.insertAuthUsers(datasourceId, userIds));
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消授权用户
|
||||
*/
|
||||
//@RequiresPermissions("system:role:edit")
|
||||
@Log(title = "数据源管理", businessType = BusinessType.GRANT)
|
||||
@PutMapping("/authUser/cancel")
|
||||
public AjaxResult cancelAuthUser(@RequestBody SysDatasource sysDatasource) {
|
||||
return toAjax(sysDatasourceService.deleteAuthUser(sysDatasource));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量取消授权用户
|
||||
*/
|
||||
//@RequiresPermissions("system:role:edit")
|
||||
@Log(title = "角色管理", businessType = BusinessType.GRANT)
|
||||
@PutMapping("/authUser/cancelAll")
|
||||
public AjaxResult cancelAuthUserAll(String datasourceId, Long[] userIds) {
|
||||
return toAjax(sysDatasourceService.deleteAuthUsers(datasourceId, userIds));
|
||||
}
|
||||
|
||||
@RequiresPermissions("system:datasource:add")
|
||||
@Log(title = "激活工厂数据库", businessType = BusinessType.UPDATE)
|
||||
@GetMapping("/changeDBStatus/{datasourceId}")
|
||||
public AjaxResult changeDBStatus(@PathVariable("datasourceId") String datasourceId) {
|
||||
SysDatasource sysDatasource = new SysDatasource();
|
||||
sysDatasource.setDatasourceId(datasourceId);
|
||||
return success(sysDatasourceService.changeDBStatus(sysDatasource));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package com.op.system.mapper;
|
||||
|
||||
import com.op.system.api.domain.SysUser;
|
||||
import com.op.system.domain.SysDatasource;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 工厂数据库Mapper接口
|
||||
*
|
||||
* @author sf
|
||||
* @date 2023-05-18
|
||||
*/
|
||||
@Mapper
|
||||
public interface SysDatasourceMapper
|
||||
{
|
||||
/**
|
||||
* 查询工厂数据库
|
||||
*
|
||||
* @param id 工厂数据库主键
|
||||
* @return 工厂数据库
|
||||
*/
|
||||
public SysDatasource selectSysDatasourceById(String id);
|
||||
|
||||
/**
|
||||
* 查询工厂数据库列表
|
||||
*
|
||||
* @param sysDatasource 工厂数据库
|
||||
* @return 工厂数据库集合
|
||||
*/
|
||||
public List<SysDatasource> selectSysDatasourceList(SysDatasource sysDatasource);
|
||||
|
||||
/**
|
||||
* 新增工厂数据库
|
||||
*
|
||||
* @param sysDatasource 工厂数据库
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertSysDatasource(SysDatasource sysDatasource);
|
||||
|
||||
/**
|
||||
* 修改工厂数据库
|
||||
*
|
||||
* @param sysDatasource 工厂数据库
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateSysDatasource(SysDatasource sysDatasource);
|
||||
|
||||
/**
|
||||
* 删除工厂数据库
|
||||
*
|
||||
* @param id 工厂数据库主键
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteSysDatasourceById(String id);
|
||||
|
||||
/**
|
||||
* 批量删除工厂数据库
|
||||
*
|
||||
* @param ids 需要删除的数据主键集合
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteSysDatasourceByIds(String[] ids);
|
||||
|
||||
List<SysDatasource> selectAllocatedList(SysDatasource sysDatasource);
|
||||
|
||||
List<SysUser> selectUnallocatedList(SysUser user);
|
||||
|
||||
int batchUserDB(List<SysDatasource> list);
|
||||
|
||||
int deleteUserRoleInfo(SysDatasource sysDatasource);
|
||||
|
||||
int deleteUserRoleInfos(@Param("datasourceId") String datasourceId, @Param("userIds") Long[] userIds);
|
||||
|
||||
List<SysDatasource> selectDBListByDept(SysDatasource qo);
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package com.op.system.service;
|
||||
|
||||
import com.op.system.api.domain.SysUser;
|
||||
import com.op.system.domain.SysDatasource;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 工厂数据库Service接口
|
||||
*
|
||||
* @author sf
|
||||
* @date 2023-05-18
|
||||
*/
|
||||
public interface ISysDatasourceService
|
||||
{
|
||||
/**
|
||||
* 查询工厂数据库
|
||||
*
|
||||
* @param id 工厂数据库主键
|
||||
* @return 工厂数据库
|
||||
*/
|
||||
public SysDatasource selectSysDatasourceById(String id);
|
||||
|
||||
/**
|
||||
* 查询工厂数据库列表
|
||||
*
|
||||
* @param sysDatasource 工厂数据库
|
||||
* @return 工厂数据库集合
|
||||
*/
|
||||
public List<SysDatasource> selectSysDatasourceList(SysDatasource sysDatasource);
|
||||
|
||||
/**
|
||||
* 新增工厂数据库
|
||||
*
|
||||
* @param sysDatasource 工厂数据库
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertSysDatasource(SysDatasource sysDatasource);
|
||||
|
||||
/**
|
||||
* 修改工厂数据库
|
||||
*
|
||||
* @param sysDatasource 工厂数据库
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateSysDatasource(SysDatasource sysDatasource);
|
||||
|
||||
/**
|
||||
* 批量删除工厂数据库
|
||||
*
|
||||
* @param ids 需要删除的工厂数据库主键集合
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteSysDatasourceByIds(String[] ids);
|
||||
|
||||
/**
|
||||
* 删除工厂数据库信息
|
||||
*
|
||||
* @param id 工厂数据库主键
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteSysDatasourceById(String id);
|
||||
|
||||
List<SysDatasource> selectAllocatedList(SysDatasource sysDatasource);
|
||||
|
||||
List<SysUser> selectUnallocatedList(SysUser user);
|
||||
|
||||
void checkRoleDataScope(String datasourceId);
|
||||
|
||||
int insertAuthUsers(String datasourceId, Long[] userIds);
|
||||
|
||||
int deleteAuthUser(SysDatasource sysDatasource);
|
||||
|
||||
int deleteAuthUsers(String datasourceId, Long[] userIds);
|
||||
|
||||
String changeDBStatus(SysDatasource sysDatasource);
|
||||
}
|
@ -0,0 +1,224 @@
|
||||
package com.op.system.service.impl;
|
||||
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
||||
import com.op.common.core.utils.DateUtils;
|
||||
import com.op.common.core.utils.StringUtils;
|
||||
import com.op.common.core.utils.uuid.IdUtils;
|
||||
import com.op.common.datasource.creator.DynamicDatasourceCreator;
|
||||
import com.op.system.api.domain.SysUser;
|
||||
import com.op.system.domain.SysDatasource;
|
||||
import com.op.system.mapper.SysDatasourceMapper;
|
||||
import com.op.system.service.ISysDatasourceService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 工厂数据库Service业务层处理
|
||||
*
|
||||
* @author sf
|
||||
* @date 2023-05-18
|
||||
*/
|
||||
@Service
|
||||
public class SysDatasourceServiceImpl implements ISysDatasourceService {
|
||||
private static final Logger log = LoggerFactory.getLogger(SysDatasourceServiceImpl.class);
|
||||
@Autowired
|
||||
private SysDatasourceMapper sysDatasourceMapper;
|
||||
@Value("${sqlserve.prev}")
|
||||
private String mysqlPrev;
|
||||
@Value("${sqlserve.affter}")
|
||||
private String mysqlAffter;
|
||||
@Value("${sqlserve.poolName.prev}")
|
||||
private String poolNamePrev;
|
||||
@Value("${sqlserve.driver}")
|
||||
private String driver;
|
||||
@Resource
|
||||
private DynamicDatasourceCreator dynamicDatasourceCreator;
|
||||
|
||||
/**
|
||||
* 查询工厂数据库
|
||||
*
|
||||
* @param id 工厂数据库主键
|
||||
* @return 工厂数据库
|
||||
*/
|
||||
@Override
|
||||
public SysDatasource selectSysDatasourceById(String id) {
|
||||
return sysDatasourceMapper.selectSysDatasourceById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询工厂数据库列表
|
||||
*
|
||||
* @param sysDatasource 工厂数据库
|
||||
* @return 工厂数据库
|
||||
*/
|
||||
@Override
|
||||
public List<SysDatasource> selectSysDatasourceList(SysDatasource sysDatasource) {
|
||||
return sysDatasourceMapper.selectSysDatasourceList(sysDatasource);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增工厂数据库
|
||||
*
|
||||
* @param sysDatasource 工厂数据库
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int insertSysDatasource(SysDatasource sysDatasource) {
|
||||
sysDatasource.setCreateTime(DateUtils.getNowDate());
|
||||
sysDatasource.setId(IdUtils.randomUUID().replace("-",""));
|
||||
sysDatasource.setDriveClassName(driver);
|
||||
if(StringUtils.isNotEmpty(sysDatasource.getIpPort())){
|
||||
sysDatasource.setUrl(mysqlPrev+sysDatasource.getIpPort().split("/")[0]
|
||||
+mysqlAffter+sysDatasource.getIpPort().split("/")[1]);
|
||||
}
|
||||
if(sysDatasource.getDeptId()!=null){
|
||||
if(StringUtils.isNotEmpty(sysDatasource.getSystemType())){
|
||||
sysDatasource.setPoolName(poolNamePrev+"_"+sysDatasource.getSystemType()+"_"+sysDatasource.getDeptId());
|
||||
}else{
|
||||
sysDatasource.setPoolName(poolNamePrev+"_"+sysDatasource.getDeptId());
|
||||
}
|
||||
}
|
||||
|
||||
return sysDatasourceMapper.insertSysDatasource(sysDatasource);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改工厂数据库
|
||||
*
|
||||
* @param sysDatasource 工厂数据库
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int updateSysDatasource(SysDatasource sysDatasource) {
|
||||
|
||||
//修改之后status->1 不生效,需要重新测试链接才生效
|
||||
sysDatasource.setStatus("1");
|
||||
sysDatasource.setUpdateTime(DateUtils.getNowDate());
|
||||
return sysDatasourceMapper.updateSysDatasource(sysDatasource);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除工厂数据库
|
||||
*
|
||||
* @param ids 需要删除的工厂数据库主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteSysDatasourceByIds(String[] ids) {
|
||||
return sysDatasourceMapper.deleteSysDatasourceByIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除工厂数据库信息
|
||||
*
|
||||
* @param id 工厂数据库主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteSysDatasourceById(String id) {
|
||||
return sysDatasourceMapper.deleteSysDatasourceById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysDatasource> selectAllocatedList(SysDatasource sysDatasource) {
|
||||
return sysDatasourceMapper.selectAllocatedList(sysDatasource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysUser> selectUnallocatedList(SysUser user) {
|
||||
return sysDatasourceMapper.selectUnallocatedList(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkRoleDataScope(String datasourceId) {
|
||||
// if (!SysUser.isAdmin(SecurityUtils.getUserId())) {
|
||||
// SysRole role = new SysRole();
|
||||
// role.setRoleId(roleId);
|
||||
// List<SysRole> roles = SpringUtils.getAopProxy(this).selectRoleList(role);
|
||||
// if (StringUtils.isEmpty(roles)) {
|
||||
// throw new ServiceException("没有权限访问角色数据!");
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
@Override
|
||||
public int insertAuthUsers(String datasourceId, Long[] userIds) {
|
||||
// 新增用户与数据源管理
|
||||
List<SysDatasource> list = new ArrayList<SysDatasource>();
|
||||
for (Long userId : userIds) {
|
||||
SysDatasource ur = new SysDatasource();
|
||||
ur.setUserId(userId.toString());
|
||||
ur.setDatasourceId(datasourceId.toString());
|
||||
list.add(ur);
|
||||
}
|
||||
return sysDatasourceMapper.batchUserDB(list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int deleteAuthUser(SysDatasource sysDatasource) {
|
||||
return sysDatasourceMapper.deleteUserRoleInfo(sysDatasource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int deleteAuthUsers(String datasourceId, Long[] userIds) {
|
||||
return sysDatasourceMapper.deleteUserRoleInfos(datasourceId, userIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String changeDBStatus(SysDatasource sd) {
|
||||
|
||||
SysDatasource sysDatasource = sysDatasourceMapper.selectSysDatasourceById(sd.getDatasourceId());
|
||||
|
||||
if(sysDatasource == null){
|
||||
return "数据库链接不存在";
|
||||
}
|
||||
try {
|
||||
Class.forName(sysDatasource.getDriveClassName());
|
||||
Connection conn = DriverManager.getConnection(sysDatasource.getUrl(),
|
||||
sysDatasource.getUserName(),
|
||||
sysDatasource.getPassword());
|
||||
if (conn != null) {
|
||||
if (conn.isClosed()) {
|
||||
log.error("[{}]数据库连接检测异常:[{}]",sysDatasource.getPoolName());
|
||||
return "数据库链接异常,请检查数据库基本信息。";//假如打不开数据库链接则跳过初始化数据库
|
||||
} else {
|
||||
//说明数据库可以正常连接,关闭测试连接
|
||||
conn.close();
|
||||
log.info("[{}]数据库连接检测正常", sysDatasource.getPoolName());
|
||||
}
|
||||
} else {
|
||||
log.error("[{}]数据库连接检测异常:[{}]", sysDatasource.getPoolName());
|
||||
return "数据库链接异常,请检查数据库基本信息。";
|
||||
}
|
||||
|
||||
sysDatasource.setUpdateTime(DateUtils.getNowDate());
|
||||
sysDatasource.setStatus("0");//激活
|
||||
sysDatasourceMapper.updateSysDatasource(sysDatasource);
|
||||
|
||||
DataSourceProperty dataSourceProperty = new DataSourceProperty();
|
||||
dataSourceProperty.setUrl(sysDatasource.getUrl());
|
||||
dataSourceProperty.setUsername(sysDatasource.getUserName());
|
||||
dataSourceProperty.setPassword(sysDatasource.getPassword());
|
||||
dataSourceProperty.setDriverClassName(sysDatasource.getDriveClassName());
|
||||
dataSourceProperty.setPoolName(sysDatasource.getPoolName());// 这是数据源的key
|
||||
dataSourceProperty.setLazy(false);
|
||||
dynamicDatasourceCreator.createDynamicDataSource(dataSourceProperty);
|
||||
|
||||
return "数据库链接测试成功,数据库链接状态正常";
|
||||
}catch (ClassNotFoundException | SQLException classNotFoundException){
|
||||
return "数据库链接异常,请检查数据库基本信息。";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,191 @@
|
||||
<?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.op.system.mapper.SysDatasourceMapper">
|
||||
|
||||
<resultMap type="SysDatasource" id="SysDatasourceResult">
|
||||
<result property="id" column="id" />
|
||||
<result property="url" column="url" />
|
||||
<result property="ipPort" column="ip_port" />
|
||||
<result property="userName" column="user_name" />
|
||||
<result property="password" column="password" />
|
||||
<result property="driveClassName" column="drive_class_name" />
|
||||
<result property="poolName" column="pool_name" />
|
||||
<result property="parkName" column="park_name" />
|
||||
<result property="status" column="status" />
|
||||
<result property="delFlag" column="del_flag" />
|
||||
<result property="createBy" column="create_by" />
|
||||
<result property="createTime" column="create_time" />
|
||||
<result property="updateBy" column="update_by" />
|
||||
<result property="updateTime" column="update_time" />
|
||||
<result property="remark" column="remark" />
|
||||
<result property="deptId" column="dept_id" />
|
||||
<result property="schemaCreated" column="schema_created" />
|
||||
<result property="deptName" column="dept_name" />
|
||||
<result property="systemType" column="system_type" />
|
||||
</resultMap>
|
||||
|
||||
<sql id="selectSysDatasourceVo">
|
||||
select id, url, ip_port, user_name, password, drive_class_name, pool_name, park_name,
|
||||
status, del_flag, create_by, create_time, update_by, update_time, remark,system_type,
|
||||
dept_id, schema_created from sys_datasource
|
||||
</sql>
|
||||
|
||||
<select id="selectSysDatasourceList" parameterType="SysDatasource" resultMap="SysDatasourceResult">
|
||||
select sd.id, sd.url, sd.ip_port, sd.user_name, sd.password, sd.drive_class_name, sd.pool_name, sd.park_name,
|
||||
sd.status, sd.del_flag, sd.create_by, sd.create_time, sd.update_by, sd.update_time, sd.remark,sd.system_type,
|
||||
sd.dept_id,d.dept_name, sd.schema_created from sys_datasource sd
|
||||
left join sys_dept d on sd.dept_id = d.dept_id
|
||||
<where>
|
||||
<if test="url != null and url != ''"> and url = #{url}</if>
|
||||
<if test="ipPort != null and ipPort != ''"> and ip_port like concat('%', #{ipPort}, '%')</if>
|
||||
<if test="userName != null and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
|
||||
<if test="password != null and password != ''"> and password = #{password}</if>
|
||||
<if test="driveClassName != null and driveClassName != ''"> and drive_class_name like concat('%', #{driveClassName}, '%')</if>
|
||||
<if test="poolName != null and poolName != ''"> and pool_name like concat('%', #{poolName}, '%')</if>
|
||||
<if test="parkName != null and parkName != ''"> and park_name like concat('%', #{parkName}, '%')</if>
|
||||
<if test="status != null and status != ''"> and status = #{status}</if>
|
||||
<if test="deptId != null "> and dept_id = #{deptId}</if>
|
||||
<if test="schemaCreated != null and schemaCreated != ''"> and schema_created = #{schemaCreated}</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<select id="selectSysDatasourceById" parameterType="String" resultMap="SysDatasourceResult">
|
||||
<include refid="selectSysDatasourceVo"/>
|
||||
where id = #{id}
|
||||
</select>
|
||||
<select id="selectAllocatedList" resultType="com.op.system.domain.SysDatasource">
|
||||
select sd.id,
|
||||
sd.ip_port ipPort,
|
||||
sd.pool_name poolName,
|
||||
sud.user_id userId,
|
||||
su.user_name userName,
|
||||
su.nick_name nickName
|
||||
from sys_datasource sd
|
||||
left join sys_user_datasource sud on sd.id = sud.datasource_id
|
||||
left join sys_user su on su.user_id = sud.user_id
|
||||
where sud.user_id is not null
|
||||
<if test="nickName != null and nickName != ''">
|
||||
AND su.nick_name like concat('%', #{nickName}, '%')
|
||||
</if>
|
||||
<if test="datasourceId != null and datasourceId != ''">
|
||||
AND sd.id = #{datasourceId}
|
||||
</if>
|
||||
</select>
|
||||
<select id="selectUnallocatedList" resultType="com.op.system.api.domain.SysUser">
|
||||
select distinct u.user_id userId, u.dept_id deptId, u.user_name userName, u.nick_name nickName,
|
||||
u.email, u.phonenumber, u.status, u.create_time createTime
|
||||
from sys_user u
|
||||
left join sys_user_datasource ur on u.user_id = ur.user_id
|
||||
left join sys_datasource r on r.id = ur.datasource_id
|
||||
where u.del_flag = '0' and (r.id != #{datasourceId} or r.id IS NULL)
|
||||
and u.user_id not in (select ur.user_id from sys_user_datasource ur where ur.datasource_id = #{datasourceId})
|
||||
<if test="userName != null and userName != ''">
|
||||
AND u.user_name like concat('%', #{userName}, '%')
|
||||
</if>
|
||||
<if test="phonenumber != null and phonenumber != ''">
|
||||
AND u.phonenumber like concat('%', #{phonenumber}, '%')
|
||||
</if>
|
||||
</select>
|
||||
<select id="selectDBListByDept" resultType="com.op.system.domain.SysDatasource">
|
||||
select id
|
||||
from sys_datasource
|
||||
where del_flag = 0 and dept_id = #{deptId}
|
||||
</select>
|
||||
|
||||
<insert id="insertSysDatasource" parameterType="SysDatasource">
|
||||
insert into sys_datasource
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">id,</if>
|
||||
<if test="url != null and url != ''">url,</if>
|
||||
<if test="ipPort != null">ip_port,</if>
|
||||
<if test="userName != null and userName != ''">user_name,</if>
|
||||
<if test="password != null and password != ''">password,</if>
|
||||
<if test="driveClassName != null and driveClassName != ''">drive_class_name,</if>
|
||||
<if test="poolName != null and poolName != ''">pool_name,</if>
|
||||
<if test="parkName != null">park_name,</if>
|
||||
<if test="status != null">status,</if>
|
||||
<if test="delFlag != null">del_flag,</if>
|
||||
<if test="createBy != null">create_by,</if>
|
||||
<if test="createTime != null">create_time,</if>
|
||||
<if test="updateBy != null">update_by,</if>
|
||||
<if test="updateTime != null">update_time,</if>
|
||||
<if test="remark != null">remark,</if>
|
||||
<if test="deptId != null">dept_id,</if>
|
||||
<if test="schemaCreated != null">schema_created,</if>
|
||||
<if test="systemType != null">system_type,</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">#{id},</if>
|
||||
<if test="url != null and url != ''">#{url},</if>
|
||||
<if test="ipPort != null">#{ipPort},</if>
|
||||
<if test="userName != null and userName != ''">#{userName},</if>
|
||||
<if test="password != null and password != ''">#{password},</if>
|
||||
<if test="driveClassName != null and driveClassName != ''">#{driveClassName},</if>
|
||||
<if test="poolName != null and poolName != ''">#{poolName},</if>
|
||||
<if test="parkName != null">#{parkName},</if>
|
||||
<if test="status != null">#{status},</if>
|
||||
<if test="delFlag != null">#{delFlag},</if>
|
||||
<if test="createBy != null">#{createBy},</if>
|
||||
<if test="createTime != null">#{createTime},</if>
|
||||
<if test="updateBy != null">#{updateBy},</if>
|
||||
<if test="updateTime != null">#{updateTime},</if>
|
||||
<if test="remark != null">#{remark},</if>
|
||||
<if test="deptId != null">#{deptId},</if>
|
||||
<if test="schemaCreated != null">#{schemaCreated},</if>
|
||||
<if test="systemType != null">#{systemType},</if>
|
||||
</trim>
|
||||
</insert>
|
||||
<insert id="batchUserDB">
|
||||
insert into sys_user_datasource(user_id, datasource_id) values
|
||||
<foreach item="item" index="index" collection="list" separator=",">
|
||||
(#{item.userId},#{item.datasourceId})
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
<update id="updateSysDatasource" parameterType="SysDatasource">
|
||||
update sys_datasource
|
||||
<trim prefix="SET" suffixOverrides=",">
|
||||
<if test="url != null and url != ''">url = #{url},</if>
|
||||
<if test="ipPort != null">ip_port = #{ipPort},</if>
|
||||
<if test="userName != null and userName != ''">user_name = #{userName},</if>
|
||||
<if test="password != null and password != ''">password = #{password},</if>
|
||||
<if test="driveClassName != null and driveClassName != ''">drive_class_name = #{driveClassName},</if>
|
||||
<if test="poolName != null and poolName != ''">pool_name = #{poolName},</if>
|
||||
<if test="parkName != null">park_name = #{parkName},</if>
|
||||
<if test="status != null">status = #{status},</if>
|
||||
<if test="delFlag != null">del_flag = #{delFlag},</if>
|
||||
<if test="createBy != null">create_by = #{createBy},</if>
|
||||
<if test="createTime != null">create_time = #{createTime},</if>
|
||||
<if test="updateBy != null">update_by = #{updateBy},</if>
|
||||
<if test="updateTime != null">update_time = #{updateTime},</if>
|
||||
<if test="remark != null">remark = #{remark},</if>
|
||||
<if test="deptId != null">dept_id = #{deptId},</if>
|
||||
<if test="schemaCreated != null">schema_created = #{schemaCreated},</if>
|
||||
<if test="systemType != null">system_type = #{systemType},</if>
|
||||
</trim>
|
||||
where id = #{id}
|
||||
</update>
|
||||
|
||||
<delete id="deleteSysDatasourceById" parameterType="String">
|
||||
delete from sys_datasource where id = #{id}
|
||||
</delete>
|
||||
|
||||
<delete id="deleteSysDatasourceByIds" parameterType="String">
|
||||
delete from sys_datasource where id in
|
||||
<foreach item="id" collection="array" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
</delete>
|
||||
<delete id="deleteUserRoleInfo">
|
||||
delete from sys_user_datasource where user_id=#{userId} and datasource_id=#{datasourceId}
|
||||
</delete>
|
||||
|
||||
<delete id="deleteUserRoleInfos">
|
||||
delete from sys_user_datasource where datasource_id=#{datasourceId} and user_id in
|
||||
<foreach collection="userIds" item="userId" open="(" separator="," close=")">
|
||||
#{userId}
|
||||
</foreach>
|
||||
</delete>
|
||||
</mapper>
|
@ -0,0 +1,51 @@
|
||||
package com.op.wms.config;
|
||||
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
||||
import com.op.common.core.domain.R;
|
||||
import com.op.common.datasource.creator.DynamicDatasourceCreator;
|
||||
import com.op.system.api.RemoteUserService;
|
||||
import com.op.system.api.domain.SysUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 初始化动态数据源
|
||||
*
|
||||
* @ClassName: DynamicDatasource
|
||||
* @Description: TODO
|
||||
* @author shichangzhou
|
||||
* @date 2023年4月19日 下午1:01:30
|
||||
*/
|
||||
@Component
|
||||
public class DynamicDatasource {
|
||||
|
||||
@Resource
|
||||
private DynamicDatasourceCreator dynamicDatasourceCreator;
|
||||
@Autowired
|
||||
private RemoteUserService remoteUserService;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
// 加载sf-cloud库的sys_datasource
|
||||
SysUser sysUser = new SysUser();
|
||||
sysUser.setUserId(1L);
|
||||
R<List<Map<String, String>>> dateSources0 = remoteUserService.getPoolNameList(sysUser);
|
||||
List<Map<String, String>> dateSources = dateSources0.getData();
|
||||
for (Map<String, String> dateSource : dateSources) {
|
||||
DataSourceProperty sdp = new DataSourceProperty();
|
||||
sdp.setUrl(dateSource.get("url"));
|
||||
sdp.setUsername(dateSource.get("userName"));
|
||||
sdp.setPassword(dateSource.get("password"));
|
||||
sdp.setDriverClassName(dateSource.get("driveClassName"));
|
||||
sdp.setPoolName(dateSource.get("poolName"));// 这是数据源的key
|
||||
sdp.setLazy(false);
|
||||
dynamicDatasourceCreator.createDynamicDataSource(sdp);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue