zjh
2025-04-18 b3ffebcf0f1c58ac2f33922bffc5cff330873bde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.ltkj.web.controller.api;
 
import com.alibaba.druid.pool.DruidDataSource;
import com.ltkj.common.annotation.Anonymous;
import com.ltkj.common.core.domain.AjaxResult;
import com.ltkj.common.utils.spring.SpringUtils;
import com.ltkj.web.config.wordUtil.ITableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.sql.SQLException;
import java.util.Properties;
 
@Slf4j
@RestController
@RequestMapping("/api")
@Api(tags = "AAAAAAAAAAAAA  数据库操作接口")
public class TableController {
 
    @Autowired
    ITableService tableService;
 
    @GetMapping(value = "/tableToWord", produces = "application/json;charset=utf-8", name = "导出数据库表信息生成Word")
    //@ApiOperation(value = "导出数据库表信息生成Word", httpMethod = "GET", response = AjaxResult.class, notes = "导出数据库表信息生成Word")
    public AjaxResult tableToWord() {
        try {
            return AjaxResult.success("导出数据库表信息生成Word成功", tableService.getTableInfo());
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
 
    @GetMapping("/updateDruidProperty")
    @ApiOperation(value ="切换数据库")
    @Anonymous
    public AjaxResult updateProperty(@RequestParam @ApiParam(value = "数据库ip")String ip,
                                     @RequestParam @ApiParam(value = "数据库端口")String prot,
                                     @RequestParam @ApiParam(value = "数据库名")String name,
                                     @RequestParam @ApiParam(value = "数据库用户名")String userName,
                                     @RequestParam @ApiParam(value = "数据库密码")String passWored){
        DruidDataSource dataSource = SpringUtils.getBean("masterDataSource");
        try {
            Properties properties = new Properties();
            // 这里是测试写法,具体的value可以通过请求参数传递过来
            properties.setProperty("druid.url","jdbc:mysql://"+ip+":"+prot+"/"+name+"?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
            properties.setProperty("druid.username",userName);
            properties.setProperty("druid.password",passWored);
            dataSource.restart(properties);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return AjaxResult.success();
    }
 
}