美文网首页
kotlin+springboot(1)

kotlin+springboot(1)

作者: 洋__ | 来源:发表于2017-06-01 16:59 被阅读62次

最近流行kotlin,就赶紧趁热来一波实战。
一开始使用idea自动创建kotlin-springboot项目,创建完之后整个人傻掉了,一脸懵逼。只好rm -rf ,重新创建了个java的脚手架。😳

然后,正片开始...

这次先测试了整合data-jpa:
先创建实体类 Note.kt

package com.udc.domin.note

import java.util.*
import javax.persistence.*

/**
 * Created by zhangjiqiang on 2017/6/1.
 */
@Entity
class Note{

    @Id
    @GeneratedValue
     var id: Long? = null

     var author: String? = null

     var title: String? = null

     var type: String? = null

     var status: Int? = null

     var kw: String? = null

     var mdHtml: String? = null

    @Lob
     var content: String? = null

    @Temporal(TemporalType.TIMESTAMP)
     var createTime: Date? = null

    @Temporal(TemporalType.TIMESTAMP)
     var updateTime: Date? = null

}

然后,创建repository

package com.udc.domin.note

import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.domain.Specification
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository

/**
 * Created by zhangjiqiang on 2017/6/1.
 */
@Repository
interface NoteRepository : JpaRepository<Note, Long> {
    fun findByTitle(title: String): List<Note>
    fun findAll(status: Specification<Note>, page: Pageable)
    fun findById(id: Long): Note
}

再来个测试类!

package com.udc

import com.udc.domin.note.Note
import com.udc.domin.note.NoteRepository
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner

/**
 * Created by zhangjiqiang on 2017/6/1.
 */
@RunWith(SpringRunner::class)
@SpringBootTest
class Test{

    @Autowired
    val noteRepository: NoteRepository? = null

    @Test
    fun contextLoads() {
        var note=Note()
        note.author="yangxyxxxx"
        note.kw="ssssss"
        noteRepository?.save(note)
        val list = noteRepository?.findByTitle("x2")
        println(if (list == null) "null" else list!!.size)
        println(list!!.get(0).content)
    }
}

这里踩了个小坑。说明一下,在创建Note的时候,一开始属性是用val 声明的,然后这里给对象赋值就会报错,所以Note里的所有属性都改成了var声明 😅

val list = noteRepository?.findByTitle("x2")
println(if (list == null) "null" else list!!.size)
println(list!!.get(0).content)

这里的?表示返回可能为null,使用的时候要小心了。
下面的 println(list!!.get(0).content),嗯,为什么要这么写,为什么这么写,为什么这,为什么,,看我的眼神就行了...😮😮😮😮
list!!.get(0).content
等价于
if (list != null) {
println(list.get(0).content)
}

相关文章

  • kotlin+springboot(1)

    最近流行kotlin,就赶紧趁热来一波实战。一开始使用idea自动创建kotlin-springboot项目,创建...

  • 使用Kotlin+SpringBoot进行web开发

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

  • 初探Kotlin+SpringBoot联合编程

    Kotlin是一门最近比较流行的静态类型编程语言,而且和Groovy、Scala一样同属Java系。Kotlin具...

  • Kotlin+SpringBoot与Redis整合详解

    1、Redis是简介 redis官方网 Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持...

  • Kotlin+SpringBoot与RabbitMQ整合详解

    1、前言简介 RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,如:Pyt...

  • Kotlin+SpringBoot与Swagger生成API文档

    1、前言与目的 工程架构,请看我之前撰写的 Kotlin +SpringBoot + MyBatis完美搭建最简洁...

  • 1▪1▪1▪1▪1

    今天是国际劳动节,出门看人头,上路遇堵车,处处挤破头,急哭也停不下车。 不如歇了吧 ...

  • 1+1+1…+1=1

    对“一”的理解: 赠人玫瑰,不仅仅是手留余香。 利益他人,实际上也疗愈了自己。 利他、利己,如此往复循环, 最终利...

  • (-1)×(-1)= 1

    数学家经过很长一段时间才认识到(-1)×(-1)= 1是不能被证明的(即使大数学家欧拉曾给出不能令人信服的...

  • 1-2-1-1-1

    【下马请罪】 子龙下马,向张飞跪地请罪道:“张将军,一时失手……”话未停,便被张飞一矛刺了个透心凉。子龙堵着胸口汩...

网友评论

      本文标题:kotlin+springboot(1)

      本文链接:https://www.haomeiwen.com/subject/xojefxtx.html