spring boot+scala编写web接口

本人是Java开发者,有面向对象的基础,而Scala也是面向对象的语言,学习后可快速入门。通过学习Scala的面向对象(和java面向对象类似)、Scala的高级函数(map,reduce等,和Java8中的stream编程类似)、Scala的隐式转换(在Java中可通过spring aop实现增强,Scala的隐式转换较为方便)、Scala的模式匹配(类似Java的switch语句,但使用的访问很广)。这里通过Scala结合spring boot来实现spring mvc接口的开发。

添加pom依赖

先搭建spring boot项目,这里不细说

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--添加Scala依赖-->
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>

        <!--添加jpa的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!--添加MySQL驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <!--添加Scala的plugin-->
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>compile-scala</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile-scala</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <recompileMode>incremental</recompileMode>
                    <scalaVersion>${scala.version}</scalaVersion>/.,bvc;
                    <args>
                        <arg>-deprecation</arg>
                    </args>
                    <jvmArgs>
                        <jvmArg>-Xms64m</jvmArg>
                        <jvmArg>-Xmx1024m</jvmArg>
                    </jvmArgs>
                </configuration>
            </plugin>

        </plugins>
    </build>

其中Scala的plugin用于编译、测试、打包scala的程序

配置

server:
  port: 7777
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver  #数据库驱动
    url: jdbc:mysql://localhost:3306/test?useSSL=false #本地数据库url,先在本地数据库中建立test这个库
    username: root  #数据库用户名
    password: 191016  #数据库密码
    
  jpa:
    hibernate:
      ddl-auto: update  #每次运行程序,没有表格会新建表格,表内有数据不会清空,只会更新
    database: mysql

项目结构

16565243-8acbdc4abac2ef93.png
image.png

其中controller层为程序入口
domain层为实体类
service层为业务逻辑层,提供事务控制
repository层为数据持久化层

实体类

@Entity
@Table
class Person {
  @Id
  @GeneratedValue
  @BeanProperty
  var id:Integer = _

  @BeanProperty
  var name:String = _

  @BeanProperty
  var sex:String = _
}

scala中无get/set方法

repository持久化层

trait PersonRepository extends CrudRepository[Person,Integer]{

}

trait类似于Java中接口的含义,这里继承jpa的基本Repository

service层

@Service
class PersonService @Autowired()(personRepository: PersonRepository) {
  /**
    * 保存
    *
    * @param person 保存对象
    * @return Person
    */
  @Transactional
  def save(person: Person): Person = {
    personRepository.save(person)
  }

  /**
    * 根据Id查询
    *
    * @param id 查询参数
    * @return Person
    */
  def selectPersonById(id: Integer): Person = {
    personRepository.findOne(id)
  }
}

这里的自动注入的方式和java中不相同,是写在类名的后面

controller层

@RestController
@RequestMapping(Array("/v1/person"))
class PersonController @Autowired()(personService: PersonService) {
  @PostMapping
  def save(@RequestBody person: Person): Person = {
    personService.save(person)
  }

 @GetMapping
  def selectPersonById(@RequestParam id: Integer): Person = {
    personService.selectPersonById(id)
  }
}

这里的映射路径和java中不同,必须传一个数组,而java中是传递一个字符串

测试

启动项目,通过postman测试
保存:


16565243-f3b36fdf83c84752.png
image.png

查看数据库,保存成功。


16565243-b12a4f36400e7fe3.png
image.png

查询:
16565243-9d794875ac08c78f.png
image.png

注意

Java和Scala可以相互调用,如Java写的工具类,在Scala可直接使用,不用在重新写一套Scala的工具类,反之亦然。

转载于:https://www.jianshu.com/p/9c7ee8a0ddc9

猜你喜欢

转载自blog.csdn.net/weixin_33733810/article/details/91152200