SpringBoot------初始SpringBoot

1.spring发展:

  • Servlet + jsp :原生开发,十分的麻烦,web.xml 或者代码中都会存在大量重复内容;
  • Spring 春天:2003 年====> 2020年,里面所有东西都是配置文件。集成很多框架或者做一些大型项目,会导致整个程序和项目十分的臃肿;通篇的配置文件;web.xml,tomcat 都要配置,lib依赖 也需要管理
  • SpringBoot:为了简化配置文件,就好像Spring的升级版,原来很多东西需要手动配置,现在只需要自动配置!

在Spring中,启动一个服务,就好比启动一个helloworld

2 编写一个springboot程序

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

编写HelloController

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "I miss you,my lover";
    }
}

启动主启动类,访问8080端口进行测试

注意点:新建的Controller类一定要在主启动类的子级,否则无法访问
在这里插入图片描述

3.自定义启动Logo

  • 1.在resource下新建banner.txt文件
  • 2.在在线生成网站上生成属于自己的logo:https://www.bootschool.net/ascii
  • 3.启动主进行测试启动类
    在这里插入图片描述

4. 自己编写主启动类

  • 结构不发生改变,增加@SpringBootApplication注解及使用其run方法
@SpringBootApplication
public class myApplication {

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

5.依赖问题

父依赖

<!-- 父依赖作用分析
1、自动帮你管理依赖,里面包含了几乎常用的所有依赖,如果你需要的依赖在这里面有,你就不要配置了,如果没有再配置
2、插件和资源过滤的自动管理;
-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

启动器,官网:https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/html/using-spring-boot.html#using-boot

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

6.配置:yaml,properties

yaml对空格缩进要求很严格

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  type-aliases-package: com.yang.hellospringboot.pojo
  mapper-locations: classpath:com/yang/hellospringboot/mapper/*.xml

properties

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

  mybatis.type-aliases-package= com.yang.hellospringboot.pojo
  mybatis.mapper-locations: classpath:com/yang/hellospringboot/mapper/*.xml
发布了87 篇原创文章 · 获赞 7 · 访问量 5028

猜你喜欢

转载自blog.csdn.net/y18791050779/article/details/105079570