博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Kotlin + SpringBoot + JPA 进行web开发极简教程
阅读量:7124 次
发布时间:2019-06-28

本文共 4760 字,大约阅读时间需要 15 分钟。

使用Kotlin + SpringBoot + JPA 进行web开发极简教程

开始前需要有java基础, SpringBoot基础和kotlin基础

kotlin参考, 相信对于一个Java程序员来说, 半天就能上手了

为什么选择Kotlin

Kotlin比起java来说更加简洁, 和java一样是基于JVM的编程语言, 网上关于Kotlin优点缺点的讨论也有很多, 这里就不展开了.

当然, 最主要的原因的是, 暑假实习的公司, 使用Kotlin和SpringBOot进行web开发的, o(╯□╰)o

之前对于kotlin的了解甚少, 只知道在去年的google I/O大会上成了安卓的第一语言, 其他就不了解了.

现在趁暑假前, 先学习一下kotlin.

教程开始

创建一个SpringBoot工程

首先当然是使用IDEA创建一个SpringBoot工程

这里语言选择Kotlin, 使用gradle进行管理, 之后再选择模块的时候只要选择上web, jpa和mysql就可以了

然后修改配置文件, 因为导入了jpa, 所以一定要设置好DataSource, 否则无法启动

spring:  datasource:    driver-class-name: com.mysql.jdbc.Driver    username: root    password: ABCabc123#    url: jdbc:mysql://localhost:3306/db_test?useSSL=false

配置完成后可以, 在source目录下可以看到已经创建好了一个Application.kt文件, 用于启动SpringBoot, 对应Java下的Application.java

文件

@SpringBootApplicationclass TestApplicationfun main(args: Array
) { runApplication
(*args)}

创建Controller

@RestController@RequestMapping("/hello")class HelloController {    @GetMapping    fun hello():String {        return "hello world"    }}

和java的写法非常像, 可以无缝转换

启动!

使用curl命令进行请求

➜  ~ curl "http://localhost:8080/hello"hello world

简单的请求完成了

使用Swagger2生成接口文档

使用Swagger2可以自动生成接口文档和进行接口测试, 极大的方便了后端, 不需要去花很大的功夫获去维护文档

首先试试导入Swagger2

compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.8.0'    compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.8.0'

配置Swagger2

@Configuration@EnableSwagger2class Swagger2 {    @Bean    fun createRestApi(): Docket {        return Docket(DocumentationType.SWAGGER_2)  // 使用Swagger2                .apiInfo(apiInfo())                 // 设置接口页面信息                .select()                           // 返回ApiSelectorBuilder的实例                .apis(RequestHandlerSelectors.basePackage("io.ride.vote.web"))      // api接口所在的包                .paths(PathSelectors.any())                         .build()    }    /**     * 页面信息展示     */    private fun apiInfo(): ApiInfo {        return ApiInfoBuilder()                .title("Vote RestFul APIs文档")                .description("项目API接口文档")                .contact(Contact("ride", "", "supreDong@gamil.com"))                .version("0.0.1")                .build()    }}

@Configuration注解表明这是一个配置类, @EnableSwagger2注解表明启用Swagger2

通过在controller中添加注解来生成api文档

@Api(value = "测试", description = "测试控制器")@RestController@RequestMapping("/hello")class HelloController {    @GetMapping    @ApiOperation("你好!世界!", notes = "返回hello world")    fun hello(): String {        return "hello world"    }}

之后打开:8080/swagger-ui.html可以看到生成的接口信息

如图, 在该页面上还以对接口进行测试

统一异常处理

和java下的操作是一致的, 只是把java翻译成了kotlin

@ControllerAdviceclass CustomExceptionHandler {    @ExceptionHandler(ApiException::class)    fun handlerApiException(e: ApiException): ResponseEntity
{ val result = Result(e.code, e.data) return result.ok() } @ExceptionHandler(MissingServletRequestParameterException::class) fun handMissingServletRequestParameterException(e: MissingServletRequestParameterException): ResponseEntity
{ val result = Result(HttpStatus.BAD_REQUEST.value(), e.message) return result.ok() }}class ApiException(val code: ResultCode, val data: HashMap
? = null) : RuntimeException(code.msg)

使用JPA

首先配置JPA:

spring:  jpa:    show-sql: true    hibernate:      ddl-auto: update    database: mysql

创建data类

@Entity@Table(name = "t_user")data class User(        @Id        @GeneratedValue(strategy = GenerationType.IDENTITY)        var id: Long = -1,        @Column(nullable = false)        var username: String? = null,        @Column(nullable = false)        var password: String? = null,        @Column(nullable = false)        var email: String? = null,        @Column(nullable = true)        var nickname: String? = null,        @Column(nullable = false)        var createTime: Date = Date())

创建repository类

interface IUserService {    /**     * 添加一个用户     */    fun addUser(user: User): User    /**     * 展示所有用户     */    fun listAll(): List
/** * 删除一个用户 */ fun deleteUser(id: Long)}

进行单元测试

@RunWith(SpringRunner::class)@SpringBootTestclass UserRepositoryTest {    @Autowired    private lateinit var userRepository: UserRepository    @Test    fun `find all user test`() {        println(userRepository.findAll())    }    @Test    fun `add user test`() {        val user = User(username = "ride", email = "supreDong@gmail.com", password = "123123", nickname = "ride")        println(userRepository.save(user))    }    @Test    fun `delete user test`() {        val user = userRepository.findById(1)        println(user.orElse(null))        if (user.isPresent)            userRepository.deleteById(user.get().id)    }}

在单元测试并且只能在单元测试中(kotlin1.2)可以使用反引号来定义方法

总结

使用使用kotlin结合SpringBoot是一种从船新体验, 推荐大家尝试一下

转载地址:http://tpeel.baihongyu.com/

你可能感兴趣的文章
poj3140(树的dfs)
查看>>
Castle ActiveRecord的一对多问题
查看>>
VM安装系统时提示硬件不支持(unsupported hardware detected)
查看>>
mmap探究
查看>>
那些常用的eclipse快捷键
查看>>
C++中处理XML文件
查看>>
团队编程项目作业1-成员简介及分工
查看>>
LuoguP1126 机器人搬重物(BFS)
查看>>
装饰器模式-Decerator
查看>>
Android中Intent传值与Bundle传值的区别详解
查看>>
flash 动画
查看>>
作业1(贾栋琳)
查看>>
【转】关于 @synchronized,这儿比你想知道的还要多
查看>>
POJ NOI0105-33 计算分数加减表达式的值
查看>>
CCF201604-1 折点计数(解法二)(100分)
查看>>
NUC1333 Knight Moves【DFS】
查看>>
写Java代码分别使堆溢出,栈溢出
查看>>
面向对象程序设计(二)
查看>>
学习elasticsearch(一)linux环境搭建(3)——head插件安装
查看>>
SVN cleanup 报错,清除svn的工作队列
查看>>