当前位置 主页 > 服务器问题 > Linux/apache问题 >
通过上面我们可以发现,和创建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
三、创建实体类
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.Customer import org.springframework.data.jpa.repository.JpaRepository import org.springframework.stereotype.Repository /** * @author Gjing **/ @Repository trait CustomerRepository extends JpaRepository[Customer, Integer] { /** * 通过用户名查询 * @param name 用户名 * @return Customer */ def findByCustomerName(name:String) : Customer }
这里和JAVA不同的是泛型采用的是[]中括号,这点要注意
五、Service层
import cn.gjing.tools.common.result.PageResult import com.gjing.project.scala.entity.Customer import com.gjing.project.scala.exceptions.MyServiceException import com.gjing.project.scala.repository.CustomerRepository import javax.annotation.Resource import org.springframework.data.domain.Pageable import org.springframework.stereotype.Service /** * @author Gjing **/ @Service class 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) } }