Spring Booot
Spring Boot是一个快速开发框架。可以迅速搭建出一套基于spring框架体系的应用,是SpringCloud的基础
SpringBoot开启了各种自动装配,从而简化代码的开发,不需要编写各种配置文件,只需要引入相关依赖就可以迅速搭建一个应用。
特点:
1、不需要web.xml
2、不需要springmvc.xml
3、不需要tomcat,Spring Boot内嵌了tomcat
4、不需要配置JSON解析,支持REST架构
5、个性化配置非常简单
如何使用?
1、创建maven工程,导入相关依赖
<!--继承父包-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
</parent>
<dependencies>
<!--web启动jar-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
2、创建Student 实体类
package com.lei.entity;
import lombok.Data;
@Data
public class Student {
private long id;
private String name;
private int age;
}
3、3.1StudentRepository(接口)
package com.lei.repository;
import com.lei.entity.Student;
import java.util.Collection;
import java.util.List;
public interface StudentRepository {
public Collection<Student> findAll();
public Student findById(long id);
public void saveOrUpdate(Student student);
public void deleteById(long id);
}
3.2 StudentRepositoryImpl(实现类)
package com.lei.repository.impl;
import com.lei.entity.Student;
import com.lei.repository.StudentRepository;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Repository
public class StudentRepositoryImpl implements StudentRepository {
//替代数据库
private static Map<Long,Student> studentMap;
Student student=new Student(1,"lilei");
static {
studentMap=new HashMap<>();
studentMap.put(1L,new Student(1,"lilei"));
studentMap.put(2L,new Student(2,"WANGlei"));
}
@Override
public Collection<Student> findAll() {
return studentMap.values();
}
@Override
public Student findById(long id) {
return studentMap.get(id);
}
@Override
public void saveOrUpdate(Student student) {
studentMap.put(4L,student);
}
@Override
public void deleteById(long id) {
studentMap.remove(id);
}
}
4、StudentController
package com.lei.contoller;
import com.lei.entity.Student;
import com.lei.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/findAll")
public Collection<Student> findAll(){
return studentRepository.findAll();
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id") long id){
return studentRepository.findById(id);
}
@PostMapping("/save")
public void save(@RequestBody Student student){
studentRepository.saveOrUpdate(student);
}
@PostMapping("/update")
public void update(@RequestBody Student student){
studentRepository.saveOrUpdate(student);
}
@DeleteMapping ("/delete/{id}")
public void deleteById(@PathVariable("id") long id){
studentRepository.deleteById(id);
}
}
5、Application
@SpringBootApplication//表示是SpringBoot的入口,Application的位置必须是其他业务层的父级
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
6、SpringBoot如何修改端口号??
resources中建立application.yml,不需要头文件
server:
port:9090