Spring学习——SpringBoot

SpringBoot简介

  • SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程

Spring程序缺点

  • 配置繁琐
  • 依赖设置繁琐

SpringBoot程序优点

  • 自动配置
  • 起步依赖(简化依赖配置)
  • 辅助功能(内置服务器,…)

原生开发SpringMVC程序过程

<dependencies>
    <dependency>
    	<groupId>javax.servlet</groupId>
    	<artifactId>javax.servlet-api</artifactId>					
    	<version>3.1.0</version>
    	<scope>provided</scope>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.10.RELEASE</version>
    </dependency>
</dependencies>
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class<?>[] getRootConfigclasses() {
    	return new Class[]{SpringConfig.class};
    }
    protected class<?>[] getServletConfigClasses() {
    	return new Class[]{SpringMvcConfig.class};
    }
    protected String[] getServletMappings() {
    	return new String[]{"/"};
    }
}
@Configuration
@ComponentScan("com.jihua.controller")
@EnablewebMvc
public class SpringMvcConfig {
}
@RestController
@RequestMapping( "/books" )
public class BookController {
    @Autowired
    private BookService bookService;
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Book book = bookService.getById(id);
        Integer code = book != null ? code.GET_OK : Code.GET_ERR;
        String msg = book != null ?"":"数据查询失败,请重试! ";
        return new Result(code, book, msg);
    }
}

SpringBoot入门案列

  1. 创建新模块,选择Spring初始化,并配置模块相关基础信息

1

  1. 选择当前模块需要使用的技术集

2

  1. 开发控制器类

    @RestController
    @RequestMapping("/book")
    public class BookController {
        @GetMapping("/{id}")
        public String getBookById(@PathVariable String id) {
            System.out.println("id==>" + id);
            return "book id:" + id;
        }
    }
    
  2. 运行自动生成的Application类

3

  • 测试功能正常

4

入门案例相关

最简SpringBoot程序所包含的基础文件

  • pom.xml文件

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.7.9</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.jihua</groupId>
        <artifactId>SpringDemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
    
  • Application类

    package com.jihua;
    
    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);
        }
    }
    

Spring程序与SpringBoot程序对比

类/配置文件SpringSpringBoot
pom文件中的坐标手工添加勾选添加
web3.0配置类手工制作
Spring/SpringMVC配置类手工制作
控制器手工制作手工制作

不使用IDEA如何创建SpringBoot项目?

  1. 进入SpringBoot官网

  2. 网站底部点击Spring Initializr

5

  1. 配置SpringBoot项目

6

  1. 点击GENERATE后开始下载项目

7

SpringBoot项目脱离IDEA运行

  1. 使用maven生命周期的package打包

    • IDEA中默认生成路径为当前项目的target目录下
  2. 使用java命令启动项目

    java -jar jar包文件名
    

注意:使用maven打包需要在pom文件中添加如下坐标:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
  • 使用该插件会同时打包相关依赖的jar包,并指定jar包的入口程序
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值