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

Springboot总结

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

Springboot总结

Springboot总结

1.springboot概念2.springboot开发流程

2.1 创建maven项目2.2 引入依赖

2.2.1 配置parent(起步依赖)2.2.2 配置业务框架2.2.3 配置版本号2.2.4 打包2.2.5 补充 2.3 项目基本结构2.4 Springboot的入口类

2.4.1@SpringBootApplication

2.4.1.1 @ComponentScan2.4.1.2 @EnableAutoConfiguration 2.5 配置banner2.6 springboot配置

2.6.1 调度2.6.2 运行环境2.6.3 测试环境2.6.4 上线环境 2.7 实体类2.8 mapper接口2.9 service类2.10 单元测试2.11 Controller类2.12 springboot处理时间问题

2.12.1 后台传输有时间2.12.2 前端传输有时间 2.13 接口测试工具Swagger3

2.13.1 引入Swagger框架2.13.2 配置swagger3

1.springboot概念

The primary goals of Spring Boot are:
To provide a radically faster and widely accessible ‘getting started’ experience for all Spring development.
To be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults.
To provide a range of non-functional features that are common to large classes of projects (e.g. embedded servers, security, metrics, health checks, externalized configuration).
Spring Boot does not generate code and there is absolutely no requirement for XML configuration.

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

2.springboot开发流程 2.1 创建maven项目

配置maven环境

2.2 引入依赖 2.2.1 配置parent(起步依赖)

        org.springframework.boot
        spring-boot-starter-parent
        2.5.10
   
2.2.2 配置业务框架

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

        
            mysql
            mysql-connector-java
            ${project.mysql.version}
        

        
            com.alibaba
            druid-spring-boot-starter
            ${project.druid.version}
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            ${project.mybatis.version}
        

        
            org.projectlombok
            lombok
            ${project.lombok.version}
        

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

        
            junit
            junit
            ${project.juint.version}
            test
        

        
            io.springfox
            springfox-boot-starter
            ${project.swagger.version}
        
    
2.2.3 配置版本号
 
        8
        8
        8.0.28
        1.2.8
        2.2.2
        1.18.22
        4.12
        3.0.0
    
2.2.4 打包
   
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
   




若项目已经打包完毕,则可以部署项目

2.2.5 补充

倘若我们准备更换启动服务器

       
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                
            
        
        
            org.springframework.boot
            spring-boot-starter-undertow
        
2.3 项目基本结构

2.4 Springboot的入口类

运行main方法的类
自动化配置

package com.dyit.springboot;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication sa = new SpringApplication(Main.class);

        //sa.setBannerMode(Banner.Mode.OFF);
        sa.run(args);
    }
}
2.4.1@SpringBootApplication

@SpringBootApplication = @ComponentScan+@EnableAutoConfiguration+…

2.4.1.1 @ComponentScan

扫描容器(spring容器mvc+spring)中类
@Controller/@RestController
@Service
@Repository(用在持久层的接口上,这个注解是将接口的一个实现类交给spring管理)
@Component

2.4.1.2 @EnableAutoConfiguration

自动配置

2.5 配置banner
${AnsiColor.BRIGHT_BLUE}

//                          _ooOoo_                               //
//                         o8888888o                              //
//                         88" . "88                              //
//                         (| ^_^ |)                              //
//                         O  =  /O                              //
//                      ____/`---'____                           //
//                    .'  \|     |//  `.                         //
//                   /  \|||  :  |||//                          //
//                  /  _||||| -:- |||||-                         //
//                  |   | \  -  /// |   |                       //
//                  | _|  ''---/''  |   |                       //
//                    .-__  `-`  ___/-. /                       //
//                ___`. .'  /--.--  `. . ___                     //
//              ."" '<  `.____<|>_/___.'  >'"".                  //
//            | | :  `- `.;` _ /`;.`/ - ` : | |                 //
//               `-.   _ __ /__ _/   .-` /  /                 //
//      ========`-.____`-.________/___.-`____.-'========         //
//                           `=---='                              //
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //
//            ${AnsiColor.BRIGHT_RED}佛祖显灵       无病无灾     财源滚滚  大吉大利           ${AnsiColor.BRIGHT_BLUE} //

启动失败问题:APPLICATION FAILED TO START
Description:

Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

原因:没有配置DataSource
自动配置
解决方案:1.配置yml 2.暂时不配置DataSource

@SpringBootApplication(exclude = {
     DruidDataSourceAutoConfigure.class,
     DataSourceAutoConfiguration.class
})
2.6 springboot配置

2.6.1 调度
#  进行调度
spring:
  profiles:
    active: dev
2.6.2 运行环境
# 端口号配置,部署名称
server:
  port: 8099
  servlet:
    context-path: /ssm

#配置数据源
spring:
  datasource:
    druid:
      url: jdbc:mysql://localhost:3306/mybatis_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
      username: root
      password: 123456
      driver-class-name: com.mysql.cj.jdbc.Driver
#配置mybatis
mybatis:
  type-aliases-package: com.dyit.springboot.entity
  mapper-locations: classpath:mapper
@Configuration
public class SsmConfiguration implements WebMvcConfigurer {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new Converter() {
            @Override
            @SneakyThrows
            public Date convert(String source) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date d = sdf.parse(source);
                return d;
            }
        });
    }
}



    
    日期页面


    


常见的错误

服务器返回的状态码
200—OK
400—转换问题(时间)String—>Date
404—URL错误(资源位置错误)
405—前端请求方法(GET)后台(POST)
500—后台逻辑错误(空指针错误)

2.13 接口测试工具Swagger3

2.13.1 引入Swagger框架
        
            io.springfox
            springfox-boot-starter
            ${project.swagger.version}
        
2.13.2 配置swagger3
@Configuration
public class SwaggerConfiguration {

    public Docket createRestApi(){
        return new Docket(documentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(Operation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot整合swagger3的接口文档")
                .description("描述图书馆管理的接口文档")
                .contact(new Contact("aaa","http://www.baidu.com","dyit@.com"))
                .version("1.0")
                .build();
    }
}
@Schema(description = "dto对象")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class HttpRest {
    @Schema(description = "后台返回代码")
    private Integer code;
    @Schema(description = "后台返回信息")
    private String msg;
    @Schema(description = "后台返回数据")
    private Object results;
    @Schema(description = "后台返回数据的时间")
    private Date date;
}
@RestController
@RequestMapping("/api/publisher")
@Slf4j
@Tag(name = "出版社模块")
public class PublisherController {
    @Autowired
    private IPublisherService ips;

    @Operation(summary = "获取此出版社信息方法")
    @GetMapping("/findAll")
    public HttpRest findAll(){
        List list = ips.findAll();
        log.debug("获取所有出版社的信息");
        return new HttpRest(20001,"查询出版社信息成功",list,new Date());
    }
}

运行

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

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

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