zl程序教程

您现在的位置是:首页 >  后端

当前栏目

SpringBoot入门整合 上手 简单易解

SpringBoot入门 简单 整合
2023-06-13 09:17:12 时间

目录

概述

作用

快速入门 创建SpringBoot项目

没有网络创建SpringBoot项目

教你一招 隐藏指定文件

配置文件

整合第三方技术

整合Junit

整合MyBatis

整合MyBatis-plus

整合Driver


概述

SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程

作用

由于复杂的配置和混乱的依赖管理 让开发者需要在配置和业务代之间频繁切换 而SpringBoot的出现正是解决了这种问题 让开发者只需要关注业务代码 而不需要关注配置 最主要的作用就是帮助我们快速构建庞大的spring项目 并且尽可能的减少一切xml配置 坐到开箱即用 迅速上手 让我们关注业务而非配置

快速入门 创建SpringBoot项目

        前提:确保网络连接正常

        填写包名 选择maven项目 选择对应的jdk版本 其它不要动

        选择吗对应的spring版本 对此springboot项目创建完毕

没有网络创建SpringBoot项目

        当没有网络的时候一样可以创建springboot项目

        步骤:

        1.点击maven 创建maven 不要勾选使用骨架

        2.填写坐标

        3.目录结构

        4.项目结构

        在pom.xml文件中添加父共程坐标

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

        这个时候就会出现大量的依赖

        再添加一个springboot的web启动器 一个springboot web项目就搭建完毕了

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

        完整pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>
    <groupId>com.czxy</groupId>
    <artifactId>springboot-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

教你一招 隐藏指定文件

        对于springboot项目 有好多不需要的文件 我们可以隐藏它 步骤 Setting → File Types → Ignored Files and Folders 输入要隐藏的文件名,支持*号通配符

配置文件

整合第三方技术

整合Junit

        @SpringBootTest设置Junit为SpringBoot测试类

        classes:设置springboot启动类

        如果测试类在启动类所在的包或子包中 可以省略classes

@SpringBootTest(classes = springboot.class)
public class TestA {
@SpringBootTest
public class TestA {

    @Autowired
    private UserService userService;

    @Test
    public void test01(){
        System.out.println("1");
        userService.selectAll();
    }
}

整合MyBatis

        步骤:

                1.选择当前模块技术MyBatis MySQL

                2.配置文件中 设置数据参数(SpringBoot版本低于2.4.3不含 Mysql版本大于8.0时需要设置时区 或在Mysql数据库端配置时区解决此问题)

jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC

                3.数据层添加@Mapper被容器识别

@Mapper
public interface UserMapper {

    @Select("select * from user")
    public List<User> selectAll();
}

整合MyBatis-plus

        SpringBoot整合MyBatis-plus和整合MyBatis步骤大致相同

        MyBatis-plus坐标需要手动导入

        由于SpringBoot未收录MyBatis-plus 需要指定version版本

        定义数据层继承BaseMapper

        其他步骤与整合MyBatis一样

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
@Mapper
public interface UserDao extends BaseMapper<User> {
}

整合Driver

        导入Driver坐标

        配置Driver配置

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
username: root
password: root