zjh
2024-12-09 345f5654fee547c2f06f74c9eb4855c8bd1f8712
ltkj-admin/src/main/java/com/ltkj/web/controller/api/TableController.java
@@ -1,19 +1,27 @@
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 = "AAAAAAAAAAAAAAA导出数据库")
@Api(tags = "AAAAAAAAAAAAA  数据库操作接口")
public class TableController {
    @Autowired
@@ -28,4 +36,27 @@
            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();
    }
}