根据现在的开发模式和网上的一些资料,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中的实现(二)的更多相关文章

  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. 八、springboot(五)配置定时器

    1.修改启动类 @SpringBootApplication @EnableScheduling public class DemoApplication { public static void m ...

  2. ubuntu 客户机安装VMware tools时出现编译错误,无法与主机共享文件(转)

    主机:win7旗舰版 vmware workstation 10.0.7 (其他10.x版本也有这个问题) 客户机:Ubuntu14.04.4-16.x 安装vmware tools时出现下列编译错误 ...

  3. 删除JavaScript对象中的元素

    参考http://stackoverflow.com/questions/208105/how-to-remove-a-property-from-a-javascript-object 通过dojo ...

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

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

  5. Qt中的主窗口之菜单栏

    1.Qt中的主窗口 主窗口为建立应用程序用户界面提供了一个框架 Qt开发平台中直接支持主窗口的概念 QMainWindow是Qt中主窗口的基类 QMainWindow继承于QWidget是一种容器类型 ...

  6. 小米笔记本装MAC(黑苹果),外接USB蓝牙,禁止笔记本自带的蓝牙方法

    https://blog.daliansky.net/ 解决插入耳机没有声音问题,每次开机需要重新插拔一次:https://github.com/daliansky/XiaoMi-Pro/blob/m ...

  7. PByte和PChar的关系

    作为指针是相同的, 解析的内容,稍微有点区别. var s:String;P:PChar;B:PByte;a:Integer;begin  s:='1234';  P:=PChar(s);//按chr ...

  8. CentOS 7.4 初次手记:第四章 CentOS安全了解

    第四章 CentOS安全了解... 66 第一节 user.group.chmod. 66 I 10位文件属性... 66 II user/group增删改... 67 III user/group配 ...

  9. S域传递函数的零点和极点

    传递函数的极点就是对应微分方程的特征根(回忆一下,$\frac{1}{s+a}$是$e^{-a t}$的Laplace变换),因此它们决定了系统自由运动的模态. 传递函数的零点不直接形成自由运动的模态 ...

  10. 【ActiveMQ】之安全机制(二)客户端连接安全

    配置完管控台的安全之后,我们还要配置客户端连接安全,否则大家都可以往MQ上发送消息,这样太危险! 根据官方文档,http://activemq.apache.org/security.html Act ...