java web scala_Springboot与scala编写第一个web程序

1、pom.xml文件

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.0.5.RELEASE

com.demo

spark

0.0.1-SNAPSHOT

spark

Demo project for Spring Boot

1.8

2.11.8

org.springframework.boot

spring-boot-starter-data-jpa

org.springframework.boot

spring-boot-starter-web

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.scala-lang

scala-library

${scala.version}

net.alchim31.maven

scala-maven-plugin

3.2.2

org.apache.maven.plugins

maven-compiler-plugin

3.5.1

org.scala-tools

maven-scala-plugin

compile

testCompile

incremental

${scala.version}

app

com.web.SparkApplication

-deprecation

-Xms256m

-Xmx2048m

org.springframework.boot

spring-boot-maven-plugin

org.apache.maven.plugins

maven-compiler-plugin

compile

compile

org.apache.maven.plugins

maven-shade-plugin

2.4.3

package

shade

*:*

META-INF/*.SF

META-INF/*.DSA

META-INF/*.RSA

dev

dev

true

test

test

prod

prod

View Code

2、启动文件类,包含两个

a)AppConfig.java

packagecom.webimportorg.springframework.boot.autoconfigure.SpringBootApplicationimportorg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter/***@authoryangwj

* @date 2020/8/7 10:24*/@SpringBootApplication(scanBasePackages= Array("com.web.*"))class AppConfig extendsWebMvcConfigurerAdapter {

}

b)SparkApplication.java

packagecom.webimportorg.springframework.boot.SpringApplication/***@authoryangwj

* @date 2020/8/7 10:25*/object SparkApplicationextendsApp {

SpringApplication.run(classOf[AppConfig])

}

3、domain层,实体对象,Person.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.web.domainimportjavax.persistence.{Entity, GeneratedValue, Id, Table}importscala.beans.BeanProperty/***@authoryangwj

* @date 2020/8/7 10:02*/@Entity

@TableclassPerson {

@Id

@GeneratedValue

@BeanProperty

var id:Integer=_

@BeanProperty

var name:String=_

@BeanProperty

var sex:String=_

}

View Code

4、service和serviceImpl层

a) PersonRepository.java

packagecom.web.serviceimportcom.web.domain.Personimportorg.springframework.data.repository.CrudRepository/***@authoryangwj

* @date 2020/8/7 10:06*/trait PersonRepositoryextendsCrudRepository[Person,Integer]{

}

b) PersonServiceImpl.java

packagecom.web.service.implimportcom.web.domain.Personimportcom.web.service.PersonRepositoryimportorg.springframework.beans.factory.annotation.Autowiredimportorg.springframework.stereotype.Serviceimportorg.springframework.transaction.annotation.Transactional/***@authoryangwj

* @date 2020/8/7 10:07*/@ServiceclassPersonServiceImpl @Autowired()(personRepository: PersonRepository) {/*** 保存

*

*@paramperson 保存对象

*@returnPerson*/@Transactional

def save(person: Person): Person={

personRepository.save(person)

}/*** 根据Id查询

*@paramid 查询参数

*@returnPerson*/def selectPersonById(id: Integer): Person={

personRepository.findById(id).get()

}

}

5、controller层:PersonController.java

packagecom.web.controllerimportcom.web.domain.Personimportcom.web.service.impl.PersonServiceImplimportorg.springframework.beans.factory.annotation.Autowiredimportorg.springframework.web.bind.annotation.{GetMapping, PostMapping, RequestBody, RequestMapping, RequestParam, RestController}/***@authoryangwj

* @date 2020/8/7 10:09*/@RestController

@RequestMapping(Array("/v1/person"))classPersonController @Autowired()(personService: PersonServiceImpl) {

@PostMapping

def save(@RequestBody person: Person): Person={

personService.save(person)

}

@GetMapping

def selectPersonById(@RequestParam id: Integer): Person={

personService.selectPersonById(id)

}

}

6、application.yml和application.properties文件

a) application.yml

server:

port:7777spring:

datasource:

driver-class-name: com.mysql.jdbc.Driver #数据库驱动

url: jdbc:mysql://localhost:3306/spark?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT%2B8 #本地数据库url,先在本地数据库中建立test这个库

username: root #数据库用户名

password: yang156122 #数据库密码

jpa:

hibernate:

ddl-auto: update #每次运行程序,没有表格会新建表格,表内有数据不会清空,只会更新

database: mysql

b) application.properties

###对象变量出现大写,自动转为_

spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

7、创建数据库

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

/*Navicat MySQL Data Transfer

Source Server : 本机

Source Server Version : 50726

Source Host : localhost:3306

Source Database : spark

Target Server Type : MYSQL

Target Server Version : 50726

File Encoding : 65001

Date: 2020-08-07 10:58:25*/SET FOREIGN_KEY_CHECKS=0;-- ----------------------------

-- Table structure forperson-- ----------------------------DROP TABLE IF EXISTS `person`;

CREATE TABLE `person` (

`id`int(11) DEFAULT NULL,

`name` varchar(12) DEFAULT NULL,

`sex` varchar(2) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ----------------------------

--Records of person-- ----------------------------INSERT INTO `person` VALUES ('1', '1', '1');

View Code

如有不懂,可留言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值