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(); } }