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 }