SpringBoot是什么
SpringBoot是一个快速开发框架,是Spring家族中的一员,他可以快速搭建出一套基于Spring的应用,是Spring Cloud的基础,他开启了各种自动搭配,简化了开发,不需要编写各种配置文件,只需要引入相关依赖;
- 特点
1.不需要web.xml
2.不需要springmvc.xm
3.不需要Tomcat,Spring Boot内嵌了Tomcat
4.不需要配置JCON解析,支持REST架构
5.个性化配置非常简单
如何使用
1.创建Maven工程,导入相关依赖
<?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>
<groupId>com.southwind</groupId>
<artifactId>aispringboot-1</artifactId>
<version>1.0-SNAPSHOT</version>
<!--继承父包 -->
<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>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
2. 创建Student实体类
package com.southwind.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Student {
private long id;
private String name;
private int age;
}
在这里的lombok需要安装对应插件,在File -> Setting -> Plugins中搜索lombok安装对应插件,即可使用。没安装的话对应的注解不会生效
3. StudentReository
package com.southwind.repository;
import com.southwind.bean.Student;
import java.util.Collection;
public interface StudentRepository {
public Collection<Student> findAll();
public Student findById(long id);
public void saveOrUpdate(Student student);
public void deleteById(long id);
}
- StudentRepositoryImpl
package com.southwind.repository.impl;
import com.southwind.bean.Student;
import com.southwind.repository.StudentRepository;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestBody;
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;
static {
studentMap=new HashMap<>();
studentMap.put(1L,new Student(1L,"张三",22));
studentMap.put(2L,new Student(2L,"李四",22));
studentMap.put(3L,new Student(3L,"王五",23));
}
@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(student.getId(),student);
}
@Override
public void deleteById(long id) {
studentMap.remove(id);
}
}
- StudentHandler
package com.southwind.controller;
import com.southwind.bean.Student;
import com.southwind.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
@RestController
@RequestMapping("/student")
public class StudentHandler {
@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);
}
@GetMapping("/save")
public void save(@RequestBody Student student) {
studentRepository.saveOrUpdate(student);
}
@GetMapping("/Update")
public void Update(@RequestBody Student student) {
studentRepository.saveOrUpdate(student);
}
@GetMapping("/deleteById")
public void deleteById(@PathVariable("id") long id) {
studentRepository.deleteById(id);
}
}
- 启动类
package com.southwind;
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);
}
}
@SpringBootApplication表示当前类是Spring Boot的入口
特别注意,Application 类的存放位置必须是其他相关业务类的父级,如这样,否则会出现扫描不到的情况
7.application.yml
server:
port:9090
可以设置Tomcat访问端口
通过以上步骤就可以搭建一个简单的应用了