Browse Source

数据源管理

fenze 7 months ago
parent
commit
028241c492

+ 193 - 0
zgztBus/jeecg-module-lesm/src/main/java/org/jeecg/modules/datasourceConfig/controller/DatasourceConfigController.java

@@ -0,0 +1,193 @@
+package org.jeecg.modules.datasourceConfig.controller;
+
+
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.jeecg.common.api.vo.Result;
+import org.jeecg.common.aspect.annotation.AutoLog;
+import org.jeecg.common.system.base.controller.JeecgController;
+import org.jeecg.common.system.query.QueryGenerator;
+import org.jeecg.modules.datasourceConfig.entity.DatasourceConfig;
+import org.jeecg.modules.datasourceConfig.entity.GetDataParam;
+import org.jeecg.modules.datasourceConfig.service.DatasourceConfigService;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * (DatasourceConfig)数据源配置
+ *
+ * @author makejava
+ * @since 2024-10-21 09:46:48
+ */
+@Api(tags="数据源配置")
+@RestController
+@RequestMapping("/datasourceConfig")
+@Slf4j
+public class DatasourceConfigController extends JeecgController<DatasourceConfig, DatasourceConfigService> {
+    /**
+     * 服务对象
+     */
+    @Resource
+    private DatasourceConfigService datasourceConfigService;
+
+    /**
+     * 分页查询所有数据
+     *
+     * @param datasourceConfig 查询实体
+     * @return 所有数据
+     */
+    //@AutoLog(value = "数据源配置表-分页列表查询")
+    @ApiOperation(value="数据源配置-分页列表查询", notes="数据源配置-分页列表查询")
+    @GetMapping(value = "/list")
+    public Result<IPage<DatasourceConfig>> selectAll(DatasourceConfig datasourceConfig,
+                                                     @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
+                                                     @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
+                                                     HttpServletRequest req) {
+        QueryWrapper<DatasourceConfig> queryWrapper = QueryGenerator.initQueryWrapper(datasourceConfig, req.getParameterMap());
+        Page<DatasourceConfig> page = new Page<DatasourceConfig>(pageNo, pageSize);
+        IPage<DatasourceConfig> pageList = datasourceConfigService.page(page, queryWrapper);
+        return Result.OK(pageList);
+    }
+    
+    /**
+     *   添加
+     *
+     * @param datasourceConfig
+     * @return
+     */
+    @AutoLog(value = "数据源配置表-添加")
+    @ApiOperation(value="数据源配置表-添加", notes="数据源配置表-添加")
+    @PostMapping(value = "/add")
+    public Result<String> add(@RequestBody DatasourceConfig datasourceConfig) {
+        datasourceConfigService.save(datasourceConfig);
+        return Result.OK("添加成功!");
+    }
+
+    /**
+     *  编辑
+     *
+     * @param datasourceConfig
+     * @return
+     */
+    @AutoLog(value = "数据源配置表-编辑")
+    @ApiOperation(value="数据源配置表-编辑", notes="数据源配置表-编辑")
+    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
+    public Result<String> edit(@RequestBody DatasourceConfig datasourceConfig) {
+        datasourceConfigService.updateById(datasourceConfig);
+        return Result.OK("编辑成功!");
+    }
+
+    /**
+     *   通过id删除
+     *
+     * @param id
+     * @return
+     */
+    @AutoLog(value = "数据源配置表-通过id删除")
+    @ApiOperation(value="数据源配置表-通过id删除", notes="数据源配置表-通过id删除")
+    @DeleteMapping(value = "/delete")
+    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
+        datasourceConfigService.removeById(id);
+        return Result.OK("删除成功!");
+    }
+
+    /**
+     *  批量删除
+     *
+     * @param ids
+     * @return
+     */
+    @AutoLog(value = "数据源配置表-批量删除")
+    @ApiOperation(value="数据源配置表-批量删除", notes="数据源配置表-批量删除")
+    @DeleteMapping(value = "/deleteBatch")
+    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
+        this.datasourceConfigService.removeByIds(Arrays.asList(ids.split(",")));
+        return Result.OK("批量删除成功!");
+    }
+
+    /**
+     * 通过id查询
+     *
+     * @param id
+     * @return
+     */
+    //@AutoLog(value = "数据源配置表-通过id查询")
+    @ApiOperation(value="数据源配置表-通过id查询", notes="数据源配置表-通过id查询")
+    @GetMapping(value = "/queryById")
+    public Result<DatasourceConfig> queryById(@RequestParam(name="id",required=true) String id) {
+        DatasourceConfig datasourceConfig = datasourceConfigService.getById(id);
+        if(datasourceConfig==null) {
+            return Result.error("未找到对应数据");
+        }
+        return Result.OK(datasourceConfig);
+    }
+
+    /**
+     * 导出excel
+     *
+     * @param request
+     * @param datasourceConfig
+     */
+    @RequestMapping(value = "/exportXls")
+    public ModelAndView exportXls(HttpServletRequest request, DatasourceConfig datasourceConfig) {
+		return super.exportXls(request, datasourceConfig, DatasourceConfig.class, "数据源配置表");
+    }
+    
+
+    /**
+     * 通过excel导入数据
+     *
+     * @param request
+     * @param response
+     * @return
+     */
+    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
+    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
+        return super.importExcel(request, response, DatasourceConfig.class);
+    }
+
+    /**
+     * 连接测试
+     *
+     * @param datasourceConfig
+     * @return
+     */
+    //@AutoLog(value = "数据源配置表-通过id查询")
+    @ApiOperation(value="数据源配置表-连接测试", notes="数据源配置表-连接测试")
+    @PostMapping(value = "/testConn")
+    public Result<String> testConn(@RequestBody DatasourceConfig datasourceConfig) {
+        Boolean testConn = datasourceConfigService.testConn(datasourceConfig);
+        if(testConn){
+            return Result.OK("连接成功");
+        }else {
+            return Result.error("连接失败");
+        }
+    }
+
+
+    /**
+     * 通过数据源获取数据
+     *
+     * @param getDataParam
+     * @return
+     */
+    @ApiOperation(value="数据源配置表-通过数据源获取数据", notes="数据源配置表-通过数据源获取数据")
+    @GetMapping(value = "/getDataByDatasourceConfig")
+    public Result<List<Map>> getDataByDatasourceConfig(GetDataParam getDataParam){
+        return Result.OK(datasourceConfigService.getDataByDatasourceConfig(getDataParam));
+    }
+
+}
+

+ 79 - 0
zgztBus/jeecg-module-lesm/src/main/java/org/jeecg/modules/datasourceConfig/entity/DatasourceConfig.java

@@ -0,0 +1,79 @@
+package org.jeecg.modules.datasourceConfig.entity;
+
+import java.util.Date;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.baomidou.mybatisplus.extension.activerecord.Model;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+import org.jeecg.common.aspect.annotation.Dict;
+import org.jeecgframework.poi.excel.annotation.Excel;
+import org.springframework.format.annotation.DateTimeFormat;
+
+import java.io.Serializable;
+
+
+@Data
+@TableName("datasource_config")
+@Accessors(chain = true)
+@EqualsAndHashCode(callSuper = false)
+@ApiModel(value="数据源配置表", description="数据源配置表")
+public class DatasourceConfig extends Model<DatasourceConfig> {
+
+    /**主键*/
+    @TableId(type = IdType.ASSIGN_ID)
+    @ApiModelProperty(value = "主键")
+    private String id;
+    /**创建人*/
+    @ApiModelProperty(value = "创建人")
+    private String createBy;
+    /**创建日期*/
+    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
+    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
+    @ApiModelProperty(value = "创建日期")
+    private Date createTime;
+    /**更新人*/
+    @ApiModelProperty(value = "更新人")
+    private String updateBy;
+    /**更新日期*/
+    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
+    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
+    @ApiModelProperty(value = "更新日期")
+    private Date updateTime;
+    /**所属部门*/
+    @ApiModelProperty(value = "所属部门")
+    private String sysOrgCode;
+    /**名称*/
+    @Excel(name = "数据源名称", width = 15)
+    @ApiModelProperty(value = "数据源名称")
+    private String name;
+    /**数据库类型*/
+    @Excel(name = "数据库类型", width = 15)
+    @ApiModelProperty(value = "数据库类型")
+    @Dict(dicCode = "datasource_type")
+    private String type;
+    /**数据源地址*/
+    @Excel(name = "数据源地址", width = 15)
+    @ApiModelProperty(value = "数据源地址")
+    private String url;
+    /**用户名*/
+    @Excel(name = "用户名", width = 15)
+    @ApiModelProperty(value = "用户名")
+    private String username;
+    /**密码*/
+    @Excel(name = "密码", width = 15)
+    @ApiModelProperty(value = "密码")
+    private String password;
+    /**备注*/
+    @Excel(name = "备注", width = 15)
+    @ApiModelProperty(value = "备注")
+    private String remark;
+
+}
+

+ 23 - 0
zgztBus/jeecg-module-lesm/src/main/java/org/jeecg/modules/datasourceConfig/entity/GetDataParam.java

@@ -0,0 +1,23 @@
+package org.jeecg.modules.datasourceConfig.entity;
+
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+
+@Data
+@Accessors(chain = true)
+@EqualsAndHashCode(callSuper = false)
+@ApiModel(value="通过数据源获取数据参数", description="通过数据源获取数据参数")
+public class GetDataParam {
+
+    @ApiModelProperty(value = "数据源配置id")
+    private String dataSourceConfigId;
+
+    @ApiModelProperty(value = "执行sql语句")
+    private String sql;
+
+}

+ 15 - 0
zgztBus/jeecg-module-lesm/src/main/java/org/jeecg/modules/datasourceConfig/mapper/DatasourceConfigMapper.java

@@ -0,0 +1,15 @@
+package org.jeecg.modules.datasourceConfig.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.jeecg.modules.datasourceConfig.entity.DatasourceConfig;
+
+/**
+ * (DatasourceConfig)表数据库访问层
+ *
+ * @author makejava
+ * @since 2024-10-21 09:46:52
+ */
+public interface DatasourceConfigMapper extends BaseMapper<DatasourceConfig> {
+
+}
+

+ 5 - 0
zgztBus/jeecg-module-lesm/src/main/java/org/jeecg/modules/datasourceConfig/mapper/xml/DatasourceConfigMapper.xml

@@ -0,0 +1,5 @@
+<?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="org.jeecg.modules.datasourceConfig.mapper.DatasourceConfigMapper">
+
+</mapper>

+ 26 - 0
zgztBus/jeecg-module-lesm/src/main/java/org/jeecg/modules/datasourceConfig/service/DatasourceConfigService.java

@@ -0,0 +1,26 @@
+package org.jeecg.modules.datasourceConfig.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import org.jeecg.modules.datasourceConfig.entity.DatasourceConfig;
+import org.jeecg.modules.datasourceConfig.entity.GetDataParam;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * (DatasourceConfig)表服务接口
+ *
+ * @author makejava
+ * @since 2024-10-21 09:46:56
+ */
+public interface DatasourceConfigService extends IService<DatasourceConfig> {
+
+    //测试链接
+    public Boolean testConn(DatasourceConfig datasourceConfig);
+
+
+    //通过数据源获取数据
+    public List<Map> getDataByDatasourceConfig(GetDataParam getDataParam);
+
+}
+

+ 118 - 0
zgztBus/jeecg-module-lesm/src/main/java/org/jeecg/modules/datasourceConfig/service/impl/DatasourceConfigServiceImpl.java

@@ -0,0 +1,118 @@
+package org.jeecg.modules.datasourceConfig.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.jeecg.modules.datasourceConfig.entity.GetDataParam;
+import org.jeecg.modules.datasourceConfig.mapper.DatasourceConfigMapper;
+import org.jeecg.modules.datasourceConfig.entity.DatasourceConfig;
+import org.jeecg.modules.datasourceConfig.service.DatasourceConfigService;
+import org.springframework.stereotype.Service;
+
+import java.sql.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * (DatasourceConfig)表服务实现类
+ *
+ * @author makejava
+ * @since 2024-10-21 09:46:58
+ */
+@Service("datasourceConfigService")
+public class DatasourceConfigServiceImpl extends ServiceImpl<DatasourceConfigMapper, DatasourceConfig> implements DatasourceConfigService {
+
+
+    //测试链接
+    @Override
+    public Boolean testConn(DatasourceConfig datasourceConfig) {
+        Connection connection=null;
+        PreparedStatement preparedStatement=null;
+        try {
+            //1、导入驱动jar包
+            //2、注册驱动
+            if(datasourceConfig.getType().equals("0")){
+                Class.forName("com.mysql.cj.jdbc.Driver");
+            }else{
+                Class.forName("com.mysql.jdbc.Driver");
+            }
+
+            //3、获取数据库的连接对象
+            connection = DriverManager.getConnection(datasourceConfig.getUrl(), datasourceConfig.getUsername(), datasourceConfig.getPassword());
+            return true;
+        } catch (Exception e) {
+            return false;
+        }finally {
+            release(null,preparedStatement,connection);
+        }
+    }
+
+    @Override
+    public List<Map> getDataByDatasourceConfig(GetDataParam getDataParam) {
+        DatasourceConfig datasourceConfig = baseMapper.selectById(getDataParam.getDataSourceConfigId());
+        Connection connection=null;
+        PreparedStatement  statement = null;
+        List<Map> res = new ArrayList<>();
+        try {
+            //1、导入驱动jar包
+            //2、注册驱动
+            if(datasourceConfig.getType().equals("0")){
+                Class.forName("com.mysql.cj.jdbc.Driver");
+            }else{
+                Class.forName("com.mysql.jdbc.Driver");
+            }
+
+            //3、获取数据库的连接对象
+            connection = DriverManager.getConnection(datasourceConfig.getUrl(), datasourceConfig.getUsername(), datasourceConfig.getPassword());
+            statement = connection.prepareStatement(getDataParam.getSql());
+            ResultSet resultSet = statement.executeQuery();
+            ResultSetMetaData  rsmd = resultSet.getMetaData();
+            while(resultSet.next()){
+                Map<String,Object> map = new HashMap<String,Object>();
+                for(int i = 0 ; i < rsmd.getColumnCount() ; i++){
+                    String col_name = rsmd.getColumnName(i+1);
+                    Object col_value = resultSet.getObject(col_name);
+                    if(col_value == null){
+                        col_value = "";
+                    }
+                    map.put(col_name, col_value);
+                }
+                res.add(map);
+            }
+            return res;
+        } catch (ClassNotFoundException e) {
+            return null;
+        } catch (SQLException throwables) {
+            return null;
+        }finally {
+            release(null,statement,connection);
+        }
+    }
+
+
+    //释放mysql连接资源
+    public static void release(ResultSet rs, Statement statement, Connection conn) {
+        if (rs != null) {
+            try {
+                rs.close();
+            } catch (SQLException e) {
+                e.printStackTrace();
+            }
+        }
+        if (statement != null) {
+            try {
+                statement.close();
+            } catch (SQLException e) {
+                e.printStackTrace();
+            }
+        }
+        if (conn != null) {
+            try {
+                conn.close();
+            } catch (SQLException e2) {
+                e2.printStackTrace();
+            }
+        }
+    }
+}
+