詳解用Kotlin寫一個基于Spring Boot的RESTful服務(wù)
Spring太復雜了,配置這個東西簡直就是浪費生命。尤其在沒有什么并發(fā)壓力,隨便搞一個RESTful服務(wù),讓整個業(yè)務(wù)跑起來先的情況下,更是么有必要糾結(jié)在一堆的XML配置上。顯然這么想的人是很多的,于是就有了Spring Boot。又由于Java 8太墨跡于是有了Kotlin。
數(shù)據(jù)源使用MySql。通過Spring Boot這個基本不怎么配置的,不怎么微的微框架的Spring Data JPA和Hibernate來訪問數(shù)據(jù)。
處理依賴
這里使用Gradle來處理依賴。
首先下載官網(wǎng)給的初始項目:
git clone https://github.com/spring-guides/gs-accessing-data-jpa.git
然后跳轉(zhuǎn)到gs-accessing-data-jpa/initial
目錄下。
用IntelliJ IDEA打開這個項目,選擇使用Gradle管理依賴。
之后Gradle會自動下載依賴項。這會花一點時間。你可以去和妹子聊一會兒了。。
如果你覺得這樣很麻煩的話,可以建立一個Gradle項目。之后根據(jù)上面的例子建立一個目錄:
└── src └── main └── java └── hello
但是無論是用上面的哪種方式,最后都需要在Gradle文件中添加依賴項。這個Gradle文件是build.gradle
。添加完依賴項
之后是這樣的:
buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'spring-boot' jar { baseName = 'gs-spring-boot' version = '0.1.0' } repositories { mavenCentral() } sourceCompatibility = 1.8 targetCompatibility = 1.8 dependencies { // tag::jetty[] compile("org.springframework.boot:spring-boot-starter-web") { exclude module: "spring-boot-starter-tomcat" } compile("org.springframework.boot:spring-boot-starter-jetty") // end::jetty[] // tag::actuator[] compile("org.springframework.boot:spring-boot-starter-actuator") // end::actuator[] compile('org.springframework.boot:spring-boot-starter-data-jpa:1.3.3.RELEASE') compile('mysql:mysql-connector-java:5.1.13') testCompile("junit:junit") } task wrapper(type: Wrapper) { gradleVersion = '2.3' }
配置文件
在目錄src/main/resources/application.properties
下編輯配置文件。默認是沒有這個文件和相應(yīng)的目錄的,自行創(chuàng)建。
spring.datasource.url = jdbc:mysql://localhost:3306/test spring.datasource.username = root spring.datasource.password = root #spring.datasource.driverClassName = com.mysql.jdbc.Driver # Specify the DBMS spring.jpa.database = MYSQL # Keep the connection alive if idle for a long time (needed in production) spring.datasource.testWhileIdle = true spring.datasource.validationQuery = SELECT 1 # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate ddl auto (create, create-drop, update) spring.jpa.hibernate.ddl-auto = update # Naming strategy spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy # Use spring.jpa.properties.* for Hibernate native properties (the prefix is # stripped before adding them to the entity manager) # The SQL dialect makes Hibernate generate better SQL for the chosen database spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
無需java的配置類,或者什么XML配置文件。
使用配置項hibernate.ddl-auto = true,
項目所需的數(shù)據(jù)庫和相關(guān)表、列會自動根據(jù)定義的實體類創(chuàng)建。點擊這里,查看更多配置的說明。
創(chuàng)建一個簡單地實體類
這里定義一個簡單地實體類,并聲明為JPA實體。這個類的文件存放在目錄src\main\java\hello\Entities\
下。
package hello.Entities import javax.validation.constraints.NotNull import java.io.Serializable; import javax.persistence.*; /** * Created by Bruce on 2016/3/9. */ @Entity @Table(name = "user") data class User(@Id @GeneratedValue(strategy = GenerationType.AUTO) var id: Long? = 0, @Column(nullable = false) var name: String? = null, @Column(nullable = false) var email: String? = null) : Serializable { protected constructor() : this(id = null, name = null, email = null) { } }
這里使用了Kotlin里的data class。data class最大的優(yōu)點就是省去了定義getter和setter,以及toString()
的時間。這些都已經(jīng)默認實現(xiàn)。所以,在使用data class的對象的時候直接可以使用name
、email
當然還有id
這樣的屬性直接訪問。
無參數(shù)的構(gòu)造函數(shù)是給JPA用的,所以訪問級別設(shè)定為protected
。主構(gòu)造函數(shù)是用來創(chuàng)建和數(shù)據(jù)庫操作相關(guān)的對象的。
整個的整個類被@Entity
修飾,說明整個類是一個JPA的實體類。@Table
聲明用來表明整個類對應(yīng)的數(shù)據(jù)庫表是哪一個。
@Id
修飾的User
的屬性id
,會被JPA認為的對象的ID。同時@GeneratedValue(strategy = GenerationType.AUTO)
的修飾說明這個ID是自動生成的。
另外的兩個屬性name
和email
被@Column(nullable = false)
修飾。說明兩個列都是不可以為空的,同時說明兩個列的名字和屬性的名字是相同的。如果不同可以這樣@Column(nullable = false, name="XXXXXX")。
創(chuàng)建簡單地查詢,或者說Dao類
這個就更加的簡單了。JPA會自動在運行時創(chuàng)建數(shù)據(jù)庫需要的增刪改查的實現(xiàn)。這個實現(xiàn)可以是根據(jù)我們給出的Repository
來實現(xiàn)的。
根據(jù)User
類,我們來實現(xiàn)一個UserDao(Repository):
package hello.Entities import org.springframework.data.repository.CrudRepository import org.springframework.transaction.annotation.Transactional @Transactional interface UserDao : CrudRepository<User, Long> { fun findByEmail(email: String): User? }
泛型的類型參數(shù)分別是user和user的id的類型:User
, Long
。我們可以定義增刪改查之外的Query。比如在上面的代碼里我們定義了一個findByEmail()
方法。具體的自定義查詢時的命名規(guī)則可以查看這里。
用Controller測試一下
數(shù)據(jù)庫,Rest服務(wù)和書庫的連接都已經(jīng)搞定。那么,我們就來測試一下。
我們在目錄src\main\java\hello\Controllers
創(chuàng)建一個UserController
類來測試和數(shù)據(jù)庫的數(shù)據(jù)存取。
package hello.Controllers import hello.Entities.User import hello.Entities.UserDao import org.springframework.beans.factory.annotation.Autowired import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.ResponseBody /** * Created by Bruce on 2016/3/9. */ @RestController class UserController { @Autowired private var userDao: UserDao? = null @RequestMapping("/create") @ResponseBody public fun create(name: String, email: String): User? { try { var newUser = User(name = name, email = email) userDao?.save(newUser) return newUser } catch(e: Exception) { return null } } @RequestMapping("/delete") @ResponseBody public fun delete(id: Long): String { try { var user = User(id) userDao?.delete(user) return id.toString() + "deleted" } catch(e: Exception) { return "delete error " + e.message.toString() } } @RequestMapping("/get-by-email") @ResponseBody public fun getByEmail(email: String): User? { try { var user = userDao?.findByEmail(email) if (user != null) { return user } else { return null } } catch(e: Exception) { return null } } @RequestMapping("/update") @ResponseBody public fun updateUser(id: Long, name: String, email: String): User? { try { var user: User? = userDao?.findOne(id) ?: return null user?.name = name user?.email = email userDao?.save(user) return user } catch(e: Exception) { return null } } }
測試URL可以是這樣的:
- /create?name=Jack&email=hello@234.com,使用指定的用戶名和郵箱在數(shù)據(jù)庫里生成一個新的user,id是自動生成的。
- /delete?id=3, 刪除id值為3的user。
- /get-by-email?email=hello@234.com,注意Controller用到的UserDao.findByEmail()只返回一個user,所以如果有多個 返回值的話會報錯。
- /update?id=1&email=what@123.com&name=Bruce,更新id為1的user。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 關(guān)于Spring Boot和Kotlin的聯(lián)合開發(fā)
- spring boot + jpa + kotlin入門實例詳解
- 使用Spring boot + jQuery上傳文件(kotlin)功能實例詳解
- Spring Boot 與 Kotlin 使用JdbcTemplate連接MySQL數(shù)據(jù)庫的方法
- Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法
- Kotlin + Spring Boot 請求參數(shù)驗證的代碼實例
- Spring Boot 與 Kotlin 使用Redis數(shù)據(jù)庫的配置方法
- Spring Boot 與 Kotlin 上傳文件的示例代碼
- Spring Boot與Kotlin定時任務(wù)的示例(Scheduling Tasks)
- 利用Kotlin + Spring Boot實現(xiàn)后端開發(fā)
相關(guān)文章
基于jdk動態(tài)代理和cglib動態(tài)代理實現(xiàn)及區(qū)別說明
這篇文章主要介紹了基于jdk動態(tài)代理和cglib動態(tài)代理實現(xiàn)及區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05MyBatis-Plus實現(xiàn)公共字段自動填充功能詳解
在開發(fā)中經(jīng)常遇到多個實體類有共同的屬性字段,這些字段屬于公共字段,也就是很多表中都有這些字段,能不能對于這些公共字段在某個地方統(tǒng)一處理,來簡化開發(fā)呢?MyBatis-Plus就提供了這一功能,本文就來為大家詳細講講2022-08-08教你通過B+Tree平衡多叉樹理解InnoDB引擎的聚集和非聚集索引
大家都知道B+Tree是從二叉樹演化而來,在這之前我們來先了解二叉樹、平衡二叉樹、平衡多叉樹,這篇文章主要介紹了通過B+Tree平衡多叉樹理解InnoDB引擎的聚集和非聚集索引,需要的朋友可以參考下2022-01-01Java將Exception信息轉(zhuǎn)為String字符串的方法
今天小編就為大家分享一篇Java將Exception信息轉(zhuǎn)為String字符串的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10詳解Java使用雙異步后如何保證數(shù)據(jù)一致性
這篇文章主要為大家詳細介紹了Java使用雙異步后如何保證數(shù)據(jù)一致性,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以了解下2024-01-01