DataSourceConfig.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * Copyright (c) 2011-2020, hubin (jobob@qq.com).
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. * <p>
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.baomidou.mybatisplus.generator.config;
  17. import java.sql.Connection;
  18. import java.sql.DriverManager;
  19. import java.sql.SQLException;
  20. import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
  21. import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
  22. import com.baomidou.mybatisplus.generator.config.converts.OracleTypeConvert;
  23. import com.baomidou.mybatisplus.generator.config.converts.PostgreSqlTypeConvert;
  24. import com.baomidou.mybatisplus.generator.config.converts.SqlServerTypeConvert;
  25. import com.baomidou.mybatisplus.generator.config.rules.DbType;
  26. /**
  27. * <p>
  28. * 数据库配置
  29. * </p>
  30. *
  31. * @author YangHu
  32. * @since 2016/8/30
  33. */
  34. public class DataSourceConfig {
  35. /**
  36. * 数据库类型
  37. */
  38. private DbType dbType;
  39. /**
  40. * PostgreSQL schemaname
  41. */
  42. private String schemaname = "public";
  43. /**
  44. * 类型转换
  45. */
  46. private ITypeConvert typeConvert;
  47. /**
  48. * 驱动连接的URL
  49. */
  50. private String url;
  51. /**
  52. * 驱动名称
  53. */
  54. private String driverName;
  55. /**
  56. * 数据库连接用户名
  57. */
  58. private String username;
  59. /**
  60. * 数据库连接密码
  61. */
  62. private String password;
  63. /**
  64. * 判断数据库类型
  65. *
  66. * @return 类型枚举值
  67. */
  68. public DbType getDbType() {
  69. if (null == dbType) {
  70. if (driverName.contains("mysql")) {
  71. dbType = DbType.MYSQL;
  72. } else if (driverName.contains("oracle")) {
  73. dbType = DbType.ORACLE;
  74. } else if (driverName.contains("postgresql")) {
  75. dbType = DbType.POSTGRE_SQL;
  76. } else {
  77. throw new MybatisPlusException("Unknown type of database!");
  78. }
  79. }
  80. return dbType;
  81. }
  82. public DataSourceConfig setDbType(DbType dbType) {
  83. this.dbType = dbType;
  84. return this;
  85. }
  86. public String getSchemaname() {
  87. return schemaname;
  88. }
  89. public void setSchemaname(String schemaname) {
  90. this.schemaname = schemaname;
  91. }
  92. public ITypeConvert getTypeConvert() {
  93. if (null == typeConvert) {
  94. switch (getDbType()) {
  95. case ORACLE:
  96. typeConvert = new OracleTypeConvert();
  97. break;
  98. case SQL_SERVER:
  99. typeConvert = new SqlServerTypeConvert();
  100. break;
  101. case POSTGRE_SQL:
  102. typeConvert = new PostgreSqlTypeConvert();
  103. break;
  104. default:
  105. // 默认 MYSQL
  106. typeConvert = new MySqlTypeConvert();
  107. break;
  108. }
  109. }
  110. return typeConvert;
  111. }
  112. public DataSourceConfig setTypeConvert(ITypeConvert typeConvert) {
  113. this.typeConvert = typeConvert;
  114. return this;
  115. }
  116. /**
  117. * 创建数据库连接对象
  118. *
  119. * @return Connection
  120. */
  121. public Connection getConn() {
  122. Connection conn = null;
  123. try {
  124. Class.forName(driverName);
  125. conn = DriverManager.getConnection(url, username, password);
  126. } catch (ClassNotFoundException | SQLException e) {
  127. e.printStackTrace();
  128. }
  129. return conn;
  130. }
  131. public String getUrl() {
  132. return url;
  133. }
  134. public DataSourceConfig setUrl(String url) {
  135. this.url = url;
  136. return this;
  137. }
  138. public String getDriverName() {
  139. return driverName;
  140. }
  141. public DataSourceConfig setDriverName(String driverName) {
  142. this.driverName = driverName;
  143. return this;
  144. }
  145. public String getUsername() {
  146. return username;
  147. }
  148. public DataSourceConfig setUsername(String username) {
  149. this.username = username;
  150. return this;
  151. }
  152. public String getPassword() {
  153. return password;
  154. }
  155. public DataSourceConfig setPassword(String password) {
  156. this.password = password;
  157. return this;
  158. }
  159. }