项目结构
pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<scala.version>2.11.8</scala.version>
<mysql.version>5.1.44</mysql.version>
<mybatis.spring.version>1.3.3</mybatis.spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<!--数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<recompileMode>incremental</recompileMode>
<scalaVersion>${scala.version}</scalaVersion>
<launchers>
<launcher>
<id>app</id>
<mainClass>com.staryea.scala.ScalaApplication</mainClass>
<args>
<arg>-deprecation</arg>
</args>
<jvmArgs>
<jvmArg>-Xms256m</jvmArg>
<jvmArg>-Xmx2048m</jvmArg>
</jvmArgs>
</launcher>
</launchers>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
启动类
/**
* @author: hs
* @Date: 2019/10/30 14:51
* @Description: springboot 启动类
*/
@SpringBootApplication
class AppConfig
object ScalaApplication extends App {
SpringApplication.run(classOf[AppConfig])
}
Dao层
/**
* @author: hs
* @Date: 2019/10/30 16:52
* @Description:
*/
@Mapper
trait UserMapper {
def getUserInfo(@Param("id") id: Int):User
}
mapper文件(UserMapper)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.staryea.scala.dao.UserMapper">
<select id="getUserInfo" resultType="com.staryea.scala.entity.User">
select * from `user`
<where>
<if test="id != null and id !='' ">
id=#{id}
</if>
</where>
</select>
</mapper>
Service层
/**
* @author: hs
* @Date: 2019/10/30 18:04
* @Description:
*/
trait UserService {
def getUserInfo(id:Int):User
}
实现类
/**
* @author: hs
* @Date: 2019/10/30 18:05
* @Description:
*/
@Service
class UserServiceImpl @Autowired()(val userMapper: UserMapper) extends UserService {
override def getUserInfo(id: Int): User = {
userMapper.getUserInfo(id)
}
}
controller层
/**
* @author: hs
* @Date: 2019/10/30 15:20
* @Description:
*/
@Controller
@RequestMapping(Array("/api"))
class UserController @Autowired()(userService: UserService) {
@ResponseBody
@GetMapping(Array("/userInfo/{id}"))
def getUserList(@PathVariable("id") id: Int): User = {
val user: User = userService.getUserInfo(id)
println(user.createTime)
println(user.description)
println(user.id)
println(user.userName)
user
}
}
YML文件
server:
port: 1517
spring:
application:
name: spingboot-scala
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/master?useSSL=false&characterEncoding=utf8
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
druid:
initialSize: 5 # 初始化大小,最小,最大
minIdle: 5
maxActive: 20
maxWait: 60000 # 配置获取连接等待超时的时间
timeBetweenEvictionRunsMillis: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
minEvictableIdleTimeMillis: 30000 # 配置一个连接在池中最小生存的时间,单位是毫秒
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true # 打开PSCache,并且指定每个连接上PSCache的大小
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,slf4j # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
stat-view-servlet:
login-username: tianyan
login-password: J&&Hjh7*mM
mybatis:
mapper-locations:
- classpath:mapper/*Mapper.xml
configuration:
map-underscore-to-camel-case: true
版权声明:本文为qq_26869339原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。