SpringBoot系列之集成Scala开发API接口
Posted smileNicky
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot系列之集成Scala开发API接口相关的知识,希望对你有一定的参考价值。
SpringBoot系列之集成Scala开发API接口
最近需要用scala去写一些数据同步的程序,结合ETL实现,因为不熟悉scala语法,所以想到scala里结合springboot框架,快速开发,并没有系统学习scala,有些代码可能不够精简,有问题欢迎提出
后端主要技术栈:
- Scala 2.11.12
- JDK 1.8
- SpringBoot 2.2.1.RELEASE
- Mybatis Plus 3.4.3.4
1、项目环境搭建
使用阿里脚手架快速创建一个SpringBoot项目:链接:https://start.aliyun.com/bootstrap.html
首先,需要知道scala
文件经过编译后也是会生成.class
文件的,在maven配置文件里也需要加上一些配置maven-scala-plugin
,保证scala程序正常编译,配置文件相对比较复杂,读者可以参考
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>springboot-scala</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-scala</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.2.1.RELEASE</spring-boot.version>
<mybatis.plus.version>3.4.3.4</mybatis.plus.version>
<dynamic.datasource.version>3.4.1</dynamic.datasource.version>
<mysql.connector.version>5.1.27</mysql.connector.version>
<hutool.all.version>5.7.11</hutool.all.version>
<scala.version>2.11.12</scala.version>
<scala.compat.version>2.11</scala.compat.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>$mybatis.plus.version</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>$dynamic.datasource.version</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter-test</artifactId>
<version>$mybatis.plus.version</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>$mysql.connector.version</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>$hutool.all.version</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>$scala.version</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>$scala.version</version>
</dependency>
<dependency>
<groupId>com.typesafe.scala-logging</groupId>
<artifactId>scala-logging-slf4j_2.11</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2-core_$scala.compat.version</artifactId>
<version>2.4.16</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_$scala.compat.version</artifactId>
<version>2.2.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>$spring-boot.version</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<recompileMode>incremental</recompileMode>
<scalaVersion>$scala.version</scalaVersion>
<launchers>
<launcher>
<id>app</id>
<mainClass>com.example.scala.ScalaExamplApplication</mainClass>
<args>
<arg>-deprecation</arg>
</args>
<jvmArgs>
<jvmArg>-Xms64m</jvmArg>
<jvmArg>-Xmx1024m</jvmArg>
</jvmArgs>
</launcher>
</launchers>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.1.RELEASE</version>
<configuration>
<mainClass>com.example.scala.ScalaExamplApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<configuration>
<scalaVersion>$scala.version</scalaVersion>
</configuration>
</plugin>
</plugins>
</reporting>
<repositories>
<repository>
<id>scala-tools.org</id>
<name>Scala-Tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>scala-tools.org</id>
<name>Scala-Tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url>
</pluginRepository>
</pluginRepositories>
</project>
加上配置,主要配置数据源和mybatis plus
spring:
datasource:
dynamic:
primary: shop #设置默认的数据源或者数据源组,默认值即为master
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
shop:
url: jdbc:mysql://127.0.0.1:3306/shop?useUnicode=true&characterEncoding=utf-8
username: root
password:
driver-class-name: com.mysql.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
schema: classpath:db/schema-mysql.sql
data: classpath:db/data-mysql.sql
slave_1:
url: jdbc:mysql://127.0.0.1:3306/canaltest?useUnicode=true&characterEncoding=utf-8
username: root
password:
driver-class-name: com.mysql.jdbc.Driver
mybatis-plus:
type-aliases-package: com.example.scala.*.*.model
mapper-locations: classpath*:mapper/*/*.xml
configuration:
map-underscore-to-camel-case: true
default-statement-timeout: 60
cache-enabled: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Scala的Application类,也是调用SpringBoot的SpringApplication
类
package com.example.scala
import org.springframework.boot.SpringApplication
object ScalaExamplApplication extends App
SpringApplication.run(classOf[AppConfiguration]);
引入@SpringBootApplication
package com.example.scala
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
class AppConfiguration
实体类,使用了MyBatis Plus的一些注解,可以直接使用,要保证实体类正常转json数据返回给前端,加上@BeanProperty
注解
package com.example.scala.model
import com.baomidou.mybatisplus.annotation.TableId, TableName
import javax.validation.constraints.Email, NotBlank, NotEmpty
import lombok.Data, ToString
import scala.beans.BeanProperty
@Data
@TableName(value = "user")
@ToString
class User
@TableId
@BeanProperty
var id : Integer = _
@BeanProperty
@NotBlank(message = "用户名必须填!")
var name : String = _
@BeanProperty
var age : Integer = _
@BeanProperty
@Email(message = "邮箱格式不对")
var email : String = _
scala的接口要用trait
标记
package com.example.scala.dao
import com.baomidou.mybatisplus.core.mapper.BaseMapper
import com.example.scala.model.User
import org.apache.ibatis.annotations.Mapper
@Mapper
trait UserDao extends BaseMapper[User]
业务api:
package com.example.scala.service
import com.baomidou.mybatisplus.extension.service.IService
import com.example.scala.model.User
trait IUserService extends IService[User]
继承接口scala里用with
关键字
package以上是关于SpringBoot系列之集成Scala开发API接口的主要内容,如果未能解决你的问题,请参考以下文章
springboot系列十springboot集成swaggerUI