当前位置:   article > 正文

kotlin service 服务 连接MYSQL 数据库_kotlin添加mysql依赖

kotlin添加mysql依赖

kotlin service 服务 连接MYSQL 数据库

1、添加关键依赖:

gradle中添加依赖

implementation 'org.springframework.boot:spring-boot-starter-jdbc:2.1.5.RELEASE'
implementation 'mysql:mysql-connector-java:6.0.2'
  • 1
2、配置MySQL连接字:

== 特别注意引入的mysql-connector 版本,
!!!如果是6以上的版本则需要添加 “com.mysql.cj.jdbc.Driver”;否则添加“com.mysql.jdbc.Driver” ==

@JvmField var DIRECT_BASE_URL = "jdbc:mysql://**.**.**.**:3306/testDBName?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false&serverTimezone=GMT%2B8"
@JvmField var DIRECT_BASE_USERNAME = "db_account"
@JvmField var DIRECT_BASE_PASSWORD = "db_password"
@JvmField var DIRECT_BASE_DRIVER_NAME="com.mysql.cj.jdbc.Driver"
//mysql connetor 版本低于6是使用低版本,6以上的则使用上述版本
//@JvmField var DIRECT_BASE_DRIVER_NAME_LOW="com.mysql.jdbc.Driver"
  • 1
  • 2
  • 3
  • 4
  • 5
3、连接数据库
@JvmStatic
private var connection: Connection? = null
@JvmStatic
private fun dataConnectionPostgres(): Connection {
    val url = BootProperties.DIRECT_BASE_URL
    val username = BootProperties.DIRECT_BASE_USERNAME
    val password = BootProperties.DIRECT_BASE_PASSWORD
    Class.forName(BootProperties.DIRECT_BASE_DRIVER_NAME)
    var conn: Connection? = DriverManager.getConnection(url, username, password)
    return conn!!
}
@JvmStatic
fun connection() : Connection {
    if (connection == null) {
        connection = dataConnectionPostgres()
    }
    return connection!!
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/1009230
推荐阅读
相关标签
  

闽ICP备14008679号