栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【编程不良人】快速入门SpringBoot学习笔记01---SSM环境搭建回顾、SSM开发中存在的问题、SpringBoot引言、优势、约定、环境搭建、注解说明、配置文件拆分、项目创建的两种方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【编程不良人】快速入门SpringBoot学习笔记01---SSM环境搭建回顾、SSM开发中存在的问题、SpringBoot引言、优势、约定、环境搭建、注解说明、配置文件拆分、项目创建的两种方式

1. SSM环境搭建回顾

配套视频:【编程不良人】2021年SpringBoot最新最全教程_哔哩哔哩_bilibili

【编程不良人】2021年SpringBoot最新最全教程_哔哩哔哩_bilibili

  • spring

  • springmvc

  • mybatis

  • spring springmvc mybatis 简单功能 员工添加 查询 所有

  # 项目
  -  需求分析  概要设计(库表设计)   详细设计(验证库表正确性)  编码(环境搭建+业务代码)  测试  部署上线
  ​
  # 员工添加 查询所有功能  SSM
  - 库表   库: ssm   数据库:mysql  表: id name birthday salary
  drop database if exists ssm;
  create database if not exists ssm;
  use ssm;
  drop table if exists emp;
  create table if not exists emp(
      id int(11) not null primary key auto_increment,
      name varchar(40),
      birthday timestamp,
      salary double(10,2)
  )engine=innodb default charset=utf8mb4;
  ​
  # 编码 环境搭建新建Maven-webapp项目 ssm:spring  springmvc   一个团队开发 无缝整合
  - springmvc  spring  mybatis
  ​
1.1 pom.xml引入依赖
  
      junit
      junit
      4.11
      test
  
  
      org.springframework
      spring-core
      5.3.6
  
      org.springframework
      spring-beans
      5.3.6
  
      org.springframework
      spring-aop
      5.3.6
  
      org.springframework
      spring-context
      5.3.6
  
      org.springframework
      spring-context-support
      5.3.6
  
      org.springframework
      spring-jdbc
      5.3.6
  
      org.springframework
      spring-web
      5.3.6
  
  
      org.springframework
      spring-webmvc
      5.3.6
  
  
      com.alibaba
      druid
      1.2.4
  
  
      mysql
      mysql-connector-java
      5.1.38
  
  
      org.mybatis
      mybatis
      3.5.6
  
  
      org.mybatis
      mybatis-spring
      2.0.6
  
  
      com.fasterxml.jackson.core
      jackson-databind
      2.9.3
  
  
      org.aspectj
      aspectjweaver
      1.9.5
  
  
      org.aspectj
      aspectjrt
      1.9.5
  

注意:spring相关依赖要保证版本号一致

1.2 编写spring.xml配置文件
  
  
  ​
      
      
  ​
      
      
          
          
          
          
      
  ​
      
      
          
          
              .   ____          _            __ _ _
   /\ / ___'_ __ _ _(_)_ __  __ _    
  ( ( )___ | '_ | '_| | '_ / _` |    
   \/  ___)| |_)| | | | | || (_| |  ) ) ) )
    '  |____| .__|_| |_|_| |___, | / / / /
   =========|_|==============|___/=/_/_/_/
   :: Spring Boot ::                (v2.5.0)
  ​
  2022-04-28 11:29:21.768  INFO 10908 --- [           main] com.study.SpringBoot01Application        : Starting SpringBoot01Application using Java 1.8.0_131 on cjn-PC with PID 10908 (D:Software_DevelopmentIDEA_codeSpringBootspringboot01targetclasses started by cjn in D:Software_DevelopmentIDEA_codeSpringBootspringboot01)
  2022-04-28 11:29:21.775  INFO 10908 --- [           main] com.study.SpringBoot01Application        : No active profile set, falling back to default profiles: default
  2022-04-28 11:29:22.628  INFO 10908 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8888 (http)
  2022-04-28 11:29:22.642  INFO 10908 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
  2022-04-28 11:29:22.642  INFO 10908 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.46]
  2022-04-28 11:29:22.716  INFO 10908 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
  2022-04-28 11:29:22.716  INFO 10908 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 895 ms
  2022-04-28 11:29:23.044  INFO 10908 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8888 (http) with context path ''
  2022-04-28 11:29:23.053  INFO 10908 --- [           main] com.study.SpringBoot01Application        : Started SpringBoot01Application in 1.805 seconds (JVM running for 3.3)
  2022-04-28 11:29:23.054  INFO 10908 --- [           main] o.s.b.a.ApplicationAvailabilityBean      : Application availability state LivenessState changed to CORRECT
  2022-04-28 11:29:23.056  INFO 10908 --- [           main] o.s.b.a.ApplicationAvailabilityBean      : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
  ​
  //说明:  出现以上日志说明启动成功

注意:到这里项目环境已经搭建成功了,看看仅仅需要5分钟

5.5 创建控制器并测试
  //在项目中创建指定的包结构
   
                      @Controller
  package com.study.controller;
  ​
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;
  ​
  
  @RestController
  @RequestMapping("hello")
  public class HelloController {
      
      @RequestMapping("test")
      public String test(){
          System.out.println("hello springboot!");
          return "Hello SpringBoot!";
      }
  }                                   

启动项目进行测试控制器:

  # 注意: springboot的项目启动默认无项目名,此处指定了项目名
  - 访问路径: http://localhost:8888/springboot01/hello/test
  ​
  # 注意: springboot的项目启动默认无项目名
  - 访问路径: http://localhost:8888/hello/test

测试结果:

(1)配置项目名

 (2)不配置项目名

 与此同时,控制台输出:hello springboot!

6. 相关注解说明

配套视频:【编程不良人】2021年SpringBoot最新最全教程_哔哩哔哩_bilibili

  # springboot =  springmvc(控制器controller) +  spring(工厂)
  ​
  # 入口类 SpringApplication
  - SpringBootApplication: 全局入口类 有且只能有一个
  ​
  # @SpringBootApplication:  注解
    组合注解: 就是由多个注解组合而成一个注解
    元注解 : 用来修饰注解的注解,如:@Target、@Retention、@Documented、@Inherited
            @Target: 指定注解作用范围
            @Retention: 指定注解什么时候有效
  包含下面三个注解:​
  # @SpringBootConfiguration:
      这个注解就是用来自动配置spring、springmvc(初始化servlet ...)相关环境
   
  # @EnableAutoConfiguration: 开启自动配置
      自动配置核心注解  自动配置spring相关环境  自动与项目中引入的第三方技术自动配置其环境
      mybatis-springboot、redis-springboot 、es-springboot 、rabbitmq 第三方技术
   
  # @ComponentScan: 组件扫描
      根据注解发挥注解作用,默认扫描当前包及其子包
   
  # 注意启动springboot应用时候需要传递main函数参数作为启动的第二个参数,它的主要作用是:
      测试用,项目启动后动态传参,在启动时指定jvm参数覆盖默认配置
  ​

7. 配置文件

配套视频:【编程不良人】2021年SpringBoot最新最全教程_哔哩哔哩_bilibili

7.1 配置文件的拆分

       说明:在实际开发过程中生产环境和测试环境有可能是不一样的,因此将生产中的配置和测试中的配置拆分开是非常必要的,在springboot中也提供了配置文件拆分的方式。

 这里以生产中项名名称不一致为例:

  • 生产中项目名为: springboot_prod

  • 测试中项目名为: springboot_dev

  • 端口同时为: 8888(也可设置不同)

  # 主配置文件 application.yml:用来书写相同的的配置
  # 公共配置
  server:
    port: 8888 # 修改内嵌服务器端口号
  #  servlet:
  #    context-path: /springboot01 # 修改项目名 注意项目名必须以“/”开头
  ​
  # 激活环境配置,指定哪个环境配置生效(此处写配置文件名的简写)
  spring:
    profiles:
  #    active: prod # 让prod生效
      active: dev # 让dev生效
      
      
  # 生产配置文件 application-pord.yml,其中prod为生产配置文件简名
  server:
    servlet:
      context-path: /springboot_prod   

      
  # 测试配置文件 application-dev.yml,其中dev为测试配置文件简名
  server:
    servlet:
      context-path: /springboot_dev

 启动项目后测试:

生产访问路径:http://localhost:8888/springboot_prod/hello/test

测试访问路径:http://localhost:8888/springboot_dev/hello/test

控制台同时输出hello springboot!

7.2 启动指定配置文件

       说明:往往在企业级开发过程中为SpringBoot应用启动时指定一个完整外部配置也是经常用到的,在SpringBoot中也提供这个方式来启动项目,如下所示:

 application_local_prod.yml配置文件代码:

  
  # 公共配置
  server:
    port: 9999 # 修改内嵌服务器端口号
    servlet:
      context-path: /springboot_local_prod
  ​

需要额外进行配置:

Program arguments:需要指定绝对路径

例如:

--spring.config.location=D:Software_DevelopmentIDEA_codeSpringBootspringboot01application-local-prod.yml

复制application_local_prod.yml路径方法:选中文件---右键---Copy---Absolute Path

外部部署时打包成jar包:java -jar --spring.config.location=绝对路径  xxx.jar

启动项目后测试:

访问路径:http://localhost:9999/springboot_local_prod/hello/test

控制台同时输出hello springboot!

8. 创建项目的两种方式

配套视频:【编程不良人】2021年SpringBoot最新最全教程_哔哩哔哩_bilibili

8.1 方式一:基于maven

       File---New---Module---Maven---可以不选任何骨架 / 可以勾选maven-archetype-quickstart / 可以勾选maven-archetype-webapp,完成项目创建后需要额外添加test目录下的resources目录。

8.2 方式二:基于Spring Initializr快速初始化(推荐)

File---New---Module---Spring Initializr---选择SDK和starter service URL

设置Group、Artifict、Type、Packaging、Java Version等参数

 选择依赖

 创建后的项目结构如下:

通过Spring Initializr创建的项目结构完整,生成的其他文件说明如下:

1、创建项目时,建议各字段间以-分割,例如:spring-boot-day02

2、.mvn属于隐藏文件,主要用于配合mvnw(Linux)、mvnw.cmd(Windows)进行项目启动

3、.gitIgnore属于Git忽略文件配置文件,用于Git使用

4、Spring Initializr创建项目完成后默认配置文件为application.properties,建议修改为.yml后缀,即:application.yml,相比于properties,yml更加主流

pom.xml内容说明:

  
  
      org.springframework.boot
      spring-boot-starter-parent
      2.6.7
       
  
    
    
        org.springframework.boot
        spring-boot-starter-web
    
  ​
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    
  
  
      
          
              org.springframework.boot
              spring-boot-maven-plugin
          
      
  

8.3 添加配置
  
  server:
    port: 8888 # 修改内置服务器访问端口
    servlet:
      context-path: /spring-boot-02

8.4 新建Controller测试服务器
  
  package com.study.controller;
  ​
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;
  ​
  
  @RestController
  @RequestMapping("hello")
  public class HelloController {
      //测试路径:http://localhost:8888/spring-boot-02/hello/test
      @RequestMapping("test")
      public String test(){
          System.out.println("Hello SpringBoot!");
          return "Hi SpringBoot!";
      }
  }
  ​

测试结果:

 控制台输出:Hello SpringBoot!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/842281.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号