feat:多数据源模块
parent
931c5a4634
commit
3fccdf3da0
|
@ -0,0 +1,106 @@
|
|||
package com.zhiLian.business.controller;
|
||||
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.zhiLian.business.domain.Entinfo;
|
||||
import com.zhiLian.business.service.IEntinfoService;
|
||||
import com.zhiLian.common.core.domain.Result;
|
||||
import com.zhiLian.common.core.utils.poi.ExcelUtil;
|
||||
import com.zhiLian.common.core.web.controller.BaseController;
|
||||
import com.zhiLian.common.core.web.page.TableDataInfo;
|
||||
import com.zhiLian.common.log.annotation.Log;
|
||||
import com.zhiLian.common.log.enums.BusinessType;
|
||||
import com.zhiLian.common.security.annotation.RequiresPermissions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 多数据源Controller
|
||||
*
|
||||
* @author muyu
|
||||
* @date 2024-06-06
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/entinfo")
|
||||
public class EntinfoController extends BaseController
|
||||
{
|
||||
@Autowired
|
||||
private IEntinfoService entinfoService;
|
||||
|
||||
/**
|
||||
* 查询多数据源列表
|
||||
*/
|
||||
// @RequiresPermissions("system:entinfo:list")
|
||||
@GetMapping("/list")
|
||||
public Result<TableDataInfo<Entinfo>> list(Entinfo entinfo)
|
||||
{
|
||||
startPage();
|
||||
List<Entinfo> list = entinfoService.selectEntinfoList(entinfo);
|
||||
return getDataTable(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出多数据源列表
|
||||
*/
|
||||
// @RequiresPermissions("system:entinfo:export")
|
||||
@Log(title = "多数据源", businessType = BusinessType.EXPORT)
|
||||
@PostMapping("/export")
|
||||
public void export(HttpServletResponse response, Entinfo entinfo)
|
||||
{
|
||||
List<Entinfo> list = entinfoService.selectEntinfoList(entinfo);
|
||||
ExcelUtil<Entinfo> util = new ExcelUtil<Entinfo>(Entinfo.class);
|
||||
util.exportExcel(response, list, "多数据源数据");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取多数据源详细信息
|
||||
*/
|
||||
// @RequiresPermissions("system:entinfo:query")
|
||||
@GetMapping(value = "/{id}")
|
||||
public Result getInfo(@PathVariable("id") Long id)
|
||||
{
|
||||
return success(entinfoService.selectEntinfoById(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增多数据源
|
||||
*/
|
||||
// @RequiresPermissions("system:entinfo:add")
|
||||
@Log(title = "多数据源", businessType = BusinessType.INSERT)
|
||||
@PostMapping
|
||||
public Result add(@RequestBody Entinfo entinfo)
|
||||
{
|
||||
return toAjax(entinfoService.insertEntinfo(entinfo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改多数据源
|
||||
*/
|
||||
// @RequiresPermissions("system:entinfo:edit")
|
||||
@Log(title = "多数据源", businessType = BusinessType.UPDATE)
|
||||
@PutMapping
|
||||
public Result edit(@RequestBody Entinfo entinfo)
|
||||
{
|
||||
return toAjax(entinfoService.updateEntinfo(entinfo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除多数据源
|
||||
*/
|
||||
// @RequiresPermissions("system:entinfo:remove")
|
||||
@Log(title = "多数据源", businessType = BusinessType.DELETE)
|
||||
@DeleteMapping("/{ids}")
|
||||
public Result remove(@PathVariable Long[] ids)
|
||||
{
|
||||
return toAjax(entinfoService.deleteEntinfoByIds(ids));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.zhiLian.business.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.zhiLian.common.core.annotation.Excel;
|
||||
import com.zhiLian.common.core.web.domain.BaseEntity;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
|
||||
/**
|
||||
* 多数据源对象 entinfo
|
||||
*
|
||||
* @author muyu
|
||||
* @date 2024-06-06
|
||||
*/
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("entinfo")
|
||||
public class Entinfo extends BaseEntity
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 数据源key */
|
||||
@Excel(name = "数据源key")
|
||||
private String entCode;
|
||||
|
||||
/** 数据源ip */
|
||||
@Excel(name = "数据源ip")
|
||||
private String ip;
|
||||
|
||||
/** 数据源端口 */
|
||||
@Excel(name = "数据源端口")
|
||||
private Integer port;
|
||||
|
||||
/** 数据源ID */
|
||||
private Long id;
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.zhiLian.business.mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.zhiLian.business.domain.Entinfo;
|
||||
|
||||
/**
|
||||
* 多数据源Mapper接口
|
||||
*
|
||||
* @author muyu
|
||||
* @date 2024-06-06
|
||||
*/
|
||||
public interface EntinfoMapper extends BaseMapper<Entinfo>
|
||||
{
|
||||
/**
|
||||
* 查询多数据源
|
||||
*
|
||||
* @param id 多数据源主键
|
||||
* @return 多数据源
|
||||
*/
|
||||
public Entinfo selectEntinfoById(Long id);
|
||||
|
||||
/**
|
||||
* 查询多数据源列表
|
||||
*
|
||||
* @param entinfo 多数据源
|
||||
* @return 多数据源集合
|
||||
*/
|
||||
public List<Entinfo> selectEntinfoList(Entinfo entinfo);
|
||||
|
||||
/**
|
||||
* 新增多数据源
|
||||
*
|
||||
* @param entinfo 多数据源
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertEntinfo(Entinfo entinfo);
|
||||
|
||||
/**
|
||||
* 修改多数据源
|
||||
*
|
||||
* @param entinfo 多数据源
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateEntinfo(Entinfo entinfo);
|
||||
|
||||
/**
|
||||
* 删除多数据源
|
||||
*
|
||||
* @param id 多数据源主键
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteEntinfoById(Long id);
|
||||
|
||||
/**
|
||||
* 批量删除多数据源
|
||||
*
|
||||
* @param ids 需要删除的数据主键集合
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteEntinfoByIds(Long[] ids);
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.zhiLian.business.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.zhiLian.business.domain.Entinfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 多数据源Service接口
|
||||
*
|
||||
* @author muyu
|
||||
* @date 2024-06-06
|
||||
*/
|
||||
public interface IEntinfoService extends IService<Entinfo>
|
||||
{
|
||||
/**
|
||||
* 查询多数据源
|
||||
*
|
||||
* @param id 多数据源主键
|
||||
* @return 多数据源
|
||||
*/
|
||||
public Entinfo selectEntinfoById(Long id);
|
||||
|
||||
/**
|
||||
* 查询多数据源列表
|
||||
*
|
||||
* @param entinfo 多数据源
|
||||
* @return 多数据源集合
|
||||
*/
|
||||
public List<Entinfo> selectEntinfoList(Entinfo entinfo);
|
||||
|
||||
/**
|
||||
* 新增多数据源
|
||||
*
|
||||
* @param entinfo 多数据源
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertEntinfo(Entinfo entinfo);
|
||||
|
||||
/**
|
||||
* 修改多数据源
|
||||
*
|
||||
* @param entinfo 多数据源
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateEntinfo(Entinfo entinfo);
|
||||
|
||||
/**
|
||||
* 批量删除多数据源
|
||||
*
|
||||
* @param ids 需要删除的多数据源主键集合
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteEntinfoByIds(Long[] ids);
|
||||
|
||||
/**
|
||||
* 删除多数据源信息
|
||||
*
|
||||
* @param id 多数据源主键
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteEntinfoById(Long id);
|
||||
}
|
|
@ -3,13 +3,16 @@ package com.zhiLian.business.service.impl;
|
|||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.zhiLian.business.domain.AuthRoleResp;
|
||||
|
||||
import com.zhiLian.business.domain.Business;
|
||||
|
||||
import com.zhiLian.business.domain.Entinfo;
|
||||
import com.zhiLian.business.mapper.BusinessMapper;
|
||||
import com.zhiLian.business.remote.factory.RemoteUserLoginFactory;
|
||||
import com.zhiLian.business.service.IBusinessService;
|
||||
import com.zhiLian.common.core.domain.Result;
|
||||
import com.zhiLian.common.core.utils.DateUtils;
|
||||
import com.zhiLian.common.redis.service.RedisService;
|
||||
import com.zhiLian.common.security.utils.SecurityUtils;
|
||||
import com.zhiLian.common.system.domain.LoginUser;
|
||||
import com.zhiLian.common.system.domain.SysUser;
|
||||
|
@ -29,6 +32,7 @@ import java.net.ProtocolException;
|
|||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 企业Service业务层处理
|
||||
|
@ -47,7 +51,7 @@ public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business>
|
|||
private RemoteUserService remoteUserService;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String,String> redisTemplate;
|
||||
private RedisService redisService;
|
||||
|
||||
/**
|
||||
* 查询企业
|
||||
|
@ -71,7 +75,6 @@ public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business>
|
|||
@Override
|
||||
public List<Business> selectBusinessList(Business business)
|
||||
{
|
||||
|
||||
LoginUser loginUser = SecurityUtils.getLoginUser();
|
||||
SysUser user = remoteUserService.selectByUserId(loginUser.getUserid());
|
||||
if (user.getUserType().equals("00")) {
|
||||
|
@ -122,17 +125,25 @@ public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business>
|
|||
httpConnectMysql(business);
|
||||
return businessMapper.updateBusiness(business);
|
||||
}
|
||||
@Autowired
|
||||
private EntinfoServiceImpl entinfoService;
|
||||
|
||||
/**
|
||||
* 判断redis是否存在端口号
|
||||
* @param business
|
||||
*/
|
||||
public void httpConnectMysql(Business business){
|
||||
redisService.deleteObject("entinfo");
|
||||
List<Entinfo> list = entinfoService.list();
|
||||
redisService.setCacheList("entinfo",list);
|
||||
if (business.getBusinessStates().equals(2) ) {
|
||||
if (!redisTemplate.hasKey(business.getId()+business.getName())){
|
||||
redisTemplate.opsForValue().set(business.getId()+business.getName(),String.valueOf(3306+business.getId()));
|
||||
extracted(business);
|
||||
}
|
||||
Entinfo build = Entinfo.builder()
|
||||
.entCode("test_" + business.getId())
|
||||
.ip("192.168.120.128")
|
||||
.port(Integer.valueOf(3306 + Integer.valueOf(String.valueOf(business.getId())))).build();
|
||||
entinfoService.insertEntinfo(build);
|
||||
// redisService.setCacheObject(String.valueOf(business.getId()),JSON.toJSONString(build));
|
||||
extracted(business);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -142,11 +153,12 @@ public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business>
|
|||
* @param business
|
||||
*/
|
||||
private static void extracted(Business business) {
|
||||
String postUrl="http://122.51.111.225:10006/webhook/%E6%96%B0%E5%BB%BA%E4%BC%81%E4%B8%9A%E6%95%B0%E6%8D%AE%E6%BA%90";
|
||||
String postUrl="http://192.168.120.128/webhook/%E6%96%B0%E5%BB%BAmysql%E6%9C%8D%E5%8A%A1";
|
||||
HashMap<String, String> hashMap = new HashMap<>();
|
||||
hashMap.put("businessId", business.getId()+ business.getName());
|
||||
hashMap.put("businessId",business.getId()+ business.getName());
|
||||
hashMap.put("mysqlPort",String.valueOf(3306+ business.getId()));
|
||||
String json = JSON.toJSONString(hashMap);
|
||||
|
||||
// 3.创建连接与设置连接参数
|
||||
URL urlObj = null;
|
||||
try {
|
||||
|
@ -178,6 +190,7 @@ public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business>
|
|||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,96 @@
|
|||
package com.zhiLian.business.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.zhiLian.business.domain.Entinfo;
|
||||
import com.zhiLian.business.mapper.EntinfoMapper;
|
||||
import com.zhiLian.business.service.IEntinfoService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 多数据源Service业务层处理
|
||||
*
|
||||
* @author muyu
|
||||
* @date 2024-06-06
|
||||
*/
|
||||
@Service
|
||||
public class EntinfoServiceImpl extends ServiceImpl<EntinfoMapper, Entinfo>
|
||||
implements IEntinfoService
|
||||
{
|
||||
@Autowired
|
||||
private EntinfoMapper entinfoMapper;
|
||||
|
||||
/**
|
||||
* 查询多数据源
|
||||
*
|
||||
* @param id 多数据源主键
|
||||
* @return 多数据源
|
||||
*/
|
||||
@Override
|
||||
public Entinfo selectEntinfoById(Long id)
|
||||
{
|
||||
return entinfoMapper.selectEntinfoById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询多数据源列表
|
||||
*
|
||||
* @param entinfo 多数据源
|
||||
* @return 多数据源
|
||||
*/
|
||||
@Override
|
||||
public List<Entinfo> selectEntinfoList(Entinfo entinfo)
|
||||
{
|
||||
return entinfoMapper.selectEntinfoList(entinfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增多数据源
|
||||
*
|
||||
* @param entinfo 多数据源
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int insertEntinfo(Entinfo entinfo)
|
||||
{
|
||||
return entinfoMapper.insertEntinfo(entinfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改多数据源
|
||||
*
|
||||
* @param entinfo 多数据源
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int updateEntinfo(Entinfo entinfo)
|
||||
{
|
||||
return entinfoMapper.updateEntinfo(entinfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除多数据源
|
||||
*
|
||||
* @param ids 需要删除的多数据源主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteEntinfoByIds(Long[] ids)
|
||||
{
|
||||
return entinfoMapper.deleteEntinfoByIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除多数据源信息
|
||||
*
|
||||
* @param id 多数据源主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteEntinfoById(Long id)
|
||||
{
|
||||
return entinfoMapper.deleteEntinfoById(id);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
<?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.zhiLian.business.mapper.EntinfoMapper">
|
||||
|
||||
<resultMap type="com.zhiLian.business.domain.Entinfo" id="EntinfoResult">
|
||||
<result property="entCode" column="ent_code" />
|
||||
<result property="ip" column="ip" />
|
||||
<result property="port" column="port" />
|
||||
<result property="id" column="id" />
|
||||
</resultMap>
|
||||
|
||||
<sql id="selectEntinfoVo">
|
||||
select ent_code, ip, port, id from entinfo
|
||||
</sql>
|
||||
|
||||
<select id="selectEntinfoList" parameterType="com.zhiLian.business.domain.Entinfo" resultMap="EntinfoResult">
|
||||
<include refid="selectEntinfoVo"/>
|
||||
<where>
|
||||
<if test="entCode != null and entCode != ''"> and ent_code = #{entCode}</if>
|
||||
<if test="ip != null and ip != ''"> and ip = #{ip}</if>
|
||||
<if test="port != null "> and port = #{port}</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<select id="selectEntinfoById" parameterType="Long" resultMap="EntinfoResult">
|
||||
<include refid="selectEntinfoVo"/>
|
||||
where id = #{id}
|
||||
</select>
|
||||
|
||||
<insert id="insertEntinfo" parameterType="com.zhiLian.business.domain.Entinfo" useGeneratedKeys="true" keyProperty="id">
|
||||
insert into entinfo
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="entCode != null">ent_code,</if>
|
||||
<if test="ip != null">ip,</if>
|
||||
<if test="port != null">port,</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="entCode != null">#{entCode},</if>
|
||||
<if test="ip != null">#{ip},</if>
|
||||
<if test="port != null">#{port},</if>
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<update id="updateEntinfo" parameterType="com.zhiLian.business.domain.Entinfo">
|
||||
update entinfo
|
||||
<trim prefix="SET" suffixOverrides=",">
|
||||
<if test="entCode != null">ent_code = #{entCode},</if>
|
||||
<if test="ip != null">ip = #{ip},</if>
|
||||
<if test="port != null">port = #{port},</if>
|
||||
</trim>
|
||||
where id = #{id}
|
||||
</update>
|
||||
|
||||
<delete id="deleteEntinfoById" parameterType="Long">
|
||||
delete from entinfo where id = #{id}
|
||||
</delete>
|
||||
|
||||
<delete id="deleteEntinfoByIds" parameterType="String">
|
||||
delete from entinfo where id in
|
||||
<foreach item="id" collection="array" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
</delete>
|
||||
</mapper>
|
|
@ -241,7 +241,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
|||
// 新增用户岗位关联
|
||||
insertUserPost(user);
|
||||
// 新增用户与角色管理
|
||||
if (user.getRoleId()==null&& user.getRoleId().equals("")){
|
||||
if (null == user.getRoleId()|| user.getRoleId().equals("")){
|
||||
user.setRoleId(Long.valueOf(2));
|
||||
insertUserRole(user);
|
||||
return rows;
|
||||
|
|
|
@ -190,8 +190,8 @@
|
|||
insert into sys_user(
|
||||
<if test="userId != null and userId != 0">user_id,</if>
|
||||
<if test="deptId != null and deptId != 0">dept_id,</if>
|
||||
<if test="userType != null and userType != 0">user_type,</if>
|
||||
<if test="userName != null and userName != ''">user_name,</if>
|
||||
<if test="userType != null and userType != 0">user_type,</if>
|
||||
<if test="nickName != null and nickName != ''">nick_name,</if>
|
||||
<if test="email != null and email != ''">email,</if>
|
||||
<if test="avatar != null and avatar != ''">avatar,</if>
|
||||
|
|
|
@ -1,14 +1,23 @@
|
|||
package com.zhiLian.vehicle.datasource;
|
||||
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.zhiLian.common.redis.service.RedisService;
|
||||
import com.zhiLian.common.system.remote.RemoteUserService;
|
||||
import com.zhiLian.vehicle.datasource.config.factory.DruidDataSourceFactory;
|
||||
import com.zhiLian.vehicle.datasource.config.role.DynamicDataSource;
|
||||
import com.zhiLian.vehicle.datasource.domain.DataSourceInfo;
|
||||
import com.zhiLian.vehicle.datasource.domain.EntInfo;
|
||||
import com.zhiLian.vehicle.datasource.domain.Entinfo;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import springfox.documentation.spring.web.json.Json;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
@ -16,33 +25,46 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 数据源配置
|
||||
* @Date 2023-8-1 上午 11:05
|
||||
* BingRui.Hou
|
||||
* 数据源配置
|
||||
*/
|
||||
@Log4j2
|
||||
@Configuration
|
||||
@Component
|
||||
@AutoConfigureBefore(RedisService.class)
|
||||
public class ManyDataSource {
|
||||
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
private List<EntInfo> dataSourceInfoList(){
|
||||
List<EntInfo> databaseNameList = new ArrayList<>(){{
|
||||
add(EntInfo.builder()
|
||||
.entCode("test_1")
|
||||
@Autowired
|
||||
private RedisTemplate<String,String> redisTemplate;
|
||||
|
||||
|
||||
@Lazy
|
||||
private List<Entinfo> dataSourceInfoList(){
|
||||
List<Entinfo> databaseNameList = new ArrayList<>(){{
|
||||
add(Entinfo.builder()
|
||||
.entCode("test_00")
|
||||
.ip("192.168.120.128")
|
||||
.port(3306)
|
||||
.build());
|
||||
add(EntInfo.builder()
|
||||
.entCode("test_2")
|
||||
.ip("122.51.111.225")
|
||||
.port(6666)
|
||||
.build());
|
||||
add(EntInfo.builder()
|
||||
.entCode("test_3")
|
||||
.ip("122.51.111.225")
|
||||
.port(3333)
|
||||
.build());
|
||||
}};
|
||||
List<String> entinfo = redisTemplate.opsForList().range("entinfo", 0, -1);
|
||||
|
||||
entinfo.forEach(string -> {
|
||||
Entinfo entInfo = JSON.parseObject(string, Entinfo.class);
|
||||
databaseNameList.add(entInfo);
|
||||
});
|
||||
// if(SecurityUtils.getLoginUser() == null){
|
||||
// return databaseNameList;
|
||||
// }else{
|
||||
// Long storeId = SecurityUtils.getLoginUser().getUserid();
|
||||
// SysUser sysUser = remoteUserService.selectByUserId(storeId);
|
||||
// String s = redisService.getCacheObject(String.valueOf(sysUser.getUserType()));
|
||||
// EntInfo entInfo = JSON.parseObject(s, EntInfo.class);
|
||||
// databaseNameList.add(entInfo);
|
||||
// return databaseNameList;
|
||||
// }
|
||||
return databaseNameList;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,17 +1,22 @@
|
|||
package com.zhiLian.vehicle.datasource.config;
|
||||
|
||||
|
||||
import com.zhiLian.common.security.utils.SecurityUtils;
|
||||
import com.zhiLian.common.system.domain.SysUser;
|
||||
import com.zhiLian.common.system.remote.RemoteUserService;
|
||||
import com.zhiLian.vehicle.datasource.config.holder.DynamicDataSourceHolder;
|
||||
import org.aspectj.lang.annotation.After;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 数据源切面
|
||||
* @Date 2023-8-2 下午 08:26
|
||||
* BingRui.Hou
|
||||
* 数据源切面
|
||||
*
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
|
@ -21,14 +26,24 @@ public class DataSourceAsp {
|
|||
public void pointcut () {
|
||||
}
|
||||
|
||||
@Lazy
|
||||
@Autowired
|
||||
private RemoteUserService remoteUserService;
|
||||
|
||||
|
||||
/**
|
||||
* 的每一個方法執行之前 執行的處理
|
||||
*/
|
||||
@Before("pointcut()")
|
||||
public void beforeMethod() {
|
||||
// EntInfo build = EntInfo.builder()
|
||||
// .entCode("test_" + 12)
|
||||
// .ip("192.168.120.128")
|
||||
// .port(Integer.valueOf(3307)).build();
|
||||
// redisService.setCacheObject(String.valueOf(12), JSON.toJSONString(build));
|
||||
Long storeId = SecurityUtils.getLoginUser().getUserid();
|
||||
DynamicDataSourceHolder.setDynamicDataSourceKey("test_"+storeId);
|
||||
SysUser sysUser = remoteUserService.selectByUserId(storeId);
|
||||
DynamicDataSourceHolder.setDynamicDataSourceKey("test_"+sysUser.getUserType());
|
||||
}
|
||||
/**
|
||||
* 的每一個方法執行之后 執行的處理
|
||||
|
|
|
@ -14,7 +14,7 @@ import java.util.Map;
|
|||
* 动态数据源
|
||||
* 调用AddDefineDataSource组件的addDefineDynamicDataSource()方法,获取原来targetdatasources的map,并将新的数据源信息添加到map中,并替换targetdatasources中的map
|
||||
* 切换数据源时可以使用@DataSource(value = "数据源名称"),或者DynamicDataSourceContextHolder.setContextKey("数据源名称")
|
||||
* @author Dongzl
|
||||
* @author BingRui.Hou
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package com.zhiLian.vehicle.datasource.contents;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 数据源常量
|
||||
* @Date 2023-8-1 上午 11:02
|
||||
* BingRui.Hou
|
||||
* 数据源常量
|
||||
*
|
||||
*/
|
||||
public class DatasourceContent {
|
||||
|
||||
|
|
|
@ -11,9 +11,9 @@ import static com.zhiLian.vehicle.datasource.contents.DatasourceContent.*;
|
|||
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 数据源实体类
|
||||
* @Date 2023-8-1 上午 11:15
|
||||
* BingRui.Hou
|
||||
* 数据源实体类
|
||||
*
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
package com.zhiLian.vehicle.datasource.domain;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* BingRui.Hou
|
||||
*
|
||||
* @Description 描述
|
||||
* @ClassName EntInfo
|
||||
* @Date 2024/06/04 14:34
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class EntInfo {
|
||||
private String entCode;
|
||||
|
||||
//ip
|
||||
private String ip;
|
||||
|
||||
//端口
|
||||
private Integer port;
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.zhiLian.vehicle.datasource.domain;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.zhiLian.common.core.annotation.Excel;
|
||||
import com.zhiLian.common.core.web.domain.BaseEntity;
|
||||
import lombok.*;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* BingRui.Hou
|
||||
*
|
||||
* @Description 描述
|
||||
* EntInfo
|
||||
* @Date 2024/06/04 14:34
|
||||
*/
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class Entinfo extends BaseEntity implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 数据源key */
|
||||
@Excel(name = "数据源key")
|
||||
private String entCode;
|
||||
|
||||
/** 数据源ip */
|
||||
@Excel(name = "数据源ip")
|
||||
private String ip;
|
||||
|
||||
/** 数据源端口 */
|
||||
@Excel(name = "数据源端口")
|
||||
private Integer port;
|
||||
|
||||
/** 数据源ID */
|
||||
private Long id;
|
||||
|
||||
|
||||
}
|
|
@ -13,9 +13,9 @@ import javax.annotation.Resource;
|
|||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Description: 数据源工具类
|
||||
* @Author Dongzl
|
||||
* @Date 2022/8/18 17:20
|
||||
* 数据源工具类
|
||||
* BingRui.Hou
|
||||
*
|
||||
*
|
||||
*/
|
||||
@Slf4j
|
||||
|
|
|
@ -59,8 +59,8 @@ public class Vehicle extends BaseEntity
|
|||
private Long batteryNumber;
|
||||
|
||||
/** 企业ID */
|
||||
@Excel(name = "企业ID")
|
||||
private Long businessId;
|
||||
// @Excel(name = "企业ID")
|
||||
// private Long businessId;
|
||||
|
||||
@Excel(name="围栏组ID")
|
||||
private Long groupId;
|
||||
|
|
|
@ -66,7 +66,7 @@ public class VehicleServiceImpl extends ServiceImpl<VehicleMapper, Vehicle>
|
|||
if (user.getUserType().equals("00")) {
|
||||
return vehicleMapper.selectVehicleList(vehicle);
|
||||
}
|
||||
vehicle.setBusinessId(Long.valueOf(user.getUserType()));
|
||||
// vehicle.setBusinessId(Long.valueOf(user.getUserType()));
|
||||
List<Vehicle> vehicles = vehicleMapper.selectVehicleList(vehicle);
|
||||
// vehicles.forEach(vehicle1 -> {
|
||||
// Result result = remoteFileService.getInfo(vehicle1.getId());
|
||||
|
@ -86,9 +86,9 @@ public class VehicleServiceImpl extends ServiceImpl<VehicleMapper, Vehicle>
|
|||
@Override
|
||||
public int insertVehicle(Vehicle vehicle)
|
||||
{
|
||||
LoginUser loginUser = SecurityUtils.getLoginUser();
|
||||
SysUser user = remoteUserService.selectByUserId(loginUser.getUserid());
|
||||
vehicle.setBusinessId(Long.valueOf(user.getUserType()));
|
||||
// LoginUser loginUser = SecurityUtils.getLoginUser();
|
||||
// SysUser user = remoteUserService.selectByUserId(loginUser.getUserid());
|
||||
// vehicle.setBusinessId(Long.valueOf(user.getUserType()));
|
||||
vehicle.setCreateTime(DateUtils.getNowDate());
|
||||
return vehicleMapper.insertVehicle(vehicle);
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ public class VehicleServiceImpl extends ServiceImpl<VehicleMapper, Vehicle>
|
|||
{
|
||||
LoginUser loginUser = SecurityUtils.getLoginUser();
|
||||
SysUser user = remoteUserService.selectByUserId(loginUser.getUserid());
|
||||
vehicle.setBusinessId(Long.valueOf(user.getUserType()));
|
||||
// vehicle.setBusinessId(Long.valueOf(user.getUserType()));
|
||||
vehicle.setCreateTime(DateUtils.getNowDate());
|
||||
vehicle.setUpdateTime(DateUtils.getNowDate());
|
||||
return vehicleMapper.updateVehicle(vehicle);
|
||||
|
|
|
@ -4,6 +4,8 @@ server:
|
|||
|
||||
# Spring
|
||||
spring:
|
||||
main:
|
||||
allow-circular-references: true
|
||||
application:
|
||||
# 应用名称
|
||||
name: zhiLian-vehicle
|
||||
|
|
|
@ -13,7 +13,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<result property="battery" column="battery" />
|
||||
<result property="motorNumber" column="motor_number" />
|
||||
<result property="batteryNumber" column="battery_number" />
|
||||
<result property="businessId" column="business_id" />
|
||||
<!-- <result property="businessId" column="business_id" />-->
|
||||
<result property="remark" column="remark" />
|
||||
<result property="createBy" column="create_by" />
|
||||
<result property="createTime" column="create_time" />
|
||||
|
@ -23,7 +23,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
</resultMap>
|
||||
|
||||
<sql id="selectVehicleVo">
|
||||
select id, number,group_id, type_id, electonic_id, motor, battery, motor_number, battery_number, business_id, remark, create_by, create_time, update_by, update_time from vehicle
|
||||
select id, number,group_id, type_id, electonic_id, motor, battery, motor_number, battery_number, remark, create_by, create_time, update_by, update_time from vehicle
|
||||
</sql>
|
||||
|
||||
<select id="selectVehicleList" parameterType="com.zhiLian.vehicle.domain.Vehicle" resultMap="VehicleResult">
|
||||
|
@ -37,7 +37,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<if test="battery != null and battery != ''"> and battery = #{battery}</if>
|
||||
<if test="motorNumber != null "> and motor_number = #{motorNumber}</if>
|
||||
<if test="batteryNumber != null "> and battery_number = #{batteryNumber}</if>
|
||||
<if test="businessId != null "> and business_id = #{businessId}</if>
|
||||
<!-- <if test="businessId != null "> and business_id = #{businessId}</if>-->
|
||||
<if test="groupId != null "> and group_id = #{groupId}</if>
|
||||
</where>
|
||||
</select>
|
||||
|
@ -58,7 +58,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<if test="battery != null">battery,</if>
|
||||
<if test="motorNumber != null">motor_number,</if>
|
||||
<if test="batteryNumber != null">battery_number,</if>
|
||||
<if test="businessId != null">business_id,</if>
|
||||
<!-- <if test="businessId != null">business_id,</if>-->
|
||||
<if test="remark != null">remark,</if>
|
||||
<if test="createBy != null">create_by,</if>
|
||||
<if test="createTime != null">create_time,</if>
|
||||
|
@ -75,7 +75,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<if test="battery != null">#{battery},</if>
|
||||
<if test="motorNumber != null">#{motorNumber},</if>
|
||||
<if test="batteryNumber != null">#{batteryNumber},</if>
|
||||
<if test="businessId != null">#{businessId},</if>
|
||||
<!-- <if test="businessId != null">#{businessId},</if>-->
|
||||
<if test="remark != null">#{remark},</if>
|
||||
<if test="createBy != null">#{createBy},</if>
|
||||
<if test="createTime != null">#{createTime},</if>
|
||||
|
@ -95,7 +95,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<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="businessId != null">business_id = #{businessId},</if>
|
||||
<!-- <if test="businessId != null">business_id = #{businessId},</if>-->
|
||||
<if test="remark != null">remark = #{remark},</if>
|
||||
<if test="createBy != null">create_by = #{createBy},</if>
|
||||
<if test="createTime != null">create_time = #{createTime},</if>
|
||||
|
|
Loading…
Reference in New Issue