1 @IocBean 2 public class SqlCommandModel{ 3 4 //用户名 5 @Inject("java:$conf.get('jdbc.username')") 6 private String username; 7 //用户密码 8 @Inject("java:$conf.get('jdbc.password')") 9 private String password; 10 //从哪个主机导出数据库,如果没有指定这个值,则默认取localhost 11 @Inject("java:$conf.get('jdbc.host')") 12 private String host; 13 //使用的端口号 14 @Inject("java:$conf.get('jdbc.port')") 15 private String port; 16 // 路径是mysql中 bin 文件 的位置 17 @Inject("java:$conf.get('mysqlPath')") 18 private String mysqlPath; 19 20 // 导出数据库名称 21 @Inject("java:$conf.get('jdbc.exportDatabaseName')") 22 private String exportMysqlDataBase; 23 24 // 导入数据库名称 25 @Inject("java:$conf.get('jdbc.importDatabaseName')") 26 private String importDataBase; 27 28 29 // # 用户名 30 // jdbc.username=root 31 // # 数据库密码 32 // jdbc.password=123456 33 // # localhost 34 // jdbc.host=127.0.0.1 35 // # 端口号 36 // jdbc.port=3306 37 // # mysql下的bin文件的路径 (linux) 38 // mysqlPath=usr/local/mysql/bin 39 // 40 // # 要导出的数据库名称 41 // jdbc.exportDatabaseName=bankmanaer 42 // # 要导入的目标数据库 43 // jdbc.importDatabaseName=bankmanaer 44 45 46 /** 47 * 获取导出命令 48 * @param exportDatabaseName 表名称 49 * @param exportPath 导出路径 50 * @return 51 */ 52 public String getExportCommand(String exportDataTableName,String exportPath) { 53 54 StringBuffer command = new StringBuffer(); 55 //注意哪些地方要空格,哪些不要空格 56 command.append("mysqldump -u ").append(username).append(" -p").append(password)//密码是用的小p,而端口是用的大P。 57 .append(" -h").append(host).append(" -P").append(port).append(" ").append(exportMysqlDataBase + " ").append(exportDataTableName).append(" -r ").append(exportPath); 58 59 // 如果是linux系统上则加上数据库路径 60 // command.append(mysqlPath).append("mysqldump -u ").append(username).append(" -p").append(password)//密码是用的小p,而端口是用的大P。 61 // .append(" -h").append(host).append(" -P").append(port).append(" ").append(exportMysqlDataBase + " ").append(exportDataTableName).append(" -r ").append(exportPath); 62 63 return command.toString(); 64 } 65 66 67 // 得到 导入 数据库的命令 68 // 得到 导入数据 的 命令行语句 69 /** 70 * 71 * @param importPath 导入文件所在路径 72 * @return 73 */ 74 public String[] getImportCommand(String importPath) { 75 76 //第一步,获取登录命令语句 77 String loginCommand = new StringBuffer().append("mysql -h").append(host).append(" -u ").append(username).append(" -p").append(password) 78 .append(" -P").append(port).toString(); 79 //第二步,获取切换数据库到目标数据库的命令语句 80 String switchCommand = new StringBuffer().append("use ").append(importDataBase).toString(); 81 //第三步,获取导入的命令语句 82 String importCommand = new StringBuffer(" source ").append(importPath).toString(); 83 //需要返回的命令语句数组 84 85 String[] commands = new String[] {loginCommand, switchCommand, importCommand}; 86 87 return commands; 88 } 89 90 91 92 public static void main(String[] args) throws InterruptedException, IOException { 93 94 SqlCommandModel sqlCommandModel = new SqlCommandModel(); 95 96 String[] tables = new String[5]; 97 98 // 这里其实是在命令窗口中执行的 command 命令行 99 List<Process> list = new ArrayList<>(); 100 for (int i = 0; i < tables.length; i++) { 101 String exportCommand = sqlCommandModel.getExportCommand(tables[i], "D:\" + tables[i] + ".sql"); 102 Runtime runtime = Runtime.getRuntime(); 103 // 这里其实是在命令窗口中执行的 command 命令行 104 list.add(runtime.exec(exportCommand)); 105 } 106 //等待所有子进程处理完毕 107 for (Process process : list) { 108 while(process.waitFor() != 0){ 109 ; 110 } 111 } 112 113 } 114 115 }