SpringBoot入门

Spring Boot简化了Java EE项目的繁琐配置,提供了快速构建应用的方式。本文将介绍Spring Boot的概述、核心特点,演示如何通过Spring官网和IDEA创建项目,并简述其版本介绍、配置文件及Web项目的三层结构。
摘要由CSDN通过智能技术生成

SpringBoot概述

随着技术的发展,Java EE使用 Spring 逐渐变得笨重起来,大量的 XML 文件存在于项目之中。繁琐的配置,整合第三方框架的配置问题,导致了开发和部署效率的降低

Spring Boot 是伴随着 Spring 4.0 诞生的,从字面理解,Boot是引导的意思,因此 Spring Boot 旨在帮助开发者快速搭建 Spring 框架。Spring Boot 继承了原有 Spring 框架的优秀基因,使 Spring 在使用中更加方便快捷。

Spring Boot与Spring

SpringBoot简介

 

 Spring 、Spring MVC和Spring Boot、

 Spring Boot核心特点

 新建Spring Boot项目演示

Spring官网 start.spring.io

  • 访问 http://start.spring.io/。
  • 在页面上输入相应的 Spring Boot 版本、Group 和 Artifact 信息以及项目依赖,然后创建项目。
  • 创建Spring Boot工程

 

IDEA集成的Spring Initializr


IDEA 中可以通过File->New->Project来快速构建 Spring Boot 工程。如下,选择 Spring Initializr,在 Project SDK 中选择刚刚我们导入的 jdk,点击 Next,到了项目的配置信息。

Group:填企业域名,
Artifact:填项目名称,
Dependencies:可以添加我们项目中所需要的依赖信息,根据实际情况来添加,本课程只需要选择 Web 即可。
 

Spring Boot版本介绍

 

GA:General Available:通用可使用,官方推荐使用此版本

PRE:预览版,内测版

SNAPSHOT

RC:Release

稳定性(由小到大):

PRE<SNAPSHOT<RC<GA

配置文件简介

yml:分层级,冒号后需要空格

server:
#更改端口号
  port: 8081
  servlet:
    #公共前缀
    context-path: /first

#配置自定义属性
school:
  grade: 3
  classnum: 6

# 配置数据库地址
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/springbootlearn?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true

配置自定义属性案例

配置类

package com.imooc.springbootleam;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * School配置类
 */
// @Component (把普通pojo实例化到spring容器中,相当于配置文件中的 )
@Component
// @ConfigurationProperties为Springboot中的一个注解,用于绑定实体类与配置文件,用来把properties或者yml配置文件转化为bean来使用的。
@ConfigurationProperties(prefix = "school")
public class SchoolConfig {

    Integer grade;
    Integer classnum;

    public Integer getGrade() {
        return grade;
    }

    public void setGrade(Integer grade) {
        this.grade = grade;
    }

    public Integer getClassnum() {
        return classnum;
    }

    public void setClassnum(Integer classnum) {
        this.classnum = classnum;
    }

}

读取配置类

package com.imooc.springbootleam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 读取配置类
 */
@RestController
public class ConfigController {

    @Autowired
    SchoolConfig schoolConfig;

    @GetMapping("/gradefromconfig")
    public String gradeClass(){
        return schoolConfig.grade+" : "+schoolConfig.classnum;
    }

}

Web项目的三层结构

Domain层

package com.imooc.springbootleam;

public class Student {
    Integer id;
    String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

Mapper层

package com.imooc.springbootleam;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

/**
 * mapper接口,操作db
 */
@Mapper
@Repository
public interface StudentMapper {

    @Select("select * from student where id = #{id}")
    Student findByID(Integer id);

}

Service层

package com.imooc.springbootleam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * service层
 */
@Service
public class StudentService {
    @Autowired
    StudentMapper studentMapper;

    public Student findStudent(Integer id){
        return  studentMapper.findByID(id);
    }
}

Controller层

package com.imooc.springbootleam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 学生类controller层
 */
@RestController
public class StudentController {

    @Autowired
    StudentService studentService;

    @GetMapping("/student")
    public String student(@RequestParam Integer num){
       Student student = studentService.findStudent(num);
       return student.toString();
    }

}

Spring Boot是一个用于快速构建基于Spring框架的应用程序的开源框架。它提供了一种简化的方法来配置和部署应用程序,使开发人员能够更快地开发和运行应用程序。 Spring Boot Actuator是Spring Boot的一个组件,它可以帮助我们监控和管理Spring Boot应用程序,包括健康检查、审计、统计和HTTP追踪等功能。要使用Spring Boot Actuator,只需引入相应的起步依赖,并在应用程序的入口点类上添加@SpringBootApplication注解即可。在该类中,使用@SpringBootApplication注解相当于同时添加了@Configuration、@EnableAutoConfiguration和@ComponentScan注解,它标识了当前应用程序是一个Spring Boot应用程序。要启动Spring Boot应用程序,只需在主启动类中编写main函数,通过调用SpringApplication.run(Application.class, args)方法来启动应用程序。在开发过程中,<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [SpringBoot入门](https://blog.csdn.net/weixin_45905210/article/details/121712027)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [spring boot 入门](https://blog.csdn.net/zhshx19900318/article/details/129476812)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值