根据现在的开发模式和网上的一些资料,SpringBoot需要对业务和操作进行分层,通常分为controller、entity、service、respository等结构。下面以Kotlin官网的例子,讲解在分层的时候,需要做什么配置。

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

  1. package com.kotlinSpringBoot.entity
  2.  
  3. import java.util.*
  4. import javax.persistence.Entity
  5. import javax.persistence.GeneratedValue
  6. import javax.persistence.GenerationType
  7. import javax.persistence.Id
  8.  
  9. @Entity
  10. class People(
  11. @Id @GeneratedValue(strategy = GenerationType.AUTO)
  12. val id: Long?,
  13. val firstName: String?,
  14. val lastName: String?,
  15. val gender: String?,
  16. val age: Int?,
  17. val gmtCreated: Date,
  18. val gmtModified: Date
  19. ) {
  20. override fun toString(): String {
  21. return "People(id=$id, firstName='$firstName', lastName='$lastName', gender='$gender', age=$age, gmtCreated=$gmtCreated, gmtModified=$gmtModified)"
  22. }
  23. }

根据官网写的代码,结果却标红了:

因为上面的代码使用了JPA,但是没有引入相关的文件,在build.gradle中的dependencies添加相应的依赖即可解决该错误:

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

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

  1. package com.kotlinSpringBoot.repository
  2.  
  3. import com.kotlinSpringBoot.entity.People
  4. import org.springframework.data.repository.CrudRepository
  5.  
  6. interface PeopleRepository : CrudRepository<People, Long> {
  7. fun findByLastName(lastName: String): List<People>?
  8. }

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

  1. package com.kotlinSpringBoot.service
  2.  
  3. import com.kotlinSpringBoot.entity.People
  4. import com.kotlinSpringBoot.repository.PeopleRepository
  5. import org.springframework.beans.factory.annotation.Autowired
  6. import org.springframework.stereotype.Service
  7.  
  8. class PeopleService {
  9. @Autowired
  10. val peopleRepository: PeopleRepository? = null
  11.  
  12. fun findByLastName(lastName: String): List<People>? {
  13. return peopleRepository?.findByLastName(lastName)
  14. }
  15.  
  16. fun <S : People?> save(entity: S): S? {
  17. return peopleRepository?.save(entity)
  18. }
  19.  
  20. fun <S : People?> save(entities: MutableIterable<S>?): MutableIterable<S>? {
  21. return peopleRepository?.save(entities)
  22. }
  23.  
  24. fun delete(entities: MutableIterable<People>?) {
  25. }
  26.  
  27. fun delete(entity: People?) {
  28. }
  29.  
  30. fun delete(id: Long?) {
  31. }
  32.  
  33. fun findAll(ids: MutableIterable<Long>?): MutableIterable<People>? {
  34. return peopleRepository?.findAll(ids)
  35. }
  36.  
  37. fun findAll(): MutableIterable<People>? {
  38. return peopleRepository?.findAll()
  39. }
  40.  
  41. fun exists(id: Long?): Boolean {
  42. return peopleRepository?.exists(id)!!
  43. }
  44.  
  45. fun count(): Long {
  46. return peopleRepository?.count()!!
  47. }
  48.  
  49. fun findOne(id: Long?): People? {
  50. return peopleRepository?.findOne(id)
  51. }
  52.  
  53. fun deleteAll() {
  54. }
  55. }

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

  1. package com.kotlinSpringBoot.controller
  2.  
  3. import com.kotlinSpringBoot.service.PeopleService
  4. import org.springframework.beans.factory.annotation.Autowired
  5. import org.springframework.stereotype.Controller
  6. import org.springframework.web.bind.annotation.GetMapping
  7. import org.springframework.web.bind.annotation.RequestParam
  8. import org.springframework.web.bind.annotation.ResponseBody
  9.  
  10. @Controller
  11. class PeopleController {
  12. @Autowired
  13. val peopleService: PeopleService? = null
  14.  
  15. @GetMapping(value = "/hello")
  16. @ResponseBody
  17. fun hello(@RequestParam(value = "lastName") lastName: String): Any {
  18. val peoples = peopleService?.findByLastName(lastName)
  19. val map = HashMap<Any, Any>()
  20. map.put("hello", peoples!!)
  21. return map
  22. }
  23. }

在controller包内新增类HelloWorldController

  1. package com.kotlinSpringBoot.controller
  2.  
  3. import org.springframework.web.bind.annotation.GetMapping
  4. import org.springframework.web.bind.annotation.RestController
  5.  
  6. @RestController
  7. class HelloWorldController {
  8. @GetMapping(value = *arrayOf("/helloworld", "/"))
  9. fun helloworld(): Any {
  10. return "Hello,World!"
  11. }
  12. }

分层结束,下面说一下执行主类的另一种方法

点击图中的bootrun运行程序,报错:没有指定的主类myMainClass。上一节中我们建立了主类,如下:

  1. package com.SpringBootUseKotlin.Code
  2.  
  3. import org.springframework.boot.SpringApplication
  4. import org.springframework.boot.autoconfigure.SpringBootApplication
  5.  
  6. @SpringBootApplication
  7. open class myMainClass{
  8.  
  9. }
  10.  
  11. fun main(args:Array<String>){
  12. SpringApplication.run(myMainClass::class.java, *args)
  13. }

我们在build.gradle里加上mainClassName属性。注意,mainClassName依赖于插件application,如果报错说该属性未定义,则在build.gradle中添加:

  1. apply plugin: 'application'

那么这个属性的值是多少呢?这个类名是myMainClass,那么mainClassName的值是否为:com.SpringBootUseKotlin.Code.MyMainClass ?其实并不是。

我们可以通过下面的操作查看到类的名称(点击主类,在Run的菜单中选择设置):

所以真正的mainClassName应该设置为com.SpringBootUseKotlin.Code.MyMainClassKt,注意,后面多了个Kt。

设了类名之后,需要在主类中加上注解:

  1. package com.kotlinSpringBoot
  2.  
  3. import org.springframework.boot.SpringApplication
  4. import org.springframework.boot.autoconfigure.SpringBootApplication
  5.  
  6. //注解MapperScan需要import该jar包import org.mybatis.spring.annotation.MapperScan;
  7.  
  8. @SpringBootApplication @MapperScan("com.kotlinSpringBoot.mapper") //这个是刚加的注解,以便主类可以被扫描到 open class Application {
  9.  
  10. } fun main(args: Array<String>) { SpringApplication.run(Application::class.java, *args) }

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

  1. buildscript {
  2. ext.mybatisVersion = '3.3.1'
  3. ext.mybatis_spring = '1.2.5'
  4. }
  5.  
  6. dependencies {
  7. compile "org.mybatis:mybatis:$mybatisVersion"
  8. compile "org.mybatis:mybatis-spring:$mybatis_spring"
  9. }

配置完成后再点击一次gradle的bootrun,则可以看到下面的输出了:

SpringBoot在Kotlin中的实现(二)的更多相关文章

  1. SpringBoot在Kotlin中的实现(一)

    本节记录如何用Kotlin初步搭建一个SpringBoot的环境(使用Gradle自动化构建工具). 1.新建一个Gradle的Kotlin 配置完成后,build.gradle的配置如下: buil ...

  2. springboot 与 mybatis 中事务特性讲解

    1 MyBatis自动参与到 spring 事务管理中,无需额外配置,只要org.mybatis.spring.SqlSessionFactoryBean引用的数据源与 DataSourceTrans ...

  3. Kotlin中的object 与companion object的区别

    之前写了一篇Kotlin中常量和静态方法的文章,最近有人提出一个问题,在companion object中调用外部的成员变量会调用不到,这才意识到问题,本篇文章会带着这个疑问来解决问题. 一. obj ...

  4. DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描

    DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许进制转载  吐槽之后应该有所改了,该方式可以作为一种过渡方式 ...

  5. Kotlin——中级篇(二): 属性与字段详解

    在前面的章节中,详细的为大家讲解到了Kotlin中对类的类的定义.使用.初始化.初始化.类继承等内容,但是在一个类中,几乎上是不可能不出现属性与字段(field)的,这一篇文章就为大家奉上Kotlin ...

  6. Kotlin——高级篇(二):高阶函数详解与标准的高阶函数使用

    在上面一个章节中,详细的讲解了Kotlin中关于Lambda表达式的语法以及运用,如果还您对其还不甚理解,请参见Kotlin--高级篇(一):Lambda表达式详解.在这篇文章中,多次提到了Kotli ...

  7. Kotlin——初级篇(二):变量、常量、注释

    在Kotlin中的变量.常量以及注释多多少少和Java语言是有着不同之处的.不管是变量.常量的定义方式,还是注释的使用.下面详细的介绍Kotlin中的变量.常量.注释的使用.以及和Java的对比. 如 ...

  8. SpringBoot初始教程之日志处理(二)

    SpringBoot初始教程之日志处理(二) 1.介绍 SpringBoot默认是采用logback进行日志处理.Logback是由log4j创始人设计的又一个开源日志组件.Logback是由log4 ...

  9. Kotlin 中文文档

    Kotlin 中文文档 标签: Kotlinkotlin中文文档 2017-02-14 18:14 4673人阅读 评论(0) 收藏 举报  分类: kotlin 转载地址:http://www.tu ...

随机推荐

  1. jwt再度理解

    1,负载部分只用base64编码,是可逆的,不能存放密码 2,加密算法不在乎是对称还是非对称,因为jwt的验签不需要解密 3,一般的验签是用私钥加密签名,公钥验签,和加密反过来,加密是公钥加密,服务器 ...

  2. httpclient中文乱码

    https://blog.csdn.net/teamlet/article/details/8605840

  3. Jmeter -- HTTP Request Defaults HTTP请求默认值

    一.HTTP Request Defaults的作用: 该组件可以为我们的http请求设置默认的值.假如,我们创建一个测试计划有很多个请求且都是发送到相同的server,这时我们只需添加一个Http ...

  4. hanlp和jieba等六大中文分工具的测试对比

    本篇文章测试的哈工大LTP.中科院计算所NLPIR.清华大学THULAC和jieba.FoolNLTK.HanLP这六大中文分词工具是由  水...琥珀 完成的.相关测试的文章之前也看到过一些,但本篇 ...

  5. hadoop行业技术创新解决方案

    如今有很多公司都在努力挖掘他们拥有的大量数据,包括结构化.非结构化.半结构化以及二进制数据等,来探索对数据的深入利用. 大多数公司估计他们只分析了已有数据的12%,剩余88%还没有被充分利用.大量的数 ...

  6. windbg条件断点总结

    1 . 条件断点是断点命令 ( bp 或者 bu ) 与j命令或者.if命令一起使用的,后面跟着一个gc命令   0:000> bp Address "j (Condition) 'O ...

  7. VI常用命令及linux下软件

    一.一般模式:删除.复制与粘贴类命令 x,X x为向后删除一个字符,X为先前删除一个字符 nx(n代表数字) 向后删除n个字符 dd 删除当前行 D 删除当前行所有字符,试成为空行 ndd(n代表数字 ...

  8. XBOX360

    [汇总+分享]XBOX360多人游戏汇总贴https://tieba.baidu.com/p/3550398060?pn=13&red_tag=3423139816&traceid= ...

  9. 国外的开源项目Shopizer部署问题

    版本:shopizer-2.2.0 项目地址:https://github.com/shopizer-ecommerce/shopizer 使用IDEA时遇到和修改的问题 1.修改数据库类型为MYSQ ...

  10. Ubuntu16.04 LTS软件中心闪退及修改阿里源

    现象: 进入软件中心点击任意,直接退出 解决办法: 先更换软件源,我的为阿里云 1. 备份 源位置 :/etc/apt/sources.list 2. 更改 sudo vi /etc/apt/sour ...