I am a Java developer, there are object-oriented basis, while Scala is an object-oriented language, after learning can be started quickly. (And java object oriented similarly) object-oriented learning Scala, the (similar map, reduce, etc., and stream programming Java8) Scala high-level functions, Scala implicit conversion (for enhanced by spring aop in Java can be, Scala implicit conversion is more convenient), Scala pattern matching (similar to Java's switch statement, but the use of a wide access). Here is achieved through the development of spring mvc interface Scala combined spring boot.
Add pom-dependent
First project to build a spring boot, not elaborate here
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--添加Scala依赖-->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<!--添加jpa的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--添加MySQL驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--添加Scala的plugin-->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.1</version>
<executions>
<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>
<configuration>
<recompileMode>incremental</recompileMode>
<scalaVersion>${scala.version}</scalaVersion>/.,bvc;
<args>
<arg>-deprecation</arg>
</args>
<jvmArgs>
<jvmArg>-Xms64m</jvmArg>
<jvmArg>-Xmx1024m</jvmArg>
</jvmArgs>
</configuration>
</plugin>
</plugins>
</build>
Scala plugin where the program is used to compile, test, package of scala
Configuration
server:
port: 7777
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver #数据库驱动
url: jdbc:mysql://localhost:3306/test?useSSL=false #本地数据库url,先在本地数据库中建立test这个库
username: root #数据库用户名
password: 191016 #数据库密码
jpa:
hibernate:
ddl-auto: update #每次运行程序,没有表格会新建表格,表内有数据不会清空,只会更新
database: mysql
Project structure
Wherein the controller layer program entry is a
domain-based physical layer
service business logic layer providing transaction control
data repository layer persistence layer
Entity class
@Entity
@Table
class Person {
@Id
@GeneratedValue
@BeanProperty
var id:Integer = _
@BeanProperty
var name:String = _
@BeanProperty
var sex:String = _
}
No scala get / set methods
repository persistence layer
trait PersonRepository extends CrudRepository[Person,Integer]{
}
trait similar to the meaning of the interface in Java, where inheritance of basic Repository jpa
service layer
@Service
class PersonService @Autowired()(personRepository: PersonRepository) {
/**
* 保存
*
* @param person 保存对象
* @return Person
*/
@Transactional
def save(person: Person): Person = {
personRepository.save(person)
}
/**
* 根据Id查询
*
* @param id 查询参数
* @return Person
*/
def selectPersonById(id: Integer): Person = {
personRepository.findOne(id)
}
}
Automatic injector and the manner herein java not the same, is written on the back of the class name
controller layer
@RestController
@RequestMapping(Array("/v1/person"))
class PersonController @Autowired()(personService: PersonService) {
@PostMapping
def save(@RequestBody person: Person): Person = {
personService.save(person)
}
@GetMapping
def selectPersonById(@RequestParam id: Integer): Person = {
personService.selectPersonById(id)
}
}
Here mapping different paths and java, must pass an array, and a java string is passed
test
Start the project, the postman test
save:
View database saved successfully.
Inquire:
note
Java and Scala can call each other, such as tools written in Java can be used directly in Scala, not a re-write of the Scala tools, and vice versa.
Reproduced in: https: //www.jianshu.com/p/9c7ee8a0ddc9