JdbcUtils.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.isBlank(jdbcUrl), "Error: The jdbcUrl is Null, Cannot read database type");
  38. String url = jdbcUrl.toLowerCase();
  39. if (url.contains(":mysql:") || url.contains(":cobar:")) {
  40. return DbType.MYSQL;
  41. } else if (url.contains(":mariadb:")) {
  42. return DbType.MARIADB;
  43. } else if (url.contains(":oracle:")) {
  44. return DbType.ORACLE;
  45. } else if (url.contains(":sqlserver:") || url.contains(":microsoft:")) {
  46. return DbType.SQL_SERVER2005;
  47. } else if (url.contains(":sqlserver2012:")) {
  48. return DbType.SQL_SERVER;
  49. } else if (url.contains(":postgresql:")) {
  50. return DbType.POSTGRE_SQL;
  51. } else if (url.contains(":hsqldb:")) {
  52. return DbType.HSQL;
  53. } else if (url.contains(":db2:")) {
  54. return DbType.DB2;
  55. } else if (url.contains(":sqlite:")) {
  56. return DbType.SQLITE;
  57. } else if (url.contains(":h2:")) {
  58. return DbType.H2;
  59. } else if (url.contains(":dm:")) {
  60. return DbType.DM;
  61. } else if (url.contains(":xugu:")) {
  62. return DbType.XU_GU;
  63. } else if (url.contains(":kingbase:") || url.contains(":kingbase8:")) {
  64. return DbType.KINGBASE_ES;
  65. } else if (url.contains(":phoenix:")) {
  66. return DbType.PHOENIX;
  67. } else {
  68. logger.warn("The jdbcUrl is " + jdbcUrl + ", Mybatis Plus Cannot Read Database type or The Database's Not Supported!");
  69. return DbType.OTHER;
  70. }
  71. }
  72. }