Scala and SBT and Spring Boot

  |   0 评论   |   0 浏览

Scala and SBT and Spring Boot

SBT

https://www.scala-sbt.org/

IDEA 安装 Scala 插件

image.png

SBT 原始参数配置信息

配置镜像

进入 Cmder bash 执行

mkdir ~/.sbt && \
cat > ~/.sbt/repositories <<EOF
[repositories]
  local
  aliyunmaven: https://maven.aliyun.com/repository/public/
  sbt-plugin-releases: https://repo.scala-sbt.org/scalasbt/sbt-plugin-releases, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
EOF

image.png

IDEA 创建 testsbt

Cigarette.scala

package com.lab.test
package domain

import scala.beans.BeanProperty

class Cigarette {
  @BeanProperty var id: Long = _
  @BeanProperty var brand: String = _
  @BeanProperty var tar: Int = _
  @BeanProperty var price: BigDecimal = _
}

TestService.scala

package com.lab.test
package service

import domain.Cigarette

import org.springframework.stereotype.Service

@Service
class TestService {

  def listCigarette(): List[Cigarette] = {
    val list = List

    val c1 = new Cigarette
    c1.setId(1)
    c1.setBrand("南京")
    c1.setPrice(95)
    c1.setTar(10)

    val c2 = new Cigarette
    c2.setId(2)
    c2.setBrand("玉溪")
    c2.setPrice(45)
    c2.setTar(13)

    list(c1, c2)
  }
}

TestController.scala

package com.lab.test
package controller

import domain.Cigarette
import service.TestService

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.{GetMapping, RestController}

import java.util
import scala.jdk.CollectionConverters._

@RestController
class TestController(@Autowired val testService: TestService) {
  @GetMapping(Array("/buy"))
  def listCigarettes(): util.List[Cigarette] = {
    testService.listCigarette().asJava
  }
}

TestApplication.scala

package com.lab.test

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication
class TestApplication

object TestApplication extends App {
  SpringApplication.run(classOf[TestApplication], args: _*)
}

build.sbt

name := "testsbt"

version := "0.1"
scalaVersion := "2.13.4"

idePackagePrefix := Some("com.lab.test")
mainClass in(Compile, run) := Some("com.lab.test.TestApplication")

scalacOptions ++= Seq(
  "-encoding", "utf8",
  "-unchecked",
  "-feature",
  "-deprecation"
)

javacOptions ++= Seq(
  "-source", "1.8",
  "-target", "1.8",
  "-encoding", "utf8",
  "-Lint:unchecked",
  "-Lint:deprecation",
  "-g"
)

val sbVersion = "2.3.7.RELEASE"

libraryDependencies ++= Seq(
  "org.springframework.boot" % "spring-boot-starter-web" % sbVersion
)

Run

image.png

image.png


标题:Scala and SBT and Spring Boot
作者:uid1024
地址:http://javadaily.cn/articles/2021/01/23/1611408841359.html