Browse Source

数据库复制测试

huangzhiyong 7 years ago
parent
commit
76e3ebd330

+ 5 - 0
pom.xml

@ -211,6 +211,11 @@
            <artifactId>joda-time</artifactId>
            <version>2.9.9</version>
        </dependency>
        <dependency>
            <groupId>com.yihu.core</groupId>
            <artifactId>ehr-dbhelper</artifactId>
            <version>1.1.9</version>
        </dependency>
    </dependencies>
    <build>

+ 4 - 0
src/main/java/com/yihu/hos/common/constants/ContextAttributes.java

@ -10,6 +10,10 @@ public interface ContextAttributes {
    String USER_INFO = "userInfo";
    String TENANT_SESSION = "tenantSession";//session缓存的的schema 和 tenant
    String GLOBAL_DB = "global_db";//平台管理中心库
    String BASE_DB = "base_db";//基础库,用于新增租户后的库复制
    String TABLE_NAME = "TABLE_NAME";//表名
    String SHELL_RESPONSE = "shell_repsonse.";

+ 22 - 4
src/main/java/com/yihu/hos/tenant/controller/TenantController.java

@ -10,12 +10,9 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@ -189,7 +186,7 @@ public class TenantController extends BaseController{
    @ResponseBody
    public Result createDB(HttpServletRequest request,
                           @ApiParam(name = "dbName", value = "数据库名称", allowMultiple = true)
                           @RequestPart() String dbName) {
                           @RequestParam() String dbName) {
        try {
            tenantService.createDB(dbName);
@ -199,4 +196,25 @@ public class TenantController extends BaseController{
            return Result.error("新建数据库失败!");
        }
    }
    /**
     * 数据库复制
     * @param dbName
     * @param dbConfig
     * @return
     * @throws Exception
     */
    @RequestMapping("dataBaseCopy")
    @ResponseBody
    public Result requestTest(
            @ApiParam(name = "dbName", value = "源数据库名称", allowMultiple = true)
            @RequestParam(value = "dbName") String dbName,
            @ApiParam(name = "dbConfig", value = "源数据库连接", allowMultiple = true)
            @RequestParam(value = "dbConfig")  String dbConfig) throws Exception {
        Result result = tenantService.copyBaseTables(dbName, dbConfig);
        return result;
    }
}

+ 1 - 1
src/main/java/com/yihu/hos/tenant/dao/TenantDao.java

@ -60,7 +60,7 @@ public class TenantDao extends SQLGeneralDAO {
    }
    public void createDB(String dbName) throws Exception {
        String sql = "CREATE DATABASE "+dbName;
        String sql = "CREATE DATABASE IF NOT EXISTS "+dbName;
        super.execute(sql);
    }

+ 48 - 3
src/main/java/com/yihu/hos/tenant/service/TenantService.java

@ -1,22 +1,25 @@
package com.yihu.hos.tenant.service;
import com.yihu.ehr.dbhelper.jdbc.DBHelper;
import com.yihu.hos.common.constants.ContextAttributes;
import com.yihu.hos.config.MongoConfig;
import com.yihu.hos.core.encrypt.DES;
import com.yihu.hos.core.log.Logger;
import com.yihu.hos.core.log.LoggerFactory;
import com.yihu.hos.services.ServiceMycatEventService;
import com.yihu.hos.tenant.dao.TenantDao;
import com.yihu.hos.tenant.model.DBInfoModel;
import com.yihu.hos.tenant.model.TenantModel;
import com.yihu.hos.web.framework.constant.MycatConstant;
import com.yihu.hos.web.framework.constant.SqlConstants;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.model.bo.ServiceMycat;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;
@ -31,6 +34,7 @@ import java.util.UUID;
public class TenantService {
    public static final String BEAN_ID = "TenantService";
    static private final Logger logger = LoggerFactory.getLogger(TenantService.class);
    @Autowired
    private TenantDao tenantDao;
@ -139,4 +143,45 @@ public class TenantService {
        return Result.success("新建成功");
    }
    @Transactional
    public Result createDB(DBHelper targetDb,String targetDbName) throws Exception {
        //创建数据库
        String createDb = "CREATE DATABASE IF NOT EXISTS "+targetDbName;
        targetDb.execute(createDb);
        return Result.success("新建成功");
    }
    /**
     *  //TODO 库复制  (测试,可删除)
     * 数据库复制
     * @param targetDbName  目标数据库名称
     * @param targetDbUri   目标数据库连接信息
     * @return
     */
    public Result copyBaseTables(String targetDbName,String targetDbUri) throws Exception {
        String baseDbUri = "jdbc:mysql://172.19.103.57:3306/base_db?user=root&password=xmjkzl";//源数据库连接
        DBHelper originDb = new DBHelper(ContextAttributes.BASE_DB, baseDbUri);
        DBHelper targetDb = new DBHelper(targetDbName, targetDbUri);
        //创建数据库
        createDB(targetDb,targetDbName);
        //查询源数据库所有表名
        String tablesSql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '"+ ContextAttributes.BASE_DB+"';";
        //创建表
        List<JSONObject> tableNames = originDb.query(tablesSql);
        for (JSONObject jsonObject:tableNames){
            try {
            String tableName = jsonObject.getString(ContextAttributes.TABLE_NAME);
            boolean createTable = targetDb.execute(SqlConstants.CREATE_TABLE + targetDbName+"."+tableName + SqlConstants.LIKE + ContextAttributes.BASE_DB+"."+tableName);
            boolean insertData = targetDb.execute(SqlConstants.INSERT_INTO +  targetDbName+"."+tableName + SqlConstants.SELECT + "*" + SqlConstants.FROM + ContextAttributes.BASE_DB+"."+tableName);
            System.out.println(insertData);
            }catch (Exception e){
                logger.error("执行异常:"+e.getMessage());
            }
        }
        //新增表数据
        return Result.success("新建成功");
    }
}