FileExporterApp.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package com.vcarecity.cvs;
  2. import com.google.inject.Guice;
  3. import com.google.inject.Injector;
  4. import com.vcarecity.cvs.core.ReflectionUtil;
  5. import com.vcarecity.cvs.module.PropertiesModule;
  6. import com.vcarecity.cvs.module.SQLModule;
  7. import com.vcarecity.cvs.properties.AppProperties;
  8. import com.vcarecity.cvs.properties.DbProperties;
  9. import com.vcarecity.cvs.starter.SQLStarter;
  10. import com.vcarecity.cvs.type.ResultHandlerEnum;
  11. import me.yuxiaoyao.config.loader.ConfigLoader;
  12. import java.util.Map;
  13. import java.util.concurrent.ConcurrentHashMap;
  14. /**
  15. * @author Kerry on 19/11/27
  16. */
  17. public class FileExporterApp {
  18. public static Injector injector;
  19. public static Map<String, String[]> TABLE_COLUMN = new ConcurrentHashMap<>();
  20. public static void main(String[] args) throws Exception {
  21. ReflectionUtil.initTableClassMapper("com.vcarecity.cvs.entity");
  22. AppProperties appProperties = ConfigLoader.parseConfig(args, AppProperties.class, null);
  23. jdbcArgs(args, appProperties);
  24. String table = getTableByArgs(args);
  25. if (table == null || table.length() == 0) {
  26. throw new Error("table name must not be null or empty!");
  27. }
  28. Class<?> tableClass = ReflectionUtil.getTableClass(table);
  29. boolean isMysql;
  30. Object properties;
  31. final ResultHandlerEnum type = isImportToMysql(args);
  32. if (type == ResultHandlerEnum.MYSQL || type == ResultHandlerEnum.MYSQL_PS) {
  33. properties = "mysql";
  34. } else {
  35. String basePath = table;
  36. final AppProperties.Export export = appProperties.getExport();
  37. if (export != null) {
  38. basePath = export.getFolder() + table;
  39. }
  40. properties = basePath;
  41. }
  42. injector = Guice.createInjector(new PropertiesModule(appProperties), new SQLModule(type));
  43. final SQLStarter instance = injector.getInstance(SQLStarter.class);
  44. instance.exportDataAndConvert(table, properties, tableClass);
  45. }
  46. private static ResultHandlerEnum isImportToMysql(String[] args) {
  47. String type = null;
  48. for (String arg : args) {
  49. if (arg.startsWith("--type=")) {
  50. type = arg.substring("--type=".length()).trim();
  51. break;
  52. }
  53. }
  54. return ResultHandlerEnum.fromType(type);
  55. }
  56. private static String getTableByArgs(String[] args) {
  57. for (String arg : args) {
  58. if (arg.startsWith("--table=")) {
  59. return arg.substring("--table=".length()).trim();
  60. } else if (arg.startsWith("-t=")) {
  61. return arg.substring("-t=".length()).trim();
  62. }
  63. }
  64. return null;
  65. }
  66. private static void jdbcArgs(String[] args, AppProperties properties) {
  67. final DbProperties database = properties.getFromDb();
  68. for (String arg : args) {
  69. if (arg.startsWith("jdbc.url=")) {
  70. database.setUrl(arg.substring("jdbc.url=".length()).trim());
  71. } else if (arg.startsWith("jdbc.user=")) {
  72. database.setUser(arg.substring("jdbc.user=".length()).trim());
  73. } else if (arg.startsWith("jdbc.password=")) {
  74. database.setPassword(arg.substring("jdbc.password=".length()).trim());
  75. }
  76. }
  77. properties.setFromDb(database);
  78. }
  79. }