spring boot的概念、原理,如何创建spring boot工程、启动spring boot,使用spring boot(二)

第二篇,结合案例上手学习spring boot

2. Spring Boot的使用
创建Spring Boot工程

1、同样,首先创建 maven 工程。然后导入相关依赖(比如:导入了lombok、web jar包),在导入依赖的时候不要直接写 <dependencies> 要先继承父包
在这里插入图片描述

<!--先继承父包-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.2</version>
</parent>

<dependencies>
	<!--由于要在web环境中启动,所以先配一个web启动jar包-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

2、例子:完成学生数据的增删改查。所以,开始创建实体类(所以上面pom.xml文件中加上了 lombok 依赖)
在这里插入图片描述
3、创建 repository 接口层 和 其实现类

//repository包
public interface StudentRepository {
    public Collection<Student> findAll();
    public Student findById(Long id);
    public void saveOrUpdate(Student student);
    public void deleteById(Long id);
}
//repository包下的Impl包

@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,"李四",23));
        studentMap.put(3L,new Student(3L,"王五",24));
    }
    @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);
    }
}

4、controller层的 handler

@RestController
@RequestMapping("/student")
public class StudentHandler {

    @Autowired
    private StudentRepository studentRepository;
    @GetMapping("/findAll")
    public Collection<Student> find(){
        return studentRepository.findAll();
    }

    @GetMapping("/findById/{id}")
    public Student findBy(@PathVariable("id") long id){
        return studentRepository.findById(id);
    }

    @PostMapping("/save")
    public void saveStudent(@RequestBody Student student){
        studentRepository.saveOrUpdate(student);
    }

    @PutMapping("/update")
    public void update(@RequestBody Student student){
        studentRepository.saveOrUpdate(student);
    }

    @DeleteMapping("/delete")
    public void delete(@PathVariable("id")  long id){
        studentRepository.deleteById(id);
    }

}
启动Spring Boot工程

相关业务代码写完之后,就可以启动了,怎么启动?----> 我们发现上面搭建的是web环境,而web环境启动需要tomcat,之前说过,spring boot内嵌了tomcat(也就是说不需要配置tomcat),即使像之前那样配置tomcat,也根本配置不上,因为这根本就不是一个web工程,只是搭建了web环境,本身是java工程。那怎么启动内嵌的呢?

5、创建启动类 Application(类名可自定义),这个类就作为当前 spring boot的一个入口,一个普通类,怎么当作一个入口?加上 @SpringBootApplication 注解

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
@SpringBootApplication注解

@SpringBootApplication 表示 当前类(启动类)是 Spring Boot 的入口。启动类的存放位置必须是其他相关业务类的存放位置的父级。
在这里插入图片描述
运行启动类,可以发现下面标志,表示成功启动spring boot。
在这里插入图片描述
在这里插入图片描述

更改内嵌tomcat端口号

这个 application.yml 文件也就是 Spring Boot 自己的配置文件
在这里插入图片描述

# 注意:必须体现层级关系,而且yml文件中不能出现中文,这里只是为了说明
server:
  port: 9090

在这里插入图片描述

后续继续讲解 spring boot 整合jsp

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小学鸡!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值