標籤:cto ice 命名 檔案中 response body last rest run
根據現在的開發模式和網上的一些資料,SpringBoot需要對業務和操作進行分層,通常分為controller、entity、service、respository等結構。下面以Kotlin官網的例子,講解在分層的時候,需要做什麼配置。
1、在包com.SpringBootUseKotlin中建立包entity,添加新的class,命名為People
package com.kotlinSpringBoot.entityimport java.util.*import javax.persistence.Entityimport javax.persistence.GeneratedValueimport javax.persistence.GenerationTypeimport javax.persistence.Id@Entityclass 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.repositoryimport com.kotlinSpringBoot.entity.Peopleimport org.springframework.data.repository.CrudRepositoryinterface PeopleRepository : CrudRepository<People, Long> { fun findByLastName(lastName: String): List<People>?}
3、在包com.SpringBootUseKotlin中建立包service,新增class,命名為:PeopleService
package com.kotlinSpringBoot.serviceimport com.kotlinSpringBoot.entity.Peopleimport com.kotlinSpringBoot.repository.PeopleRepositoryimport org.springframework.beans.factory.annotation.Autowiredimport org.springframework.stereotype.Serviceclass 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.controllerimport com.kotlinSpringBoot.service.PeopleServiceimport org.springframework.beans.factory.annotation.Autowiredimport org.springframework.stereotype.Controllerimport org.springframework.web.bind.annotation.GetMappingimport org.springframework.web.bind.annotation.RequestParamimport org.springframework.web.bind.annotation.ResponseBody@Controllerclass 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.controllerimport org.springframework.web.bind.annotation.GetMappingimport org.springframework.web.bind.annotation.RestController@RestControllerclass HelloWorldController { @GetMapping(value = *arrayOf("/helloworld", "/")) fun helloworld(): Any { return "Hello,World!" }}
分層結束,下面說一下執行主類的另一種方法
點擊圖中的bootrun運行程式,報錯:沒有指定的主類myMainClass。上一節中我們建立了主類,如下:
package com.SpringBootUseKotlin.Codeimport org.springframework.boot.SpringApplicationimport org.springframework.boot.autoconfigure.SpringBootApplication@SpringBootApplicationopen 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.kotlinSpringBootimport org.springframework.boot.SpringApplicationimport 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中的實現(二)