Springboot之kotlin&java
简介
我们都知道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:
JAVAHOME/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:
JAVAHOME/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)
}
源码地址
总结&反思
留给观看者总结反思!