欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot 與 Kotlin 使用JdbcTemplate連接MySQL數(shù)據(jù)庫的方法

 更新時(shí)間:2018年01月22日 10:51:56   作者:quanke  
本文介紹在Spring Boot基礎(chǔ)下配置數(shù)據(jù)源和通過 JdbcTemplate 編寫數(shù)據(jù)訪問的示例。感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧

之前介紹了一些Web層的例子,包括構(gòu)建RESTful API、使用Thymeleaf模板引擎渲染W(wǎng)eb視圖,但是這些內(nèi)容還不足以構(gòu)建一個(gè)動(dòng)態(tài)的應(yīng)用。通常我們做App也好,做Web應(yīng)用也好,都需要內(nèi)容,而內(nèi)容通常存儲(chǔ)于各種類型的數(shù)據(jù)庫,服務(wù)端在接收到訪問請求之后需要訪問數(shù)據(jù)庫獲取并處理成展現(xiàn)給用戶使用的數(shù)據(jù)形式。

本文介紹在Spring Boot基礎(chǔ)下配置數(shù)據(jù)源和通過 JdbcTemplate 編寫數(shù)據(jù)訪問的示例。

數(shù)據(jù)源配置

在我們訪問數(shù)據(jù)庫的時(shí)候,需要先配置一個(gè)數(shù)據(jù)源,下面分別介紹一下幾種不同的數(shù)據(jù)庫配置方式。

首先,為了連接數(shù)據(jù)庫需要引入jdbc支持,在 build.gradle 中引入如下配置:

compile "org.springframework.boot:spring-boot-starter-jdbc:$spring_boot_version"

連接數(shù)據(jù)源

以MySQL數(shù)據(jù)庫為例,先引入MySQL連接的依賴包,在 build.gradle 中加入:

compile "mysql:mysql-connector-java:$mysql_version"

完整 build.gradle

group 'name.quanke.kotlin'
version '1.0-SNAPSHOT'
buildscript {
 ext.kotlin_version = '1.2.10'
 ext.spring_boot_version = '1.5.4.RELEASE'
 ext.springfox_swagger2_version = '2.7.0'
 ext.mysql_version = '5.1.21'
 repositories {
  mavenCentral()
 }
 dependencies {
  classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  classpath("org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version")
//  Kotlin整合SpringBoot的默認(rèn)無參構(gòu)造函數(shù),默認(rèn)把所有的類設(shè)置open類插件
  classpath("org.jetbrains.kotlin:kotlin-noarg:$kotlin_version")
  classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version")
 }
}
apply plugin: 'kotlin'
apply plugin: "kotlin-spring" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin
apply plugin: 'org.springframework.boot'
jar {
 baseName = 'chapter11-6-1-service'
 version = '0.1.0'
}
repositories {
 mavenCentral()
}
dependencies {
 compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
 compile "org.springframework.boot:spring-boot-starter-web:$spring_boot_version"
 compile "org.springframework.boot:spring-boot-starter-jdbc:$spring_boot_version"
 compile "mysql:mysql-connector-java:$mysql_version"
 testCompile "org.springframework.boot:spring-boot-starter-test:$spring_boot_version"
 testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}
compileKotlin {
 kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
 kotlinOptions.jvmTarget = "1.8"
}

在 src/main/resources/application.yml 中配置數(shù)據(jù)源信息

spring:
 datasource:
 url: jdbc:mysql://localhost:3306/test
 username: root
 password: 123456
 driver-class-name: com.mysql.jdbc.Driver

連接JNDI數(shù)據(jù)源

當(dāng)你將應(yīng)用部署于應(yīng)用服務(wù)器上的時(shí)候想讓數(shù)據(jù)源由應(yīng)用服務(wù)器管理,那么可以使用如下配置方式引入JNDI數(shù)據(jù)源。

如果對JNDI不是很了解的,請參考 https://baike.baidu.com/item/JNDI/3792442?fr=aladdin

spring.datasource.jndi-name=java:jboss/datasources/customers

使用JdbcTemplate操作數(shù)據(jù)庫

Spring的 JdbcTemplate 是自動(dòng)配置的,你可以直接使用 @Autowired 來注入到你自己的bean中來使用。

舉例:我們在創(chuàng)建 User 表,包含屬性id,name、age,下面來編寫數(shù)據(jù)訪問對象和單元測試用例。

定義包含有插入、刪除、查詢的抽象接口UserService

interface UserService {
 /**
  * 獲取用戶總量
  */
 val allUsers: Int?
 /**
  * 新增一個(gè)用戶
  * @param name
  * @param age
  */
 fun create(name: String, password: String?)
 /**
  * 根據(jù)name刪除一個(gè)用戶高
  * @param name
  */
 fun deleteByName(name: String)
 /**
  * 刪除所有用戶
  */
 fun deleteAllUsers()
}

通過 JdbcTemplate 實(shí)現(xiàn) UserService 中定義的數(shù)據(jù)訪問操作

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Service
/**
 * Created by http://quanke.name on 2018/1/10.
 */
@Service
class UserServiceImpl : UserService {
 @Autowired
 private val jdbcTemplate: JdbcTemplate? = null
 override val allUsers: Int?
  get() = jdbcTemplate!!.queryForObject("select count(1) from USER", Int::class.java)
 override fun create(name: String, password: String?) {
  jdbcTemplate!!.update("insert into USER(USERNAME, PASSWORD) values(?, ?)", name, password)
 }
 override fun deleteByName(name: String) {
  jdbcTemplate!!.update("delete from USER where USERNAME = ?", name)
 }
 override fun deleteAllUsers() {
  jdbcTemplate!!.update("delete from USER")
 }
}

創(chuàng)建對UserService的單元測試用例,通過創(chuàng)建、刪除和查詢來驗(yàn)證數(shù)據(jù)庫操作的正確性。

/**
 * Created by http://quanke.name on 2018/1/9.
 */
@RunWith(SpringRunner::class)
@SpringBootTest
class ApplicationTests {
 val log = LogFactory.getLog(ApplicationTests::class.java)!!
 @Autowired
 lateinit var userService: UserService
 @Test
 fun `jdbc test"`() {
  val username = "quanke"
  val password = "123456"
  // 插入5個(gè)用戶
  userService.create("$username a", "$password 1")
  userService.create("$username b", "$password 2")
  userService.create("$username c", "$password 3")
  userService.create("$username d", "$password 4")
  userService.create("$username e", "$password 5")
  log.info("總共用戶 ${userService.allUsers}")
  // 刪除兩個(gè)用戶
  userService.deleteByName("$username a")
  userService.deleteByName("$username b")
  log.info("總共用戶 ${userService.allUsers}")
 }
}

上面介紹的JdbcTemplate只是最基本的幾個(gè)操作,更多其他數(shù)據(jù)訪問操作的使用請參考:JdbcTemplate API

通過上面這個(gè)簡單的例子,我們可以看到在Spring Boot下訪問數(shù)據(jù)庫的配置依然秉承了框架的初衷:簡單。我們只需要在pom.xml中加入數(shù)據(jù)庫依賴,再到application.yml中配置連接信息,不需要像Spring應(yīng)用中創(chuàng)建JdbcTemplate的Bean,就可以直接在自己的對象中注入使用。

總結(jié)

以上所述是小編給大家介紹的Spring Boot 與 Kotlin 使用JdbcTemplate連接MySQL數(shù)據(jù)庫的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • IDEA?2022.1.4用前注意事項(xiàng)

    IDEA?2022.1.4用前注意事項(xiàng)

    這篇文章主要介紹了IDEA?2022.1.4用前注意事項(xiàng),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • java 類加載機(jī)制和反射詳解及實(shí)例代碼

    java 類加載機(jī)制和反射詳解及實(shí)例代碼

    這篇文章主要介紹了java 類加載機(jī)制和反射詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Java8 Supplier接口和Consumer接口原理解析

    Java8 Supplier接口和Consumer接口原理解析

    這篇文章主要介紹了Java8 Supplier接口和Consumer接口原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • java讀取excel表格的方法

    java讀取excel表格的方法

    這篇文章主要為大家詳細(xì)介紹了java讀取excel表格的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • 基于Java ActiveMQ的實(shí)例講解

    基于Java ActiveMQ的實(shí)例講解

    下面小編就為大家?guī)硪黄贘ava ActiveMQ的實(shí)例講解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • java格式化數(shù)字操作 NumberFormat及DecimalFormat

    java格式化數(shù)字操作 NumberFormat及DecimalFormat

    這篇文章主要介紹了java格式化數(shù)字操作 NumberFormat及DecimalFormat,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringBoot處理 CORS 跨域的方法詳解

    SpringBoot處理 CORS 跨域的方法詳解

    Springboot跨域問題,是當(dāng)前主流web開發(fā)人員都繞不開的難題,CORS是一個(gè)W3C標(biāo)準(zhǔn),全稱是”跨域資源共享”,本文將給大家詳細(xì)介紹SpringBoot 如何處理 CORS 跨域,感興趣的同學(xué)跟著小編一起來看看吧
    2023-07-07
  • java(swing)+ mysql實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)源碼

    java(swing)+ mysql實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)源碼

    這篇文章主要分享了java mysql實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)的源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Java實(shí)現(xiàn)Http工具類的封裝操作示例

    Java實(shí)現(xiàn)Http工具類的封裝操作示例

    這篇文章主要介紹了Java實(shí)現(xiàn)Http工具類的封裝操作,涉及java針對http請求與響應(yīng)、遠(yuǎn)程交互與字符串拼接等操作封裝技巧,需要的朋友可以參考下
    2018-01-01
  • SpringBoot使用Spring-Data-Jpa實(shí)現(xiàn)CRUD操作

    SpringBoot使用Spring-Data-Jpa實(shí)現(xiàn)CRUD操作

    這篇文章主要為大家詳細(xì)介紹了SpringBoot使用Spring-Data-Jpa實(shí)現(xiàn)CRUD操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08

最新評論