栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot系列之集成Scala开发API接口

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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程序正常编译,配置文件相对比较复杂,读者可以参考



    4.0.0
    com.example
    springboot-scala
    0.0.1-SNAPSHOT
    springboot-scala
    Demo project for Spring Boot


    
        1.8
        UTF-8
        UTF-8
        2.2.1.RELEASE
        3.4.3.4
        3.4.1
        5.1.27
        5.7.11
        2.11.12
        2.11
    

    

        
            org.springframework.boot
            spring-boot-starter-validation
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.baomidou
            mybatis-plus-boot-starter
            ${mybatis.plus.version}
        
        
            com.baomidou
            dynamic-datasource-spring-boot-starter
            ${dynamic.datasource.version}
        
        
            com.baomidou
            mybatis-plus-boot-starter-test
            ${mybatis.plus.version}
        

        
            mysql
            mysql-connector-java
            ${mysql.connector.version}
        

        
            cn.hutool
            hutool-all
            ${hutool.all.version}
        

        
            org.projectlombok
            lombok
            true
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        

        
            org.scala-lang
            scala-library
            ${scala.version}
        
        
            org.scala-lang
            scala-compiler
            ${scala.version}
        
        
            com.typesafe.scala-logging
            scala-logging-slf4j_2.11
            2.1.2
        
        
            org.specs2
            specs2-core_${scala.compat.version}
            2.4.16
            test
        
        
            org.scalatest
            scalatest_${scala.compat.version}
            2.2.4
            test
        

    

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    
        src/main/scala
        src/test/scala
        
            
                org.scala-tools
                maven-scala-plugin
                
                    
                        
                            compile
                            testCompile
                        
                    
                
                
                    incremental
                    ${scala.version}
                    
                        
                            app
                            com.example.scala.ScalaExamplApplication
                            
                                -deprecation
                            
                            
                                -Xms64m
                                -Xmx1024m
                            
                        
                    
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.2.1.RELEASE
                
                    com.example.scala.ScalaExamplApplication
                
                
                    
                        repackage
                        
                            repackage
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    1.8
                    1.8
                    UTF-8
                
            

        
    

    
        
            
                org.scala-tools
                maven-scala-plugin
                
                    ${scala.version}
                
            
        
    

    
        
            scala-tools.org
            Scala-Tools Maven2 Repository
            http://scala-tools.org/repo-releases
        
    

    
        
            scala-tools.org
            Scala-Tools Maven2 Repository
            http://scala-tools.org/repo-releases
        
    



加上配置,主要配置数据源和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 com.example.scala.service.impl

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.example.scala.dao.UserDao
import com.example.scala.model.User
import com.example.scala.service.IUserService
import org.springframework.stereotype.Service

@Service
class UserServiceImpl extends ServiceImpl[UserDao , User] with IUserService{

}

语法和java写法不太一样,声明方法用def ,@Autowired和返回参数,语法的可以对比一下即可

package com.example.scala.controller

import com.example.scala.model.User
import com.example.scala.service.IUserService
import javax.validation.Valid
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation._

@RestController
class UserController @Autowired()(var iUserSerive: IUserService){

  @GetMapping(value = {
    Array("/users/{id}")
  })
  def getUser(@PathVariable(value = "id") id: Integer): User  = {
    iUserSerive.getById(id)
  }

  @PostMapping(value = {
    Array("/users")
  })
  def save(@Valid @RequestBody user: User) = {
    iUserSerive.save(user);
  }

}

接口写好之后,同样配个application运行实例即可,在linux里使用curl http://127.0.0.1:8080/users/1调用接口,window系统直接使用postman调用测试接口即可

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/820909.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号