特点:
不要web.xml(但是有@requestMapping类似里面的url-pattern)
不要springmvc.xml
不要tomcat,内嵌有tomcat
不需要配置Json解析,支持Rest
里面的repository包里类似以前的dao
里面的handler类似以前的servlet
handler里的注解类似以前的web.xml里的配置
创建一个普通maven工程 结构如下

pom.xml里添加依赖
<!--继承父包-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
// web启动jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
</dependencies>
创建一个pojo类 Student
@Data
public class Student {
private long id;
private String name;
private int age;
}
repository包里一个接口,一个repository实现类
public interface StudentRepository {
public Collection<Student> findAll();
public Student findById(long id);
public void save(Student student);
public void deleteById(long id);
}
/*进行自动注入的注解*/
@Repository
public class repositoryImpl implements StudentRepository {
private static Map<Long,Student> studentMap ;
static {
studentMap = new HashMap<>();
studentMap.put(1l,new Student(0l,"g",33));
studentMap.put(2l,new Student(3l,"gd",43));
studentMap.put(3l,new Student(5l,"fg",23));
}
@Override
public Collection<Student> findAll() {
return studentMap.values();
}
@Override
public Student findById(long id) {
return studentMap.get(id);
}
@Override
public void save(Student student) {
studentMap.put(student.getId(),student);
}
@Override
public void deleteById(long id) {
studentMap.remove(id);
}
}
handler
@RestController
@RequestMapping("/student")
public class HelloHandler {
@Autowired
private repositoryImpl repo;
@GetMapping("/findByAll")
public Collection<Student> findAll(){
return repo.findAll();
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id") long id){
return repo.findById(id);
}
@PostMapping("/save")
public void save(@RequestBody Student student){
repo.save(student);
}
@DeleteMapping("/dele")
public void delete(@PathVariable("id") long id){
repo.deleteById(id);
}
}
入口启动类 Application
@SpringBootApplication
public class Aplication {
public static void main(String[] args) {
SpringApplication.run(Aplication.class,args);
}
}
注意Application类的位置,否则会404报错