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

springboot入门案例,web开发

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

springboot入门案例,web开发

目录

一.SpringBoot框架入门

1.1Spring Boot 简介

1.2Spring Boot 的特性

1.3Spring Boot 四大核心

二. 入门案例

2.1 第一个 SpringBoot 项目

2.1.1 开发步骤

2.2入门案例

​​

​​

2.3 入门案例分析

2.4 Spring Boot的核心配置文件

2.4.1 核心配置格式

2.4.2 多环境配置

2.4.4 SpringBoot自定义配置

2.5 SpringBoot前端使用JSP

2.5.4 在pom.xml文件中配置以下依赖项

2.5.6 在 application.properties文件配置 SpringMVC的视图展示为jsp

 2.5.7 com.tx.springboot.controller.Indexcontroller

三 .SpringBoot框架Web开发

3.1SpringBoot集成Mybatis

3.2 SpringBoot 事务支持

3.3SpringBoot 下的Spring MVC

3.3.1 @Controller

3.3.2@RestController

3.3.3 @RequestMapping(常用)

3.3.4 @GetMapping

3.3.5 @PostMapping

3.3.6 @PutMapping

3.3.7 @DeleteMapping

3.4 Http接口请求工具Postman

3.5 SpringBoot 实现RESTful

3.5.1了解RESTful

3.5.2 SpringBoot开发RESTful

3.5.4请求冲突问题

3.5.5 RESTful原则

3.6 Spring Boot集成Redis

3.6.1 Spring Boot集成Redis

3.7 SpringBoot 集成dubbo

3.7.1 基本集成步骤

3.7.2 SpringBoot 集成SSM+Dubbo+Redis


一.SpringBoot框架入门

1.1Spring Boot 简介         Spring Boot 是 Spring 家族中的一个全新的框架,它用来简化 Spring 应用程序的创建和 开发过程,也可以说 Spring Boot 能简化我们之前采用 SpringMVC + Spring + MyBatis 框架进行 开发的过程。         在以往我们采用 SpringMVC + Spring + MyBatis 框架进行开发的时候,搭建和整合三大框 架,我们需要做很多工作,比如配置 web.xml ,配置 Spring ,配置 MyBatis ,并将它们整合在 一起等,而 Spring Boot 框架对此开发过程进行了革命性的颠覆,完全抛弃了繁琐的 xml 配 置过程,采用大量的默认配置简化我们的开发过程。         所以采用 Spring Boot 可以非常容易和快速地创建基于 Spring 框架的应用程序,它让编 码变简单了,配置变简单了,部署变简单了,监控变简单了。正因为 Spring Boot 它化繁为 简,让开发变得极其简单和快速,所以在业界备受关注。 Spring Boot 在国内的关注趋势图: http://t.cn/ROQLquP

1.2Spring Boot 的特性 ➢ 能够快速创建基于 Spring 的应用程序 ➢ 能够直接使用 java main 方法启动内嵌的 Tomcat 服务器运行 Spring Boot 程序,不需 要部署 war 包文件 ➢ 提供约定的 starter POM 来简化 Maven 配置,让 Maven 的配置变得简单 ➢ 自动化配置,根据项目的 Maven 依赖配置, Spring boot 自动配置 Spring 、 Spring mvc 等 ➢ 提供了程序的健康检查等功能 ➢ 基本可以完全不使用 XML 配置文件,采用注解配置

1.3Spring Boot 四大核心
  1. 自动配置
  2. 起步依赖
  3. Actuator
  4. 命令行界面

二. 入门案例

2.1 第一个 SpringBoot 项目

2.1.1 开发步骤 项目名称:001-springboot-first

1.创建一个 Module,选择类型为 Spring Initializr 快速构建​

2.设置GAV坐标及pom配置信息

3.选择 Spring Boot 版本及依赖(会根据选择的依赖自动添加起步依赖并进行自动配置)​

4.设置模块名称、Content Root 路径及模块文件的目录​

5.项目创建完毕,如    

6.项目结构  ​

7.static :存放静态资源,如图片、 CSS 、 Javascript 等 templates :存放 Web 页面的模板文件 application.properties/application.yml 用于存放程序的各种依赖模块的配置信息,比如 服务 端口,数据库连接配置等

2.2入门案例 项目名称: 002-springboot-springmvc

1.创建一个新的 Module,选择类型为 Spring Initializr

2.指定GAV及pom配置信息

3.选择 Spring Boot 版本及依赖

4.修改 Content Root 路径及文件所在目录

5.对 POM.xml 文件进行解释



    4.0.0
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    
    com.tx.springboot
    springboot-first
    0.0.1-SNAPSHOT
    
    springboot-first
    
    Demo project for Spring Boot
    
    
        1.8
    
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


6.对SpringBoot项目结构进行说明

​ ➢ .mvn|mvnw|mvnw.cmd:使用脚本操作执行 maven 相关命令,国内使用较少,可删 除 ➢ .gitignore:使用版本控制工具 git 的时候,设置一些忽略提交的内容 ➢ static|templates:后面模板技术中存放文件的目录 ➢ application.properties: SpringBoot 的配置文件,很多集成的配置都可以在该文件中 进行配置,例如: Spring 、 springMVC 、 Mybatis 、 Redis 等。目前是空的 ➢ Application.java: SpringBoot 程序执行的入口,执行该程序中的 main 方法, SpringBoot 就启动了

7.创建一个springmvc的spring bootController

package com.tx.springboot.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SpringBootController {

    @RequestMapping("/springBoot/say")
    public @ResponseBody String say(){
        return "hello Spring boot";
    }
}
注意:新创建的类一定要位于 Application 同级目录或者下级目录,否则 SpringBoot 加 载不到

8.在IDEA中右键,运行Application类中的main方法

通过在控制台的输出,可以看到启动 SpringBoot 框架,会启动一个内嵌的 tomcat ,端 口号为 8080 ,上下文根为空

9.在浏览器中输入 http://localhost:8080/springBoot/say 访问

2.3 入门案例分析 ➢ Spring Boot 的父级依赖 spring-boot-starter-parent 配置之后,当前的项目就是 SpringBoot 项目 ➢ spring-boot-starter-parent 是一个 Springboot 的父级依赖,开发 SpringBoot 程序都需要继承该父级项目,它用来提供相关的 Maven 默认依赖,使用它之后,常用的 jar包依赖可以省去version 配置 ➢ Spring Boot 提供了哪些默认 jar 包的依赖,可查看该父级依赖的 pom 文件 ➢ 如果不想使用某个默认的依赖版本,可以通过 pom.xml 文件的属性配置覆盖各个依赖项,比如覆盖 Spring 版本         5.0.0.RELEASE ➢ @SpringBootApplication 注解是 Spring Boot 项目的核心注解,主要作用是开启 Spring 自动配置,如果在 Application 类上去掉该注解,那么不会启动 SpringBoot 程序 ➢ main 方法是一个标准的 Java 程序的 main 方法,主要作用是作为项目启动运行的入 口 ➢ @Controller 及 @ResponseBody 依然是我们之前的 Spring MVC ,因为 Spring Boot 的里面依然是使用我们的 Spring MVC + Spring + MyBatis 等框架

2.4 Spring Boot的核心配置文件

        Spring boot的核心配置文件用于配置Spring Boot程序,名字必须application开始

2.4.1 核心配置格式

(1).properties文件(默认采用该文件)

项目名称: 003-springboot-port-context-path 通过修改 application.properties 配置文件,在修改默认 tomcat 端口号及项目上下文件根键值对的 properties 属性文件配置方式
#设置内嵌Tomcat端口
server.port=9090

#配置项目上下文根
server.servlet.context-path=/003-springboot-port-context-path

(2).yml文件

项目名称: 004-springboot-yml ,在 003 项目基础之上         yml 是一种 yaml 格式的配置文件,主要采用一定的空格、换行等格式排版进行配置。         yaml 是一种直观的能够被计算机识别的的数据序列化格式,容易被人类阅读, yaml 类似于 xml ,但是语法比 xml 简洁很多,值与前面的冒号配置项必须要有一个空格, yml 后缀也可以使用 yaml 后缀
server:
    port: 9090
    servlet:
        context-path: /004-springboot-yml
注意:当两种格式配置文件同时存在,使用的 是.properties 配置文件

2.4.2 多环境配置         在实际开发的过程中,我们的项目会经历很多的阶段(开发-> 测试 -> 上线),每个阶段的配置也会不同,例如:端口、上下文根、数据库等,那么这个时候为了方便在不同的环境之间切换,SpringBoot 提供了多环境配置,具体步骤如下

(1) 项目名称:005-springboot-multi-environment

        为每个环境创建一个配置文件,命名必须application-环境表示.propertiest|yml​

application-dev.properties

#开发环境
#设置内嵌tomcat默认端口号
server.port=8080
#设置项目的上下文根
server.servlet.context-path=/dev
#使用yml文件形式
server:
  port: 8080
  servlet:
    context-path: /dev

application-product.properties

#生产环境

#配置内嵌的tomcat默认端口号
server.port=80

#配置项目山下文根
server.servlet.context-path=/product

application-test.properties

#测试环境

#配置内嵌tomcat端口号
server.port=8081
#配置项目的上下文根
server.servlet.context-path=/test

在总配置文件application.properties进行环境的激活

#springboot主核心配置文件
#激活使用的配置文件
spring.profiles.active=product(dev,test)

yml多文档方式 — 分隔不同配置

---
server: 
  port: 8081
spring: 
  profiles: dev
---
server:
  port: 8082
spring:
  profiles: test
---
server:
  prot: 8083
spring:
  profiles: pro
---
spring:
  profiles:
      active: pro

2.4.4 SpringBoot自定义配置

案例演示:

        在 SpringBoot 的核心配置文件中,除了使用内置的配置项之外,我们还可以在自定义配置,然后采用如下注解去读取配置的属性值.

(1)@Value注解

在核心配置文件application.properties中,添加两个自定义配置项school.name 和website.

#设置内嵌tomcat端口号
server.port=8080
#设置上下文根
server.servlet.context-path=/

school.name=tx
websit=http://www.baidu.com

application.yml格式配置 

server:
    port: 9090
    servlet:
        context-path: /

school:
    name: ssm
websit: http://www.baidu.com

 在SpringbootController中的定义属性,并且使用@Value注解或者自定义配置值。

@Controller
public class IndexController {

    @Value("${school.name}")
    private String schoolName;

    @Value("${websit}")
    private String websit;

    @RequestMapping(value = "/say")
    public @ResponseBody String say() {
        return "Hello:" + schoolName + ":" + websit;
    }
}

(2)@ConfigurationProperties

案例演示:                  在con.tx.springboot.config包下创建ConfigInfo类,并为类加上@Componet和@ConfigurationProperties注解,加上属性prefix,作用可以区分同名配置
@Component//将此类将给spring容器进行管理
@ConfigurationProperties(prefix = "school")
public class School {

    private String name;

    private String websit;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getWebsit() {
        return websit;
    }
    public void setWebsit(String websit) {
        this.websit = websit;
    }
}

application.properties配置文件

server.port=8080
server.servlet.context-path=/

school.name=ssm
school.websit=http://www.baidu.com

application.yml配置文件

server:
    port: 9090
    servlet:
        context-path: /

school:
    name: ssm
    websit: http://www.baidu.com

在IndexController中注入School配置类 

@Controller
public class IndexController {

    @Autowired
    private School school;

    @Autowired
    private Abc abc;

    @RequestMapping(value = "/say")
    public @ResponseBody String say() {

        return "school.name="+school.getName()+"-----school.websit="+school.getWebsit() + "====abc.name"+abc.getName()+"===abc.websit="+abc.getWebsit();
    }
}

(3) 在school类中使用了ConfigurationProperties注解后,IDEA会出现一个警告不影响程序的执行

 
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        

(4)中文乱码

如果在SpringBoot核心配置文件中有中文信息,会出现乱码

        一般在配置文件中,不建议出现中文(注释除外)         如果有,可以先转化为 ASCII 码

2.5 SpringBoot前端使用JSP

2.5.4 在pom.xml文件中配置以下依赖项
  
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
        
    

    
        
        
            
                
                src/main/webapp
                
                meta-INF/resources
                
                
                    *.*
                
            
        
        
            
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

2.5.6 在 application.properties文件配置 SpringMVC的视图展示为jsp
#配置视图解析器
#其中:/ 表示目录为src/main/webapp
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

yml文件形式

#配置SpringMVC视图解析器
#其中:/ 表示目录为src/main/webapp
spring:
    mvc:
        view:
            prefix: /
            suffix: .jsp

 2.5.7 com.tx.springboot.controller.Indexcontroller
@Controller
public class IndexController {

    @RequestMapping(value = "/say")
    public ModelAndView say() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message","Hello,SpringBoot");
        mv.setViewName("say");
        return mv;
    }
}

三 .SpringBoot框架Web开发

3.1SpringBoot集成Mybatis

实现步骤:

1.准备数据库

2.创建项目 springboot-web-mybatis

3.在pom.xml中添加相关jar依赖

4.配置application.properties数据源

5.代码实现

5.运行测试

实现:

数据库​

创建项目如同前例

pom.xml中添加依赖jar

 
        1.8
        
        
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            mysql
            mysql-connector-java
            
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.0
        

    

    

        
        
            
                src/main/java
                
                    ***.xml
        
    

(3)配置Mybatis逆向工程

(4)服务提供方核心配置

#配置内嵌Tomcat端口号
server.port=8081
#设置上下文根
server.servlet.context-path=/

#设置连接数据库信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.154.128:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=333

#设置dubbo配置
spring.application.name=024-springboot-dubbo-ssm-provider
#声明当前工程为服务提供者
spring.dubbo.server=true
#设置注册中心
spring.dubbo.registry=zookeeper://192.168.154.128:2181

#设置redis配置
spring.redis.host=192.168.154.128
spring.redis.port=6379
spring.redis.password=123456

(5)配置服务提供方启动类

@SpringBootApplication
@MapperScan(basePackages = "com.tx.springboot.mapper")
@EnableDubboConfiguration   //开启dubbo配置
public class Application {

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

(6)业务接口实现类

3.创建dubbo服务消费者项目

(1)创建项目,Springboot

(2)添加依赖

  
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            com.alibaba.spring.boot
            dubbo-spring-boot-starter
            2.0.0
        

        
        
            com.101tec
            zkclient
            0.10
        

        
        
            com.bjpowernode.springboot
            023-springboot-dubbo-ssm-interface
            1.0.0
        

        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
        

(3)核心配置

#设置内嵌Tomcat端口号
server.port=8080
server.servlet.context-path=/

#设置dubbo配置
spring.application.name=025-springboot-dubbo-ssm-consumer
spring.dubbo.registry=zookeeper://192.168.154.128:2181

#配置视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

(4)启动类配置

@SpringBootApplication
@EnableDubboConfiguration   //开启dubbo配置
public class Application {

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

(5)控制层

@Controller
public class StudentController {

    @Reference(interfaceName = "com.tx.springboot.service.StudentService",version = "1.0.0",check = false)
    private StudentService studentService;

    @RequestMapping(value = "/student/detail/{id}")
    public String studentDetail(Model model,
                                @PathVariable("id") Integer id) {

        Student student = studentService.queryStudentById(id);
        model.addAttribute("student",student);
        return "studentDetail";
    }
}

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

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

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