scala 整合 springboot
新建spingboot项目
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.0</version><relativePath/> </parent><groupId>cn.lihaozhe</groupId><artifactId>scala-boot</artifactId><version>1.0</version><name>scala-boot</name><description>scala-boot</description><properties><jdk.version>21</jdk.version><maven.compiler.source>21</maven.compiler.source><maven.compiler.target>21</maven.compiler.target><maven.compiler.compilerVersion>21</maven.compiler.compilerVersion><maven.compiler.encoding>utf-8</maven.compiler.encoding><project.build.sourceEncoding>utf-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><maven.test.failure.ignore>true</maven.test.failure.ignore><maven.test.skip>true</maven.test.skip><commons-dbutils.version>1.8.1</commons-dbutils.version><commons-io.version>2.14.0</commons-io.version><commons-lang3.version>3.13.0</commons-lang3.version><druid.version>1.2.20</druid.version><fastjson.version>2.0.41</fastjson.version><gson.version>2.10.1</gson.version><hutool.version>5.8.22</hutool.version><jackson.version>2.15.3</jackson.version><junit.version>5.10.0</junit.version><lombok.version>1.18.30</lombok.version><mysql.version>8.2.0</mysql.version><scala.version>2.13.12</scala.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.20</version></dependency><dependency><groupId>org.scala-tools.testing</groupId><artifactId>specs_2.10</artifactId><version>1.6.9</version><scope>test</scope></dependency><dependency><groupId>org.scalatest</groupId><artifactId>scalatest_2.13</artifactId><version>3.2.15</version><scope>test</scope></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>${junit.version}</version><scope>test</scope></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-engine</artifactId><version>${junit.version}</version><scope>test</scope></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>${hutool.version}</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>${commons-lang3.version}</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>${commons-io.version}</version></dependency><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>${gson.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>${fastjson.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.datatype</groupId><artifactId>jackson-datatype-jsr310</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>com.github.binarywang</groupId><artifactId>java-testdata-generator</artifactId><version>1.1.2</version></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>${mysql.version}</version></dependency><dependency><groupId>org.hibernate.orm</groupId><artifactId>hibernate-core</artifactId><version>6.3.1.Final</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin><plugin><groupId>net.alchim31.maven</groupId><artifactId>scala-maven-plugin</artifactId><version>4.8.1</version><configuration><scalaCompatVersion>${scala.version}</scalaCompatVersion><scalaVersion>${scala.version}</scalaVersion></configuration><executions><execution><id>scala-compile-first</id><phase>process-resources</phase><goals><goal>compile</goal></goals></execution><execution><goals><goal>testCompile</goal></goals></execution><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></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><version>3.5.0</version><configuration><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin></plugins></build></project>
创建数据库恢复数据
create database knowledge;use knowledge;
编写 pojo 类
package cn.lihaozhe.pojoimport jakarta.persistence.{Column, Entity, GeneratedValue, GenerationType, Id, Table}import scala.beans.BeanProperty/** * @author 李昊哲 * @version 1.0 */@Entity@Table(name = "dujitang")class Dujitang {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "id")@BeanPropertyvar id: Long = _@Column(name = "text")@BeanPropertyvar text: String = _override def toString: String = s"$id\t$text"}
package cn.lihaozhe.pojoimport jakarta.persistence.{Column, Entity, GeneratedValue, GenerationType, Id, Table}import scala.beans.BeanProperty/** * @author 李昊哲 * @version 1.0 */@Entity@Table(name = "cy")class Cy {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "id")@BeanPropertyvar id: Long = _@Column(name = "name")@BeanPropertyvar name: String = _@Column(name = "spell")@BeanPropertyvar spell: String = _@Column(name = "content")@BeanPropertyvar content: String = _@Column(name = "derivation")@BeanPropertyvar derivation: String = _@Column(name = "samples")@BeanPropertyvar samples: String = _}
编写持久层
package cn.lihaozhe.repositoryimport cn.lihaozhe.pojo.Dujitangimport org.springframework.data.jpa.repository.{JpaRepository, JpaSpecificationExecutor}/** * @author 李昊哲 * @version 1.0 */trait DujitangRepository extends JpaRepository[Dujitang, Long] with JpaSpecificationExecutor[Dujitang] {}
package cn.lihaozhe.repositoryimport cn.lihaozhe.pojo.Cyimport org.springframework.data.jpa.repository.{JpaRepository, JpaSpecificationExecutor}/** * @author 李昊哲 * @version 1.0 */trait CyRepository extends JpaRepository[Cy, Long] with JpaSpecificationExecutor[Cy] {}
编写业务层
package cn.lihaozhe.serviceimport cn.lihaozhe.pojo.Dujitang/** * @author 李昊哲 * @version 1.0 */trait DujitangService {/** * 随机生成一条鸡汤 * * @return 鸡汤文案 */def random(): Dujitang}
package cn.lihaozhe.serviceimport cn.lihaozhe.pojo.Cyimport org.springframework.data.domain.Page/** * @author 李昊哲 * @version 1.0 */trait CyService {/** * 分页查找 成语 * * @param word 成语关键字 * @param pageNum查询页面号 * @param pageSize 每页记录数 * @return 成语列表 */def page(word: String, pageNum: Int, pageSize: Int): Page[Cy]}
package cn.lihaozhe.service.implimport cn.lihaozhe.pojo.Dujitangimport cn.lihaozhe.repository.DujitangRepositoryimport cn.lihaozhe.service.DujitangServiceimport org.springframework.stereotype.Serviceimport scala.util.Random/** * @author 李昊哲 * @version 1.0 */@Serviceclass DujitangServiceImpl(dujitangRepository: DujitangRepository) extends DujitangService {/** * 随机生成一条鸡汤 * * @return 鸡汤文案 */override def random(): Dujitang = {val dujitangList = dujitangRepository.findAll()val index = new Random().nextInt(dujitangList.size())dujitangList.get(index)}}
package cn.lihaozhe.service.implimport cn.lihaozhe.pojo.Cyimport cn.lihaozhe.repository.CyRepositoryimport cn.lihaozhe.service.CyServiceimport jakarta.persistence.criteria.{CriteriaBuilder, CriteriaQuery, Predicate, Root}import org.apache.commons.lang3.StringUtilsimport org.springframework.data.domain.{Page, PageRequest, Sort}import org.springframework.data.jpa.domain.Specificationimport org.springframework.stereotype.Service/** * @author 李昊哲 * @version 1.0 */@Serviceclass CyServiceImpl(cyRepository: CyRepository) extends CyService {/** * 分页查找 成语 * * @param word 成语关键字 * @param pageNum查询页面号 * @param pageSize 每页记录数 * @return 成语列表 */override def page(word: String, pageNum: Int, pageSize: Int): Page[Cy] = {if (StringUtils.isBlank(word)) {cyRepository.findAll(PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Direction.ASC, "id")))} else {val sb = new StringBuildersb.append("%")sb.append(word)sb.append("%")cyRepository.findAll(new Specification[Cy] {override def toPredicate(root: Root[Cy], query: CriteriaQuery[_], cb: CriteriaBuilder): Predicate = cb.like(root.get("name").as(classOf[String]), sb.toString())}, PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Direction.ASC, "id")))}}}
编写数据接口
package cn.lihaozhe.controllerimport cn.lihaozhe.pojo.Dujitangimport cn.lihaozhe.service.DujitangServiceimport org.springframework.web.bind.annotation.{GetMapping, RequestMapping, RestController}/** * @author 李昊哲 * @version 1.0 */@RestController@RequestMapping(value = Array("/dujitang"))class DujitangController(dujitangService: DujitangService) {@GetMapping(Array("/random"))def random(): Dujitang = dujitangService.random()}
package cn.lihaozhe.controllerimport cn.lihaozhe.service.CyServiceimport org.springframework.web.bind.annotation.{GetMapping, RequestMapping, RestController}/** * @author 李昊哲 * @version 1.0 */@RestController@RequestMapping(Array("/cy"))class CyController(cyService: CyService) {@GetMapping(Array("/page"))def page(word: String, pageNum: Int = 1, pageSize: Int = 5) = cyService.page(word, pageNum, pageSize)}
application.yml
# 服务器配置server:port: 6633servlet:context-path: /session:timeout: 30mcookie:max-age: 3600encoding:charset: UTF-8force: true# spring配置spring:application:name: scala-bootmvc:format:date: yyyy-MM-dddate-time: yyyy-MM-dd HH:mm:sspath match:matching-strategy: ant_path_matcherservlet:multipart:enabled: truemax-file-size: 50MBmax-request-size: 200MBjackson:serialization:FAIL_ON_EMPTY_BEANS: false# json 序列化排除值为 null 的属性default-property-inclusion: non_null# 配置 Date 类的时间格式,如果不涉及可以不加date-format: yyyy-MM-dd HH:mm:ss# 配置 Date 类的时区,如果不涉及可以不加time-zone: GMT+8datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql:///knowledge" />&characterEncoding=UTF8&useSSL=false&useServerPrepStmts=false&rewriteBatchedStatements=true&cachePrepStmts=true&allowMultiQueries=true&serverTimeZone=Aisa/Shanghaiusername: rootpassword: lihaozhetype: com.alibaba.druid.pool.DruidDataSourcedruid:# 下面为连接池的补充设置,应用到上面所有数据源中# 初始化大小,最小,最大initial-size: 5min-idle: 5max-active: 200# 配置获取连接等待超时的时间max-wait: 60000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒time-between-eviction-runs-millis: 60000# 配置一个连接在池中最小生存的时间,单位是毫秒min-evictable-idle-time-millis: 300000validation-query: SELECT 1 FROM DUALtest-while-idle: truetest-on-borrow: falsetest-on-return: false# 打开PSCache,并且指定每个连接上PSCache的大小pool-prepared-statements: true# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙max-pool-prepared-statement-per-connection-size: 20filters: stat,wall,slf4juse-global-data-source-stat: true# 通过connectProperties属性来打开mergeSql功能;慢SQL记录# connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000stat-view-servlet:login-username: adminlogin-password: 123456reset-enable: falseurl-pattern: /druid/*allow: 0.0.0.0#deny:enabled: trueweb-stat-filter:url-pattern: /*exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"jpa:database: MySQLdatabase-platform: org.hibernate.dialect.MySQL8Dialectshow-sql: trueopen-in-view: truehibernate:ddl-auto: update#自动创建数据建库表(hibernate.hbm2ddl.auto):#create:程序运行时创建数据库表(如果有表,先删除表再创建)#update:程序运行时创建数据库表(如果有表,不会创建表)#none:不会创建数据库lhz:project:title: 李昊哲-小课slogan: 桃李不言下自成蹊map: { "B": "https://space.bilibili.com/480308139","C": "https://blog.csdn.net/qq_24330181" }list:- https://space.bilibili.com/480308139- https://blog.csdn.net/qq_24330181lhzSite:name: 李昊哲-小课url: https://space.bilibili.com/480308139siteList:- name: 李昊哲-小课 B站url: https://space.bilibili.com/480308139- name: 李昊哲-小课 C站url: https://blog.csdn.net/qq_24330181
启动 springboot 项目
浏览器测试