spring boot和spring mvc的关系
spring boot是spring mvc的升级版
spring boot的特点
1 化繁为简,2下一代框架,3微服务的入门级微框架(spring cloud的基础)
创建spring boot项目
1:idea开发工具
file-new project-
选择需要的相关组件
2:访问https://start.spring.io/创建,下载zip包解压导入
项目的结构
pom.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--spring boot的parent-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--项目的属性,版本号,打包方式等等-->
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<!--Java的版本-->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--作为web项目必须引入的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!--spring单元测试的模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--构建maven的一个插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring boot项目的启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*每个项目只有一个这样的注解*/
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
启动项目访问127.0.0.1:8080
什么都没有写报404错误
新建一个测试类
@RestController
/*@RestController是一个组合注解,放回json字符串*/
public class OneTest {
/*路径为start,请求方式get*/
@RequestMapping(value = "/start",method =RequestMethod.GET)
public String start(){
return "helllwork";
}
@RequestMapping(value = "/begin",method =RequestMethod.GET)
public String begin(){
return "index";
}
}
访问
启动项目的方式
1使用idea工具(个人电脑)
2打包jar包或是war包(适用于服务器)
3使用maven命令来启动(都行)
项目配置
建议将默认application.properties文件改为application.yml
自定义配置
server:
设置端口号
port: 8082
say: 123456
测试类
public class OneTest {
@Value("${say}")
private String say;
/*路径为start,请求方式get*/
@RequestMapping(value = "/start",method =RequestMethod.GET)
public String start(){
return "helllwork"+say;
}
显示结果:
在配置里面使用配置
say: 123456
name: maomao
person: "name: ${name},say: ${say}"
java类
public class OneTest {
@Value("${say}")
private String say;
@Value("${person}")
private String person;
/*路径为start,请求方式get*/
@RequestMapping(value = "/start",method =RequestMethod.GET)
public String start(){
return person;
}
另一种使用方式
person:
say: 123456
name: maomao
kg: 70
创建一个实体类
@Component
@ConfigurationProperties(prefix = "person")
/*读取配置文件引用person*/
public class Person {
private String name;
private String say;
private String kg;
public void setName(String name) {
this.name = name;
}
测试类
public class OneTest {
@Autowired
private Person person;
/*路径为start,请求方式get*/
@RequestMapping(value = "/start",method =RequestMethod.GET)
public String start(){
return person.getKg()+person.getName()+person.getSay();
}
切换配置环境
控制器的使用Controller的使用
@Controller(处请求理http,需要配合模板使用,了解就行,现在都前后端分离)
@RestController(Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controller )
@RequestMapping(配置url映射)
可以为整个类设定一个url,value可以设为一个集合支持多个url,如果method 省约,所有请求都能访问到(不推荐,不安全)
@RequestMapping(value = {"/start","/hi"},method =RequestMethod.GET)
(组合注解@GetMapping,@PostMapping等等)
获取url中的数据
@PathVariable(获取url中的数据)
@RequestParam(获取请求参数的指)
@GetMapping(组合注解)
@PathVariable:
public class OneTest {
@Autowired
private Person person;
/*路径为start,请求方式get*/
@RequestMapping(value = "/start/{id}"/*位置可以随意*/,method = RequestMethod.GET)
public String start(@PathVariable("id") Integer myid){
return myid.toString();
}
返回结果
@RequestParam
/*
(@RequestParam(value = "id",required = false/*不是必须传*/,defaultValue = "81"/*默认值*/)
*/
@RequestMapping(value = "/start",method = RequestMethod.POST)
public String start(@RequestParam("id") Integer myid){
return myid.toString();
}
spring boot数据库操作
Spring-data-Jpa
JPA(java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有hibernate,TopLInk等。
<!--使用数据库,需要添加两个组件,一个数据库的驱动,另一个spring data jpa-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
配饰数据库
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
时区报错加的后缀
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
jpa:
hibernate:
ddl-auto: create
show-sql: true
编写实体类
@Entity
public class Student {
@Id
/*自增*/
@GeneratedValue
private Integer id;
private String name;
private Integer age;
private Integer score;
public Student() {
}
get和set方法。。。。。。
}
运行控制台消息
数据库显示
书写Repository层(dao层)
package com.example.demo.Repository;
import com.example.demo.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRepository extends JpaRepository <Student,Integer> {
/*后面的泛型第一个是操作的类,第二个为主键的类型*/
}
直接在Controller层调用Repository
@RestController
public class StudentController {
@Autowired
private StudentRepository studentRepository;
@RequestMapping(value = "list",method = RequestMethod.GET)
public List<Student> getStudentList() {
return studentRepository.findAll();
}
}
显示结果
自己扩展方法
public interface StudentRepository extends JpaRepository <Student,Integer> {
/*后面的泛型第一个是操作的类,第二个为主键的类型*/
public List<Student> findByAge(Integer age);
}
Controller
@RequestMapping(value = "listOne",method = RequestMethod.GET)
public List<Student> getByAgeList(@RequestParam ("age") Integer age) {
return studentRepository.findByAge(age);
}
结果
事务管理
import org.springframework.transaction.annotation.Transactional;
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
@Transactional
事务管理的注解
public void insertTow() {
Student a=new Student();
a.setId(5);
a.setAge(13);
a.setName("sda");
Student b=new Student();
b.setId(3);
b.setAge(14);
b.setName("hds");
studentRepository.save(a);
studentRepository.save(b);
}