成都创新互联网站制作重庆分公司

SpringBoot中如何使用Scala

SpringBoot中如何使用Scala,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

站在用户的角度思考问题,与客户深入沟通,找到益阳网站设计与益阳网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计制作、成都网站设计、企业官网、英文网站、手机端网站、网站推广、域名注册、网页空间、企业邮箱。业务覆盖益阳地区。

一、导入依赖

4.0.0  org.springframework.boot  spring-boot-starter-parent  2.2.1.RELEASE   com.gjing.project scala-demo 0.0.1-SNAPSHOT scala-demo Demo project for Spring Boot  1.8     org.springframework.boot   spring-boot-starter-web       org.springframework.boot   spring-boot-starter-data-jpa       MySQL   mysql-connector-java         org.scala-lang   scala-library   2.13.1       cn.gjing   tools-starter-swagger   1.3.0       cn.gjing   tools-common   1.2.7            org.scala-tools    maven-scala-plugin    2.15.2                     compile       testCompile                      org.springframework.boot    spring-boot-maven-plugin    

通过上面我们可以发现,和创建Java版本的SpringBoot项目没啥不同,只是引入了scala-library这个我们之前没引入的包,同时增加了对scala编译的插件

二、配置YML文件

server: port: 8080spring: application: name: scala-demo datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=utf8&useSSL=false username: root password: root type: com.zaxxer.hikari.HikariDataSource hikari:  maximum-pool-size: 5  minimum-idle: 1  idle-timeout: 30000  connection-timeout: 30000 jpa: database: mysql hibernate:  ddl-auto: update # 设置创表引擎为Innodb,不然默认为MyiSam database-platform: org.hibernate.dialect.MySQL5InnoDBDialectswagger: base-package: com.gjing.project.scala.controller title: scala学习的demo

三、创建实体类

import javax.persistence._import scala.beans.BeanProperty/** * @author Gjing **/@Entity@Table(name = "scala_customer")class Customer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @BeanProperty var id:Integer = _ @BeanProperty var customerName:String = _ def this(customerName:String){ this() this.customerName = customerName } override def toString: String = s"Customer($id,$customerName)"}

这块和我们用java开发没啥不同,只是@BeanProperty注解会帮我们生成get和set

四、Repository层

import com.gjing.project.scala.entity.Customerimport org.springframework.data.jpa.repository.JpaRepositoryimport org.springframework.stereotype.Repository/** * @author Gjing **/@Repositorytrait CustomerRepository extends JpaRepository[Customer, Integer] { /** * 通过用户名查询 * @param name 用户名 * @return Customer */ def findByCustomerName(name:String) : Customer}

这里和JAVA不同的是泛型采用的是[]中括号,这点要注意

五、Service层

import cn.gjing.tools.common.result.PageResultimport com.gjing.project.scala.entity.Customerimport com.gjing.project.scala.exceptions.MyServiceExceptionimport com.gjing.project.scala.repository.CustomerRepositoryimport javax.annotation.Resourceimport org.springframework.data.domain.Pageableimport org.springframework.stereotype.Service/** * @author Gjing **/@Serviceclass CustomerService @Resource()(customerRepository: CustomerRepository) { /** * 保存用户 * * @param name 用户名 */ def saveCustomer(name: String): Unit = { var customer = customerRepository.findByCustomerName(name) if (customer != null) {  throw MyServiceException("添加失败,用户已存在") } customer = new Customer(name) customerRepository.save(customer) } /** * 分页查询 * * @param pageable 分页对象 * @return */ def pageCustomer(pageable: Pageable): PageResult[java.util.List[Customer]] = { val page = customerRepository.findAll(pageable) return PageResult.of(page.getContent, page.getTotalPages, page.getSize, page.getTotalElements, page.getNumber) } /** * 更新用户名 * @param id 用户id * @param name 用户名 */ def updateCustomer(id: Integer, name: String): Unit = { val customer = customerRepository.findById(id).orElseThrow(() => MyServiceException("更新失败,用户不存在")) customer.setCustomerName(name) customerRepository.saveAndFlush(customer) } /** * 删除指定用户 * @param id 用户id */ def deleteCustomer(id:Integer): Unit = { val customer = customerRepository.findById(id).orElseThrow(() => MyServiceException("删除失败,用户不存在")) customerRepository.delete(customer) }}

有意思的是,在scala中依赖注入是写在类名上的

六、Controller层

import cn.gjing.tools.common.annotation.NotEmptyimport cn.gjing.tools.common.result.PageResultimport com.gjing.project.scala.entity.Customerimport com.gjing.project.scala.service.CustomerServiceimport io.swagger.annotations.{Api, ApiImplicitParam, ApiImplicitParams, ApiOperation}import javax.annotation.Resourceimport org.springframework.data.domain.PageRequestimport org.springframework.http.ResponseEntityimport org.springframework.web.bind.annotation._/** * @author Gjing **/@RestController@Api(tags = Array("用户的相关功能"))class CustomerController @Resource()(customerService:CustomerService){ @PostMapping(Array("/customer")) @ApiOperation("添加用户") @ApiImplicitParam(name = "customerName",value = "用户名",dataType = "String",required = true,paramType = "query") @NotEmpty def saveCustomer(customerName:String): ResponseEntity[String] ={ customerService.saveCustomer(customerName) ResponseEntity.ok("添加成功") } @GetMapping(Array("/customer_page")) @ApiOperation("分页查询") @ApiImplicitParams(Array( new ApiImplicitParam(name = "page",value = "页数",required = true,dataType = "int",paramType = "query"), new ApiImplicitParam(name = "size",value = "条数",required = true,dataType = "int",paramType = "query"), )) def pageCustomer(page:Integer,size:Integer): ResponseEntity[PageResult[java.util.List[Customer]]]={ ResponseEntity.ok(customerService.pageCustomer(PageRequest.of(page, size))) } @NotEmpty @PutMapping(Array("/customer")) @ApiOperation("更新用户") @ApiImplicitParams(Array( new ApiImplicitParam(name = "id",value = "用户ID",required = true,dataType = "int",paramType = "query"), new ApiImplicitParam(name = "name",value = "用户名",required = true,dataType = "String",paramType = "query") )) def updateCustomer(id:Integer,name:String): ResponseEntity[String] = { customerService.updateCustomer(id, name) ResponseEntity.ok("修改成功") } @DeleteMapping(Array("/customer/{id}")) @ApiOperation("删除用户") def deleteCustomer(id:Integer): ResponseEntity[String] = { customerService.deleteCustomer(id) ResponseEntity.ok("删除成功") }}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


网站标题:SpringBoot中如何使用Scala
本文来源:http://cxhlcq.com/article/jegsgd.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部