MPGenerator.java 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package com.malk.qiwang.utils;
  2. import com.baomidou.mybatisplus.generator.FastAutoGenerator;
  3. import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
  4. import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.SQLException;
  8. import java.sql.Types;
  9. public class MPGenerator {
  10. // public static void main(String[] args) {
  11. // //可以直接去官方文档上查看
  12. // FastAutoGenerator
  13. // .create("jdbc:mysql://localhost:3306/invoice_db?useSSL=false&serverTimezone=Asia/Shanghai",
  14. // "root", "123456")
  15. // .globalConfig(builder -> {
  16. // builder.author("LQY")
  17. // .disableOpenDir()
  18. // .outputDir(System.getProperty("user.dir")+
  19. // "/mjava-qiwang/src/main/java");
  20. // })
  21. // .dataSourceConfig(builder ->
  22. // builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
  23. // int typeCode = metaInfo.getJdbcType().TYPE_CODE;
  24. // if (typeCode == Types.SMALLINT) {
  25. // return DbColumnType.INTEGER;
  26. // }
  27. // return typeRegistry.getColumnType(metaInfo);
  28. // }))
  29. // .packageConfig(builder -> {
  30. // builder.parent("com.malk.qiwang")
  31. // .controller("Controller")
  32. // .service("Service")
  33. // .serviceImpl("Service.impl")
  34. // .mapper("mapper")
  35. // .entity("entity");
  36. // })
  37. // .strategyConfig(builder -> {
  38. // builder.addInclude("invoice_library")
  39. // .entityBuilder().enableLombok();
  40. // })
  41. // .templateEngine(new VelocityTemplateEngine())
  42. // .execute();
  43. // }
  44. // public static void main(String[] args) {
  45. // String url = "jdbc:sqlserver://113.45.187.194:1433;"+"databaseName=dingding;"+"encrypt=false;"+"trustServerCertificate=true;";
  46. //
  47. // try (Connection conn = DriverManager.getConnection(url, "hgdd", "hgdd@2025")) {
  48. // System.out.println("✅ 连接 SQL Server 成功!");
  49. // } catch (Exception e) {
  50. // e.printStackTrace();
  51. // }
  52. // }
  53. public static void main(String[] args) {
  54. // MySQL 本地连接配置
  55. String url = "jdbc:mysql://47.103.203.2:3306/invoice_db?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
  56. String username = "root";
  57. String password = "QWhs@12345";
  58. // 加载 MySQL 驱动(JDBC 4.0 后可以省略,但保留更稳妥)
  59. try {
  60. Class.forName("com.mysql.cj.jdbc.Driver");
  61. } catch (ClassNotFoundException e) {
  62. System.out.println("❌ MySQL JDBC 驱动未找到,请检查依赖");
  63. e.printStackTrace();
  64. return;
  65. }
  66. // 使用 try-with-resources 自动关闭连接
  67. try (Connection conn = DriverManager.getConnection(url, username, password)) {
  68. System.out.println("✅ 连接本地 MySQL 数据库成功!");
  69. System.out.println("数据库: invoice_db");
  70. System.out.println("MySQL 版本: " + conn.getMetaData().getDatabaseProductVersion());
  71. } catch (SQLException e) {
  72. System.out.println("❌ 连接 MySQL 数据库失败!");
  73. System.err.println("错误代码: " + e.getErrorCode());
  74. System.err.println("错误信息: " + e.getMessage());
  75. e.printStackTrace();
  76. }
  77. }
  78. }