Scala是一门多范式的编程语言,一种类似Java的编程语言,设计初衷是实现可伸缩的语言并集成面向对象编程。Scala把Erlang风格的基于actor的并发带进了JVM,开发者可以利用Scala的actor模型在JVM上设计具伸缩性的并发应用程序,它会自动获得多核心处理器带来的优势,而不必依照复杂的Java线程模型来编写程序,接下来就介绍一下如何在SpringBoot框架中使用Scala来进行简单的Web开发,对scala不了解的建议先去学习基础哦
一、导入依赖
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://.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("删除成功") }}
这样我们一个简单的Scala版本的Web项目就写好啦,只需要启动就可以试着运行啦,本文的源代码地址:scala-demo,有任何不清楚的可以在评论区回复哈
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。