Springboot之kotlin&java
- 簡介
- 示例
- 簡介技術體系
- 操作流程
- 搭建mysql&jdk
- 初始化spring boot
- 專案包結構
- application檔案
- 檔案資料類
- 原始碼地址
- 總結&反思
簡介
我們都知道java是面向物件的編程語言,但是其實撰寫代碼有些復雜性,即使后來出現了jdk8加大代碼開發的便捷性,但是依然不夠簡潔,就算強大的jvm作為運行環境,也難以各大環境中互相運行,google提倡出kotlin語言開發,同時植入了Android內部,無需插件即可開發,這里不做過多的簡介語言的優缺點,每種語言都有自己的不足和有點,主要針對專案情況而進行,但是其實個人還是建議用java,畢竟java比較普遍和目前學習資料也比較多!接下來我們居于gradle構建kotlin的RestFul工程,
示例
簡介技術體系
- 本應用編輯工具是idea,因為新版本的idea版本工具具有kotlin插件
- 資料庫是居于docker的mysql服務
- 基礎環境是jdk8
- spring boot作為技術框架
操作流程
搭建mysql&jdk
Mysql搭建
docker的mysql啟動檔案docker-compose.yaml:
version: '2'
services:
mysql:
image: mysql:5.7
command: [mysqld, --character-set-server=utf8mb4, --collation-server=utf8mb4_unicode_ci]
environment:
MYSQL_ROOT_PASSWORD: root
TZ: Asia/Shanghai
ports:
- "3307:3306"
volumes:
- "~/data/mysql:/var/lib/mysql"
新建上面檔案(touch docker-compose.yaml),當前目錄啟動mysql服務,docker-compose up -d
jdk配置
JAVA_HOME=你的jdk的home目錄
PATH=
J
A
V
A
H
O
M
E
/
b
i
n
:
JAVA_HOME/bin:
JAVAH?OME/bin:PATH:.
CLASSPATH=
J
A
V
A
H
O
M
E
/
l
i
b
/
t
o
o
l
s
.
j
a
r
:
JAVA_HOME/lib/tools.jar:
JAVAH?OME/lib/tools.jar:JAVA_HOME/lib/dt.jar:.
export JAVA_HOME
export CLASSPATH
初始化spring boot
這里采用官網初始化方式鏈接,如下圖:

gradler包加載如下:
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final")
runtimeOnly("mysql:mysql-connector-java")
testImplementation("org.springframework.boot:spring-boot-starter-test")
專案包結構

application檔案
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3307/kotlin
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
dbcp2:
test-while-idle: true
validation-query: SELECT 1
jpa:
database: MYSQL
show-sql: true
database-platform: org.hibernate.dialect.MySQL5Dialect
hibernate:
naming:
physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
啟動類,并添加掃面路徑
檔案資料類
// Config 類
package com.lgh.run.config
import org.springframework.boot.autoconfigure.domain.EntityScan
import org.springframework.context.annotation.ComponentScan
import org.springframework.data.jpa.repository.config.EnableJpaRepositories
@ComponentScan("com.lgh")
@EntityScan("com.lgh.entity")
@EnableJpaRepositories("com.lgh.repository")
class Config
// DemoApplication 類
package com.lgh.run
import com.lgh.run.config.Config
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Import
@SpringBootApplication
@Import(Config::class)
class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}
// User
package com.lgh.entity
import javax.persistence.*
@Entity
class User(
@Id @GeneratedValue(strategy = GenerationType.AUTO) val id:Long?,
@Column(name = "user_id") val userId:Long?,
@Column(name = "user_name") val userName:String?
)
// UserRepository
package com.lgh.repository
import com.lgh.entity.User
import org.springframework.data.repository.CrudRepository
interface UserRepository: CrudRepository<User,Long>{
fun findByUserNameLike(uName:String):Collection<User>?
fun findByUserId(uId:Long):User?
}
// UserServiceImpl 類
package com.lgh.service
import com.lgh.entity.User
import com.lgh.repository.UserRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
@Service
class UserServiceImpl {
@Autowired
val userRepository: UserRepository? = null
fun findByUName(uName: String): Collection<User>? = userRepository?.findByUserNameLike(uName?.plus("%"))
fun findByUId(id:Long): User? = userRepository?.findByUserId(id)
}
# UserRepository
package com.lgh.repository
import com.lgh.entity.User
import org.springframework.data.repository.CrudRepository
interface UserRepository: CrudRepository<User,Long>{
fun findByUserNameLike(uName:String):Collection<User>?
fun findByUserId(uId:Long):User?
}
// UserController
package com.lgh.controller
import com.lgh.entity.User
import com.lgh.service.UserServiceImpl
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("/users")
class UserController {
@Autowired
val userService: UserServiceImpl? = null
@GetMapping("/id/{id}")
fun findUserById(@PathVariable("id") id: Long): User? = userService?.findByUId(id)
@GetMapping("/name/{userName}")
fun findByUserName(@PathVariable("userName") userName:String): Collection<User>? = userService?.findByUName(userName)
}
原始碼地址
github
總結&反思
留給觀看者總結反思!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/247194.html
標籤:java
上一篇:AcWing倉庫選址
