1. 程式人生 > >SpringBoot在Kotlin中的實現(二)

SpringBoot在Kotlin中的實現(二)

文件中 open 代碼 rabl delete ons list any data

根據現在的開發模式和網上的一些資料,SpringBoot需要對業務和操作進行分層,通常分為controller、entity、service、respository等結構。下面以Kotlin官網的例子,講解在分層的時候,需要做什麽配置。

1、在包com.SpringBootUseKotlin中新建包entity,添加新的class,命名為People

package com.kotlinSpringBoot.entity

import java.util.*
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType import javax.persistence.Id @Entity class People( @Id @GeneratedValue(strategy = GenerationType.AUTO) val id: Long?, val firstName: String?, val lastName: String?, val gender: String?, val age: Int?, val gmtCreated: Date, val gmtModified: Date ) { override fun toString(): String {
return "People(id=$id, firstName=‘$firstName‘, lastName=‘$lastName‘, gender=‘$gender‘, age=$age, gmtCreated=$gmtCreated, gmtModified=$gmtModified)" } }

根據官網寫的代碼,結果卻標紅了:

技術分享圖片

因為上面的代碼使用了JPA,但是沒有引入相關的文件,在build.gradle中的dependencies添加相應的依賴即可解決該錯誤:

 compile ‘org.springframework.boot:spring-boot-starter-data-jpa:1.3.3.RELEASE‘

2、在包com.SpringBootUseKotlin中新建包respository,新增class,命名為:PeopleRepository

package com.kotlinSpringBoot.repository

import com.kotlinSpringBoot.entity.People
import org.springframework.data.repository.CrudRepository

interface PeopleRepository : CrudRepository<People, Long> {
    fun findByLastName(lastName: String): List<People>?
}

3、在包com.SpringBootUseKotlin中新建包service,新增class,命名為:PeopleService

package com.kotlinSpringBoot.service

import com.kotlinSpringBoot.entity.People
import com.kotlinSpringBoot.repository.PeopleRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

class PeopleService {
    @Autowired
    val peopleRepository: PeopleRepository? = null


    fun findByLastName(lastName: String): List<People>? {
        return peopleRepository?.findByLastName(lastName)
    }

    fun <S : People?> save(entity: S): S? {
        return peopleRepository?.save(entity)
    }

    fun <S : People?> save(entities: MutableIterable<S>?): MutableIterable<S>? {
        return peopleRepository?.save(entities)
    }

    fun delete(entities: MutableIterable<People>?) {
    }

    fun delete(entity: People?) {
    }

    fun delete(id: Long?) {
    }

    fun findAll(ids: MutableIterable<Long>?): MutableIterable<People>? {
        return peopleRepository?.findAll(ids)
    }

    fun findAll(): MutableIterable<People>? {
        return peopleRepository?.findAll()
    }

    fun exists(id: Long?): Boolean {
        return peopleRepository?.exists(id)!!
    }

    fun count(): Long {
        return peopleRepository?.count()!!
    }

    fun findOne(id: Long?): People? {
        return peopleRepository?.findOne(id)
    }

    fun deleteAll() {
    }
}

4、在包com.SpringBootUseKotlin中新建包controller,新增class,命名為:PeopleController

package com.kotlinSpringBoot.controller

import com.kotlinSpringBoot.service.PeopleService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody

@Controller
class PeopleController {
    @Autowired
    val peopleService: PeopleService? = null

    @GetMapping(value = "/hello")
    @ResponseBody
    fun hello(@RequestParam(value = "lastName") lastName: String): Any {
        val peoples = peopleService?.findByLastName(lastName)
        val map = HashMap<Any, Any>()
        map.put("hello", peoples!!)
        return map
    }
}

在controller包內新增類HelloWorldController

package com.kotlinSpringBoot.controller

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class HelloWorldController {
    @GetMapping(value = *arrayOf("/helloworld", "/"))
    fun helloworld(): Any {
        return "Hello,World!"
    }
}

分層結束,下面說一下執行主類的另一種方法

技術分享圖片

點擊圖中的bootrun運行程序,報錯:沒有指定的主類myMainClass。上一節中我們建立了主類,如下:

package com.SpringBootUseKotlin.Code

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication
open class myMainClass{

}

fun main(args:Array<String>){
    SpringApplication.run(myMainClass::class.java, *args)
}

我們在build.gradle裏加上mainClassName屬性。註意,mainClassName依賴於插件application,如果報錯說該屬性未定義,則在build.gradle中添加:

apply plugin: ‘application‘

那麽這個屬性的值是多少呢?這個類名是myMainClass,那麽mainClassName的值是否為:com.SpringBootUseKotlin.Code.MyMainClass ?其實並不是。

我們可以通過下面的操作查看到類的名稱(點擊主類,在Run的菜單中選擇設置):

技術分享圖片

技術分享圖片

所以真正的mainClassName應該設置為com.SpringBootUseKotlin.Code.MyMainClassKt,註意,後面多了個Kt。

設了類名之後,需要在主類中加上註解:

package com.kotlinSpringBoot

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
//註解
MapperScan需要import該jar包
import org.mybatis.spring.annotation.MapperScan;
@SpringBootApplication @MapperScan(
"com.kotlinSpringBoot.mapper") //這個是剛加的註解,以便主類可以被掃描到
open
class Application { }
fun main(args: Array
<String>) { SpringApplication.run(Application::class.java, *args) }

上面的代碼中,需要引入org.mybatis.spring.annotation.MapperScan,因此需要在build.gradle的配置文件中增加下面的配置:

buildscript {
    ext.mybatisVersion = ‘3.3.1‘
    ext.mybatis_spring = ‘1.2.5‘
}

dependencies {
    compile "org.mybatis:mybatis:$mybatisVersion"
    compile "org.mybatis:mybatis-spring:$mybatis_spring"
}

配置完成後再點擊一次gradle的bootrun,則可以看到下面的輸出了:

技術分享圖片

SpringBoot在Kotlin中的實現(二)