SpringBoot在Kotlin中的實現(二)

根據如今的開發模式和網上的一些資料,SpringBoot須要對業務和操做進行分層,一般分爲controller、entity、service、respository等結構。下面以Kotlin官網的例子,講解在分層的時候,須要作什麼配置。java

一、在包com.SpringBootUseKotlin中新建包entity,添加新的class,命名爲Peopleweb

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)"
    }
}

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

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

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

二、在包com.SpringBootUseKotlin中新建包respository,新增class,命名爲:PeopleRepositoryapp

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>?
}

三、在包com.SpringBootUseKotlin中新建包service,新增class,命名爲:PeopleServiceide

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() {
    }
}

四、在包com.SpringBootUseKotlin中新建包controller,新增class,命名爲:PeopleControllerspring-boot

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包內新增類HelloWorldControllergradle

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!"
    }
}

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

點擊圖中的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,則能夠看到下面的輸出了:

相關文章
相關標籤/搜索