前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Kotlin+SpringBoot进行web开发

使用Kotlin+SpringBoot进行web开发

作者头像
飞奔去旅行
发布2019-06-13 16:05:04
1.7K0
发布2019-06-13 16:05:04
举报
文章被收录于专栏:智慧协同智慧协同

Kotlin已经发布1.1.0版本了,玩过后已经被其先进的语法深深迷恋。这里不再陈述Kotlin的强大,只说明一下如何与SpringBoot进行集成开发。

Demo地址:https://github.com/gefangshuai/spring-kotlin-demo

第一步:获取项目脚手架

移步https://start.spring.io定制下载项目基本雏形,我的配置如下:

基本

下载下来后,在Idea中导入,并建立基本的目录结构,如下:

Paste_Image.png

环境准备

加入jsp支持

代码语言:javascript
复制
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

加入postgresql数据库支持

代码语言:javascript
复制
<dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.3-1102.jdbc41</version>
</dependency>

加入开发者工具

用于自动部署

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

开启视图及jpa支持

修改application.properties文件,配置如下:

代码语言:javascript
复制
server.port=8082

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

spring.devtools.restart.exclude=static/**,public/**

# database
spring.datasource.url= jdbc:postgresql://localhost:5432/test
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# jpa+hibernate
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.open-in-view=true
spring.jpa.show-sql = true

至此,项目环境准备完毕。

业务开发

下面我们来进行简单的业务开发。

构造Model类

假设我们要维护一个客户信息,客户包含两个信息“firstName”和“lastName”。model类如下:

代码语言:javascript
复制
@Entity
@Table(name = "customer")
data class Customer (
        @Id @GeneratedValue(strategy = GenerationType.AUTO) var id: Int?,
        var firstName: String?,
        var lastName: String?
        ){
    constructor() : this(null, null, null)  // Spring 需要
}

这里我们用到了kotlin的数据类,因为通常我们的model类只是用来保存数据,很少做业务操作,而用数据类的好处是:可以自动帮我们生成equals()hashCode()toString()componentN() 函数 ,并具有强大的copy()功能。 参见:https://kotlin-zhcn.github.io/docs/reference/data-classes.html#数据类

这里需要注意

  1. 在 JVM 中,如果生成的类需要含有一个无参的构造函数,则所有的属性必须指定默认值。 (参见构造函数)。
代码语言:javascript
复制
data class User(val name: String = "", val age: Int = 0)

因为Spring在进行对象绑定的时候,需要model类具有无参构造,所以此处我们声明的Customer类必须指定构造参数默认值。否则Spring进行对象绑定会报错!!!

  1. 由于Spring依赖注入需要默认无参构造,所以我们需要为其创建一个默认无参的构造函数
代码语言:javascript
复制
constructor() : this(null, null, null)

当然,为了解决这个比较鸡肋的问题,Kotlin还是为我们提供了工具支持。这里只说一下Maven的配置方法: 加入依赖:

代码语言:javascript
复制
<dependency>
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-maven-noarg</artifactId>
      <version>${kotlin.version}</version>
</dependency>

开启对jpa的支持:

Paste_Image.png

这样我们写model类就不需要主动实现空构造了,编译器会帮我们实现

代码语言:javascript
复制
@Entity
@Table(name = "customer")
data class Customer (
        @Id @GeneratedValue(strategy = GenerationType.AUTO)
        var id: Int?,
        var firstName: String?,
        var lastName: String?
        ): Serializable{
//    constructor() : this(null, null, null)  // Spring 需要
}

准备Dao类

代码语言:javascript
复制
interface CustomerRepository : CrudRepository<Customer, Long> {
    fun findByLastName(lastName: String): MutableIterable<Customer>

}

这里可以看到,Kotlin与Java语法上的区别,用法其实是一样的!

编写Service类

代码语言:javascript
复制
@Service
@Transactional(readOnly = true)
class CustomerService {
    @Autowired
    lateinit var customerReposity: CustomerRepository

    @Transactional
    fun save(customer: Customer) = customerReposity.save(customer)

    fun findAll(): MutableIterable<Customer>? = customerReposity.findAll();

}

CustomerController类

代码语言:javascript
复制
@Controller
@RequestMapping("/customer")
class CustomerController {
    @Autowired
    lateinit var customerService: CustomerService

    @RequestMapping("/add")
    fun addForm(): String = "form"

    @RequestMapping(value = "save", method = arrayOf(RequestMethod.POST))
    fun saveCustomer(customer: Customer): String {
        customerService.save(customer)

        return "redirect:/"
    }
}

注意:Controller中我们注入Service用到Kotlin的属性懒加载机制

代码语言:javascript
复制
lateinit var customerService: CustomerService

因为Spring会帮我们实例化Service及其他Bean。 其他也是语法上的区别,不了解的可以自行补脑。

视图页面不再介绍,大家可以将项目下载下来看具体代码:https://github.com/gefangshuai/spring-kotlin-demo

运行示例

启动Maven配置如下:

Maven配置

运行后,会看到首页

首页

点击添加,跳转到添加表单页:

表单

提交表单,会刷新首页,出现我们添加的信息

列表

总结

附源码地址: SpringBoot Kotlin Demo

好了,至此一个简单的Kotlin+SpringBoot Demo已开发完成,大家可以以此项目为基本雏形,进行更深入的业务扩展。 总之,Kotlin带来的不仅仅是开发效率上的提高,其100% interoperable with Java™的原则弥补了Java很多的不足,绝对是一门值得学习并使用的新型语言。

看看大家的评价吧: 知乎-如何评价Kotlin语言 知乎-如何评价kotlin1.1正式发布? 说一个笑话: 有了val, 在也不用纠结 static final 和 final static 了。 :-)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.03.08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第一步:获取项目脚手架
  • 环境准备
    • 加入jsp支持
      • 加入postgresql数据库支持
        • 加入开发者工具
          • 开启视图及jpa支持
          • 业务开发
            • 构造Model类
              • 准备Dao类
                • 编写Service类
                  • CustomerController类
                  • 运行示例
                  • 总结
                  相关产品与服务
                  云开发 CLI 工具
                  云开发 CLI 工具(Cloudbase CLI Devtools,CCLID)是云开发官方指定的 CLI 工具,可以帮助开发者快速构建 Serverless 应用。CLI 工具提供能力包括文件储存的管理、云函数的部署、模板项目的创建、HTTP Service、静态网站托管等,您可以专注于编码,无需在平台中切换各类配置。
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档