SpringBoot2学习

这是我参与11月更文挑战的第10天,活动详情查看:2021最后一次更文挑战

概述

Spring Boot 是 Pivotal 团队在 Spring 的基础上提供的一套全新的开源框架,其目的是为了简化 Spring 应用的搭建和开发过程。

SpringBoot的优点简化依赖配置,简化工程配置,有很多辅助功能比如内置tomcat。我们来搭建一个入门的SpringBoot项目,idea联网在线创建

image.png 选择依赖 这里就选择一个web就行了然后卢next等创建完成,导入依赖完成

image.png 新建一个类,Controller包要与SpringBootHeimaApplication平齐 image.png

@RequestMapping("/books")
@RestController
public class TestController {

    @GetMapping
    public String getByid(){
        System.out.println("hello SpringBoot");
        return "Spring is Running.....";
    }
}
复制代码

运行一下,这里可以看到我们的tomcat使用的是 [Apache Tomcat/9.0.54] 端口是 Tomcat started on port(s): 8080,我们在浏览器访问一下

image.png 访问成功 image.png

那到底SpringBoot是指如何给我们做到简化配置,快速开发的呢,我们来分析一下我们的入门程序,其实他是通过四个东西来实现的简化开发,分别是parent,starter,引导类,内部服务器

parent

在parent标签里spring-boot-starter-parent继承了spring-boot-dependencies而这个spring-boot-dependencies里定义了多个properties自定义属性名,有定义了多个 坐标管理引用这些属性,我们在使用时版本不用写直接用。在用阿里云构建工程时,他直接在dependencyMangement标签里引用了spring-boot-dependencies效果是一样的。不同的Springboot版本引用的是不一样的。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.6</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
复制代码
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>2.5.6</version>
</parent>
复制代码

image.png image.png

starter

既然spring-boot-dependencies里定了了这么多依赖坐标,那谁在用,我呢看下pom文件的dependency看到是他在引用,这里写了一个spring-boot-starter-web这个里边我们点进去可以看到他的dependency里引用了我要用的具体依赖,这些SpringBoot已经给我们做好了,我们只需要引入starter就可以了。在实际开发中我们可能有些不写Version会报错,那是因为Springboot没有引入这个依赖,这是就需要我们自己去引入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
复制代码
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.3.12</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.3.12</version>
  <scope>compile</scope>
</dependency>
复制代码

引导类

我们配置好一个项目要去运行,就要通过引导类,我们引导类的作用就时创建Spring容器,把我们的Bean交给spring管理,run方法生成就是一个容器对象,我们可以通过getBean获取这个bean对象,@SpringBootApplication注解里@SpringBootConfiguration和@ComponentScan表明这个类就是一个配置类和包扫描,所以我们的程序要写在跟引导类在再一个目录下,要不然它加载不到我们的bean信息

@SpringBootApplication
public class SpringBootHeimaApplication {

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

}
复制代码

辅助功能

我们在上边运行程序时没有引入tomcat的,但是还是能够运行,就是web-start里引入了tomcat的依赖内置了tomcat,Springboot把tomecat作为bean交给Spring管理。

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
  <version>2.5.6</version>
  <scope>compile</scope>
</dependency>
复制代码

基础配置

springBoot的配置文件都在src/main/resources/application.properties下采用键值对的形式来写,写配置的前提是引入了相应的Starter,下边写了几个简单的配置,SpringBoot的配置文件有三种格式,另外两种以.yml和.yaml,主流yml格式。共同存在时,不同配置文件中相同配置按加载优先级相互覆盖,不同配置文件中不同配置全部保留

# 服务器端口配置
server.port= 80
# 修改banner
# 关闭banner
#spring.main.banner-mode=off

#日志
logging.level.root=info
复制代码

整合框架

整合Junit

Springboot默认导入了Junit的包并创建了测试文件,我们来测试一下。如果测试类不在启动类的包或子包内,就会测试报错,获取不到Spring容器,设施后就需要使用SpringBootTest的class属性来指定启动类的位置

public interface UserDao {
    public void save();
}
复制代码
@Repository
public class UserDaoImpl implements UserDao {

    @Override
    public void save() {
        System.out.println("save 执行了");
    }
}
复制代码
@SpringBootTest
class SpringBootHeimaApplicationTests {

    @Autowired
    private UserDao userDao;
    @Test
    void contextLoads() {
        userDao.save();
    }
}
复制代码

image.png

整合Mybatis

导入Mybatis依赖,数据库依赖,Lombok依赖

<dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <scope>runtime</scope>
 </dependency>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
 </dependency>
 <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
 </dependency>
复制代码

配置数据库配置文件

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis_test?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: root
复制代码

实体类

@Data
public class Student {
    private Integer id;
    private String studentName;
    private Integer age;
    private String address;
    private Integer gradeId;
}
复制代码

mapper文件,这里我饿们用注解实现查询所有

@Mapper
public interface StudentMapper {
    @Select("select * from student")
    List<Student> findAll();
}
复制代码

测试一下

@Autowired
private StudentMapper studentMapper;
@Test
void contextLoads() {
    List<Student> all = studentMapper.findAll();
    for (Student student : all) {
        System.out.println(student);
    }
}
复制代码

image.png

整合Mybatis-Plus

导入MybatisPlus的依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3.1</version>
</dependency>
复制代码

mapper接口继承BaseMapper

@Mapper
public interface StudentMapper extends BaseMapper<Student> {

}
复制代码

测试

@Autowired
private StudentMapper studentMapper;
@Test
void contextLoads() {
    Student student = studentMapper.selectById("41");
    System.out.println(student);
}
复制代码

猜你喜欢

转载自juejin.im/post/7031699796440121351