• SpringBoot基础入门


    SpringBoot基础入门

    1 创建SpringBoot项目

    1.1 通过pom继承parent

    通过parent标签指定pom项目的父工程

    <?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>
    
        <groupId>com.ali</groupId>
        <artifactId>springBoot_test_01</artifactId>
        <version>1.0-SNAPSHOT</version>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>8</source>
                        <target>8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.4</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    创建controller及启动类
    controller层:

    package com.zi.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class FirstController {
    
        @ResponseBody
        @RequestMapping("/first")
        public String firstController(){
            return "hello SpringBoot...";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    package com.zi;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class MySpringBootApplication {
        public static void main(String[] args) {
            SpringApplication.run(MySpringBootApplication.class, args);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    启动类需要与controller层、service层等包同层级【SpringBoot默认扫描启动类所在包及其子包】

    1.2 通过依赖管理导入SpringBoot依赖【dependencyManagement】

    在工作中我们有可能遇到我们的maven项目已经要继承一个parent项目了,此时应该使用dependencyManagement来创建SpringBoot项目

    <?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>
    
        <groupId>com.ali</groupId>
        <artifactId>springBoot_test_01</artifactId>
        <version>1.0-SNAPSHOT</version>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>8</source>
                        <target>8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        
        <!--<parent>-->
        <!--    <groupId>org.springframework.boot</groupId>-->
        <!--    <artifactId>spring-boot-starter-parent</artifactId>-->
        <!--    <version>2.5.4</version>-->
        <!--</parent>-->
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-parent</artifactId>
                    <version>2.7.1</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.7.1</version>
            </dependency>
        </dependencies>
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    1.3 通过可视化界面

    IDEA自带的SpringBoot初始化插件

    2 SpringBoot中的配置文件与资源访问

    SpringBoot中的配置文件只能是以application开头【application*】

    2.1 properties

    # 设置端口
    server.port=8081
    # 设置上下文路径
    server.servlet.context-path=/springboot02
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.2 yml

    # yml格式的文件,区分大小写,且层级之间要有两个空格;
    # 属性与值之间要留一个空格
    server:
      port: 8888
      servlet:
        context-path: /springboot02
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    注意:
    ①yml也可以配置数组或者json格式的数据;
    ②如果同时存在properties与yml文件,如果存在冲突,以properties为准

    2.3 访问static或public资源

    访问路径上不需要加/static或者/public【并且二者之间可以互相访问】

    在这里插入图片描述
    a.html代码:

    DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
        <img src="logo.jpg"/>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2.4 访问templates资源

    类似于JavaWeb中的WEB-INF里面的内容,属于私密内容,外界无法直接访问,只能通过内部跳转访问
    【FreeMarker thymeleaf 页面所在目录 】

    SpringBoot的项目结构中不同于以前的JavaWeb项目,没有webapp目录【只有需要用到jsp技术,才会有该目录】

    3 SpringBoot整合MyBatis

    3.1 导入jar包并创建对应包结构

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.28</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.2.2</version>
            </dependency>
        </dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    对应包结构:
    在这里插入图片描述

    3.2 创建User类

    package com.zi.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.io.Serializable;
    
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public class User implements Serializable {
        private Integer id;
        private String name;
        private Integer age;
        private String email;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3.3 创建UserMapper接口与UserMapper.xml

    UserMapper接口:

    package com.zi.mapper;
    
    import com.zi.pojo.User;
    import org.apache.ibatis.annotations.Mapper;
    
    import java.util.List;
    
    @Mapper
    public interface UserMapper {
        /**
         * 查询所有用户
         * @return
         */
        List<User> selectAll();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    UserMapper.xml

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.zi.mapper.UserMapper">
    
        
        <select id="selectAll" resultType="user">
            select * from t_user
        select>
    
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3.4 创建UserService与UserServiceImpl

    UserService:

    package com.zi.service;
    
    import com.zi.pojo.User;
    
    import java.util.List;
    
    public interface UserService {
        List<User> findAll();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    UserServiceImpl:

    package com.zi.service.impl;
    
    import com.zi.mapper.UserMapper;
    import com.zi.pojo.User;
    import com.zi.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserMapper userMapper;
    
        @Override
        public List<User> findAll() {
            return userMapper.selectAll();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3.5 创建UserController

    package com.zi.controller;
    
    import com.zi.pojo.User;
    import com.zi.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.List;
    
    @Controller
    @RequestMapping("/user")
    public class UserController {
        @Autowired
        private UserService userService;
    
        @ResponseBody
        @RequestMapping("/findAll")
        public List<User> findAll(){
            return userService.findAll();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    3.6 最终结果

    地址栏内容:
    在这里插入图片描述
    最终数据:

    在这里插入图片描述

    4 SpringBoot整合logback(日志)

    logback的依赖,我们不需要亲自引入,spring-boot-starter或者spring-boot-starter-web中已经包含了Logback的依赖。

    在resources目录下创建logback.xml:

    
    <configuration>
        
        <property name="LOG_HOME" value="${catalina.base}/logs/"/>
        
        <appender name="Stdout" class="ch.qos.logback.core.ConsoleAppender">
            
            <layout class="ch.qos.logback.classic.PatternLayout">
                
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
            layout>
        appender>
        
        <appender name="RollingFile"
                  class="ch.qos.logback.core.rolling.RollingFileAppender">
            <rollingPolicy
                    class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                
    
                <FileNamePattern>${LOG_HOME}/server.%d{yyyy-MM-dd}.logFileNamePattern>
                <MaxHistory>30MaxHistory>
            rollingPolicy>
            <layout class="ch.qos.logback.classic.PatternLayout">
                
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}
                    [%thread] %-5level %logger{50} - %msg%n
                pattern>
            layout>
            
            <triggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <MaxFileSize>10MBMaxFileSize>
            triggeringPolicy>
        appender>
    
        
        <root level="info">
            <appender-ref ref="Stdout"/>
            <appender-ref ref="RollingFile"/>
        root>
    
        <logger name="com.zi.mapper" level="DEBUG"/>
    
    
        
        
    
    configuration>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    5 SpringBoot整合PageHelper

    5.1 导入启动依赖

    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.5</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    5.2 定义EmpController

    @Controller
    @RequestMapping("/emp")
    public class EmpController {
    
        @Autowired
        private EmpService empService;
    
        @ResponseBody
        @RequestMapping("/findByPage/{pageNum}/{pageSize}")
        public List<Emp> findAll(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
            return empService.findByPage(pageNum, pageSize);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    5.3 定义EmpMapper及EmpMapper.xml

    EmpMapper:

    @Mapper
    public interface EmpMapper {
        List<Emp> findAll();
    }
    
    • 1
    • 2
    • 3
    • 4

    EmpMapper.xml:

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.zi.mapper.EmpMapper">
    
        <select id="findAll" resultType="emp">
            select * from emp
        select>
    
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    5.4 定义EmpService及实现类

    EmpService:

    public interface EmpService {
        List<Emp> findAll();
    
        List<Emp> findByPage(Integer pageNum, Integer pageSize);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    EmpServiceImpl:

    @Service
    public class EmpServiceImpl implements EmpService {
    
        @Autowired
        private EmpMapper empMapper;
    
        @Override
        public List<Emp> findAll() {
            return empMapper.findAll();
        }
    
        @Override
        public List<Emp> findByPage(Integer pageNum, Integer pageSize) {
            Page<Emp> page = PageHelper.startPage(pageNum, pageSize);
            List<Emp> emps = empMapper.findAll();
            PageInfo<Emp> pi =new PageInfo<>(emps);
            System.out.println("当前页"+pi.getPageNum());
            System.out.println("总页数"+pi.getPages());
            System.out.println("页大小"+pi.getSize());
            System.out.println("总记录数"+pi.getTotal());
            System.out.println("当前页数据"+pi.getList());
            return emps;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    5.5 测试

    ①地址栏中输入:
    在这里插入图片描述
    ②响应结果:
    在这里插入图片描述

  • 相关阅读:
    MySQL 字符集与乱码与collation设置的问题?
    Authing 入选 Gartner《2022 中国网络安全技术成熟度曲线》报告
    算法篇------贪心1
    Spring6 - ioc
    konachan网站之用户脚本优化Konachan站点浏览体验
    Word处理控件Aspose.Words功能演示:使用 Python 查找和替换 Word 文档中的文本
    Jmeter测试关联接口
    LLM:huggingface-datasets库
    【Linux】信号量和线程池
    基于docker搭建code-server(浏览器中的VScode)
  • 原文地址:https://blog.csdn.net/weixin_45565886/article/details/125802786