SpringBoot如何使用Scala进行开发的实现
Scala是一门多范式的编程语言,一种类似Java的编程语言,设计初衷是实现可伸缩的语言并集成面向对象编程。Scala把Erlang风格的基于actor的并发带进了JVM,开发者可以利用Scala的actor模型在JVM上设计具伸缩性的并发应用程序,它会自动获得多核心处理器带来的优势,而不必依照复杂的Java线程模型来编写程序,接下来就介绍一下如何在SpringBoot框架中使用Scala来进行简单的Web开发,对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 DemoprojectforSpringBoot 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:8080 spring: 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.MySQL5InnoDBDialect swagger: base-package:com.gjing.project.scala.controller title:scala学习的demo
三、创建实体类
importjavax.persistence._
importscala.beans.BeanProperty
/**
*@authorGjing
**/
@Entity
@Table(name="scala_customer")
classCustomer{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@BeanProperty
varid:Integer=_
@BeanProperty
varcustomerName:String=_
defthis(customerName:String){
this()
this.customerName=customerName
}
overridedeftoString:String=s"Customer($id,$customerName)"
}
这块和我们用java开发没啥不同,只是@BeanProperty注解会帮我们生成get和set
四、Repository层
importcom.gjing.project.scala.entity.Customer
importorg.springframework.data.jpa.repository.JpaRepository
importorg.springframework.stereotype.Repository
/**
*@authorGjing
**/
@Repository
traitCustomerRepositoryextendsJpaRepository[Customer,Integer]{
/**
*通过用户名查询
*@paramname用户名
*@returnCustomer
*/
deffindByCustomerName(name:String):Customer
}
这里和JAVA不同的是泛型采用的是[]中括号,这点要注意
五、Service层
importcn.gjing.tools.common.result.PageResult
importcom.gjing.project.scala.entity.Customer
importcom.gjing.project.scala.exceptions.MyServiceException
importcom.gjing.project.scala.repository.CustomerRepository
importjavax.annotation.Resource
importorg.springframework.data.domain.Pageable
importorg.springframework.stereotype.Service
/**
*@authorGjing
**/
@Service
classCustomerService@Resource()(customerRepository:CustomerRepository){
/**
*保存用户
*
*@paramname用户名
*/
defsaveCustomer(name:String):Unit={
varcustomer=customerRepository.findByCustomerName(name)
if(customer!=null){
throwMyServiceException("添加失败,用户已存在")
}
customer=newCustomer(name)
customerRepository.save(customer)
}
/**
*分页查询
*
*@parampageable分页对象
*@return
*/
defpageCustomer(pageable:Pageable):PageResult[java.util.List[Customer]]={
valpage=customerRepository.findAll(pageable)
returnPageResult.of(page.getContent,page.getTotalPages,page.getSize,page.getTotalElements,page.getNumber)
}
/**
*更新用户名
*@paramid用户id
*@paramname用户名
*/
defupdateCustomer(id:Integer,name:String):Unit={
valcustomer=customerRepository.findById(id).orElseThrow(()=>MyServiceException("更新失败,用户不存在"))
customer.setCustomerName(name)
customerRepository.saveAndFlush(customer)
}
/**
*删除指定用户
*@paramid用户id
*/
defdeleteCustomer(id:Integer):Unit={
valcustomer=customerRepository.findById(id).orElseThrow(()=>MyServiceException("删除失败,用户不存在"))
customerRepository.delete(customer)
}
}
有意思的是,在scala中依赖注入是写在类名上的
六、Controller层
importcn.gjing.tools.common.annotation.NotEmpty
importcn.gjing.tools.common.result.PageResult
importcom.gjing.project.scala.entity.Customer
importcom.gjing.project.scala.service.CustomerService
importio.swagger.annotations.{Api,ApiImplicitParam,ApiImplicitParams,ApiOperation}
importjavax.annotation.Resource
importorg.springframework.data.domain.PageRequest
importorg.springframework.http.ResponseEntity
importorg.springframework.web.bind.annotation._
/**
*@authorGjing
**/
@RestController
@Api(tags=Array("用户的相关功能"))
classCustomerController@Resource()(customerService:CustomerService){
@PostMapping(Array("/customer"))
@ApiOperation("添加用户")
@ApiImplicitParam(name="customerName",value="用户名",dataType="String",required=true,paramType="query")
@NotEmpty
defsaveCustomer(customerName:String):ResponseEntity[String]={
customerService.saveCustomer(customerName)
ResponseEntity.ok("添加成功")
}
@GetMapping(Array("/customer_page"))
@ApiOperation("分页查询")
@ApiImplicitParams(Array(
newApiImplicitParam(name="page",value="页数",required=true,dataType="int",paramType="query"),
newApiImplicitParam(name="size",value="条数",required=true,dataType="int",paramType="query"),
))
defpageCustomer(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(
newApiImplicitParam(name="id",value="用户ID",required=true,dataType="int",paramType="query"),
newApiImplicitParam(name="name",value="用户名",required=true,dataType="String",paramType="query")
))
defupdateCustomer(id:Integer,name:String):ResponseEntity[String]={
customerService.updateCustomer(id,name)
ResponseEntity.ok("修改成功")
}
@DeleteMapping(Array("/customer/{id}"))
@ApiOperation("删除用户")
defdeleteCustomer(id:Integer):ResponseEntity[String]={
customerService.deleteCustomer(id)
ResponseEntity.ok("删除成功")
}
}
这样我们一个简单的Scala版本的Web项目就写好啦,只需要启动就可以试着运行啦,本文的源代码地址:scala-demo,有任何不清楚的可以在评论区回复哈
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。