Springboot之kotlin&java

19 篇文章 1 订阅
15 篇文章 0 订阅

简介

我们都知道java是面向对象的编程语言,但是其实编写代码有些复杂性,即使后来出现了jdk8加大代码开发的便捷性,但是依然不够简洁,就算强大的jvm作为运行环境,也难以各大环境中互相运行,google提倡出kotlin语言开发,同时植入了Android内部,无需插件即可开发。这里不做过多的简介语言的优缺点,每种语言都有自己的不足和有点,主要针对项目情况而进行。但是其实个人还是建议用java,毕竟java比较普遍和目前学习资料也比较多!接下来我们居于gradle构建kotlin的RestFul工程。

示例

简介技术体系

  1. 本应用编辑工具是idea,因为新版本的idea版本工具具有kotlin插件
  2. 数据库是居于docker的mysql服务
  3. 基础环境是jdk8
  4. spring boot作为技术框架

操作流程

搭建mysql&jdk

Mysql搭建
docker的mysql启动文件docker-compose.yaml:

version: '2'

services:
  mysql:
    image: mysql:5.7
    command: [mysqld, --character-set-server=utf8mb4, --collation-server=utf8mb4_unicode_ci]
    environment:
      MYSQL_ROOT_PASSWORD: root
      TZ: Asia/Shanghai
    ports:
      - "3307:3306"
    volumes: 
      - "~/data/mysql:/var/lib/mysql"

新建上面文件(touch docker-compose.yaml),当前目录启动mysql服务,docker-compose up -d
jdk配置
JAVA_HOME=你的jdk的home目录
PATH= J A V A H O M E / b i n : JAVA_HOME/bin: JAVAHOME/bin:PATH:.
CLASSPATH= J A V A H O M E / l i b / t o o l s . j a r : JAVA_HOME/lib/tools.jar: JAVAHOME/lib/tools.jar:JAVA_HOME/lib/dt.jar:.
export JAVA_HOME
export CLASSPATH

初始化spring boot

这里采用官网初始化方式链接,如下图:
在这里插入图片描述
gradler包加载如下:

	implementation("org.springframework.boot:spring-boot-starter-data-jpa")
	implementation("org.springframework.boot:spring-boot-starter-web")
	implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
	implementation("org.jetbrains.kotlin:kotlin-reflect")
	implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
	implementation("org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final")
	runtimeOnly("mysql:mysql-connector-java")
	testImplementation("org.springframework.boot:spring-boot-starter-test")

项目包结构

在这里插入图片描述

application文件

server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3307/kotlin
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    dbcp2:
      test-while-idle: true
      validation-query: SELECT 1
  jpa:
    database: MYSQL
    show-sql: true
    database-platform: org.hibernate.dialect.MySQL5Dialect
    hibernate:
      naming:
        physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

启动类,并添加扫面路径在这里插入图片描述

文件数据类

// Config 类
package com.lgh.run.config
import org.springframework.boot.autoconfigure.domain.EntityScan
import org.springframework.context.annotation.ComponentScan
import org.springframework.data.jpa.repository.config.EnableJpaRepositories
@ComponentScan("com.lgh")
@EntityScan("com.lgh.entity")
@EnableJpaRepositories("com.lgh.repository")
class Config

// DemoApplication 类
package com.lgh.run
import com.lgh.run.config.Config
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Import
@SpringBootApplication
@Import(Config::class)
class DemoApplication
fun main(args: Array<String>) {
	runApplication<DemoApplication>(*args)
}

// User
package com.lgh.entity
import javax.persistence.*
@Entity
class User(
        @Id @GeneratedValue(strategy = GenerationType.AUTO) val id:Long?,
        @Column(name = "user_id") val userId:Long?,
        @Column(name = "user_name") val userName:String?
)

// UserRepository
package com.lgh.repository
import com.lgh.entity.User
import org.springframework.data.repository.CrudRepository
interface UserRepository: CrudRepository<User,Long>{
    fun findByUserNameLike(uName:String):Collection<User>?
    fun findByUserId(uId:Long):User?
}

// UserServiceImpl 类
package com.lgh.service
import com.lgh.entity.User
import com.lgh.repository.UserRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
@Service
class UserServiceImpl {
    @Autowired
    val userRepository: UserRepository? = null
    fun findByUName(uName: String): Collection<User>? = userRepository?.findByUserNameLike(uName?.plus("%"))
    fun findByUId(id:Long): User? = userRepository?.findByUserId(id)
}

# UserRepository
package com.lgh.repository
import com.lgh.entity.User
import org.springframework.data.repository.CrudRepository
interface UserRepository: CrudRepository<User,Long>{
    fun findByUserNameLike(uName:String):Collection<User>?
    fun findByUserId(uId:Long):User?
}

// UserController
package com.lgh.controller
import com.lgh.entity.User
import com.lgh.service.UserServiceImpl
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("/users")
class UserController {
    @Autowired
    val userService: UserServiceImpl? = null
    @GetMapping("/id/{id}")
    fun findUserById(@PathVariable("id") id: Long): User? = userService?.findByUId(id)
    @GetMapping("/name/{userName}")
    fun  findByUserName(@PathVariable("userName") userName:String): Collection<User>? = userService?.findByUName(userName)
}

源码地址

github

总结&反思

留给观看者总结反思!

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值