JdbcUtils.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2011-2020, baomidou (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. * https://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.extension.toolkit;
  17. import com.baomidou.mybatisplus.annotation.DbType;
  18. import com.baomidou.mybatisplus.core.toolkit.Assert;
  19. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  20. import org.apache.ibatis.logging.Log;
  21. import org.apache.ibatis.logging.LogFactory;
  22. /**
  23. * JDBC 工具类
  24. *
  25. * @author nieqiurong
  26. * @since 2016-12-05
  27. */
  28. public class JdbcUtils {
  29. private static final Log logger = LogFactory.getLog(JdbcUtils.class);
  30. /**
  31. * 根据连接地址判断数据库类型
  32. *
  33. * @param jdbcUrl 连接地址
  34. * @return ignore
  35. */
  36. public static DbType getDbType(String jdbcUrl) {
  37. Assert.isFalse(StringUtils.isEmpty(jdbcUrl), "Error: The jdbcUrl is Null, Cannot read database type");
  38. if (jdbcUrl.contains(":mysql:") || jdbcUrl.contains(":cobar:")) {
  39. return DbType.MYSQL;
  40. } else if (jdbcUrl.contains(":mariadb:")) {
  41. return DbType.MARIADB;
  42. } else if (jdbcUrl.contains(":oracle:")) {
  43. return DbType.ORACLE;
  44. } else if (jdbcUrl.contains(":sqlserver:") || jdbcUrl.contains(":microsoft:")) {
  45. return DbType.SQL_SERVER2005;
  46. } else if (jdbcUrl.contains(":sqlserver2012:")) {
  47. return DbType.SQL_SERVER;
  48. } else if (jdbcUrl.contains(":postgresql:")) {
  49. return DbType.POSTGRE_SQL;
  50. } else if (jdbcUrl.contains(":hsqldb:")) {
  51. return DbType.HSQL;
  52. } else if (jdbcUrl.contains(":db2:")) {
  53. return DbType.DB2;
  54. } else if (jdbcUrl.contains(":sqlite:")) {
  55. return DbType.SQLITE;
  56. } else if (jdbcUrl.contains(":h2:")) {
  57. return DbType.H2;
  58. } else if (jdbcUrl.contains(":dm:")) {
  59. return DbType.DM;
  60. } else if (jdbcUrl.contains(":xugu:")) {
  61. return DbType.XU_GU;
  62. } else if (jdbcUrl.contains(":kingbase:") || jdbcUrl.contains(":kingbase8:")) {
  63. return DbType.KINGBASE_ES;
  64. } else {
  65. logger.warn("The jdbcUrl is " + jdbcUrl + ", Mybatis Plus Cannot Read Database type or The Database's Not Supported!");
  66. return DbType.OTHER;
  67. }
  68. }
  69. }