SpringBoot学习 (2)搭建一个完整的项目 SpringBoot+SpringMVC+MyBatis

前言

记录学习过程,前接上一步
跟着大佬的步骤学习
学习了Spring Boot发现搭建框架还能这么简单的

步骤

Spring Boot特点编辑(百度)

  1. 创建独立的Spring应用程序

  2. 嵌入的Tomcat,无需部署WAR文件

  3. 简化Maven配置

  4. 自动配置Spring

  5. 提供生产就绪型功能,如指标,健康检查和外部配置

  6. 绝对没有代码生成和对XML没有要求配置

SpringBoot的核心功能

起步依赖

起步依赖本质上是一个Maven项目对象模型(Project Object Model,POM),定义了对其他库的传递依赖,这些东西加在一起即支持某项功能。简单的说,起步依赖就是将具备某种功能的坐标打包到一起,并提供一些默认的功能。

自动配置

SpringBoot的自动配置是一个运行时(更准确地说,是应用程序启动时)的过程,考虑了众多因素,才决定Spring配置应该用哪个,不该用哪个。该过程是Spring自动完成的。

  • 基于Maven补充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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--SpringBoot要求,项目要继承SpringBoot的起步依赖spring-boot-starter-parent-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test</groupId>
    <artifactId>springboottest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboottest</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--整合SpringMVC,要导入web的启动依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--整合MyBatis,要导入mybatis起步依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
        <!--MySql连接驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.41</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

其中注意mysql驱动的写法,版本号,可以会连接不上数据库

  • 建好数据库,user表
    在这里插入图片描述
  • 项目架构

注意:
SpringboottestApplication启动类要放在主要的包下
所有的包名别大写,会识别不了

不使用application.properties文件 而使用更加简洁的application.yml文件
将resource文件夹下原有的application.properties文件删除,创建application.yml配置文件(SpringBoot底层会把application.yml文件解析为application.properties)

在项目中配置多套环境的配置方法。 因为现在一个项目有好多环境,开发环境,测试环境,准生产环境,生产环境,每个环境的参数不同,所以我们就可以把每个环境的参数配置到yml文件中,这样在想用哪个环境的时候只需要在主配置文件中将用的配置文件写上就行如application.yml

笔记:在Spring
Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应你的环境标识,比如:

application-dev.yml:开发环境
application-test.yml:测试环境
application-prod.yml:生产环境
至于哪个具体的配置文件会被加载,需要在application.yml文件中通过spring.profiles.active属性来设置,其值对应{profile}值。

在这里插入图片描述

  • 配置文件
    application.yml
spring:
  profiles:
    active: dev

application-dev.yml
其中配置数据库的参数:username、password、url写自己的数据库参数
Spring集成MyBatis环境添加自己的包名

server:
  port: 8080

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

#Spring集成MyBatis环境
mybatis:
  #加载MyBatis映射文件,以Mapper.xml结尾的文件
  mapper-locations: classpath:mapping/*Mapper.xml
  #实体类扫描包
  type-aliases-package: com.test.springboottest.entity
  • entity创建实体类
    符号数据库的列
package com.test.springboottest.entity;

public class User {
    private Integer id;
    private String userName;
    private String passWord;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }



    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                '}';
    }
}

  • 数据访问层Usermapper接口
package com.test.springboottest.mapper;


import com.test.springboottest.entity.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;


@Repository
public interface UserMapper {

    User Sel(int id);
}

  • 数据库操作UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.springboottest.mapper.UserMapper">

    <resultMap id="BaseResultMap" type="com.test.springboottest.entity.User">
        <result column="id" jdbcType="INTEGER" property="id" />
        <result column="userName" jdbcType="VARCHAR" property="userName" />
        <result column="passWord" jdbcType="VARCHAR" property="passWord" />
        <result column="realName" jdbcType="VARCHAR" property="realName" />
    </resultMap>

    <select id="Sel" resultType="com.test.springboottest.entity.User">
        select * from user where id = #{id}
    </select>

</mapper>

  • 业务层service
    (一般都是一个接口加一个实现类,这里就懒的写了)
package com.test.springboottest.service;



import com.test.springboottest.entity.User;
import com.test.springboottest.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    public User Sel(int id){
        return userMapper.Sel(id);
    }
}

  • 控制器UserController
package com.test.springboottest.controller;


import com.test.springboottest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/testBoot")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("getUser/{id}")
    public String GetUser(@PathVariable int id){
        return userService.Sel(id).toString();
    }
}

  • 在启动器添加扫描mapper的类
package com.test.springboottest;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
//@MapperScan注解,扫描Mapper接口上的类
@MapperScan("com.test.springboottest.mapper") 
@SpringBootApplication
public class SpringboottestApplication {

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

}

  • 运行
    在这里插入图片描述
    在浏览器输入地址 http://localhost:8080/testBoot/getUser/1
    在这里插入图片描述
    到这就完成了SpringBoot+SpringMVC+MyBatis的项目搭建
    就这个简单的项目可以看出Spring Boot让项目简洁了许多,运行项目也快

  • 可以改控制台显示图案
    这是原来的日志图片,可以修改
    在这里插入图片描述
    在resources里添加一个banner.txt
    在这里插入图片描述
    http://www.network-science.de/ascii/这个网站可以生成文字banner
    在这里插入图片描述
    在banner.txt里:


      ___           ___                                  ___           ___
     /__/\         /  /\                  _____         /__/\         /  /\
     \  \:\       /  /::\                /  /::\        \  \:\       /  /:/_
      \  \:\     /  /:/\:\              /  /:/\:\        \  \:\     /  /:/ /\
  _____\__\:\   /  /:/  \:\            /  /:/~/::\   ___  \  \:\   /  /:/_/::\
 /__/::::::::\ /__/:/ \__\:\          /__/:/ /:/\:| /__/\  \__\:\ /__/:/__\/\:\
 \  \:\~~\~~\/ \  \:\ /  /:/          \  \:\/:/~/:/ \  \:\ /  /:/ \  \:\ /~~/:/
  \  \:\  ~~~   \  \:\  /:/            \  \::/ /:/   \  \:\  /:/   \  \:\  /:/
   \  \:\        \  \:\/:/              \  \:\/:/     \  \:\/:/     \  \:\/:/
    \  \:\        \  \::/                \  \::/       \  \::/       \  \::/
     \__\/         \__\/                  \__\/         \__\/         \__\/
    ::  Spring Boot ::       (v${spring-boot.version})

完成,运行成功:
在这里插入图片描述

发布了49 篇原创文章 · 获赞 0 · 访问量 1246

猜你喜欢

转载自blog.csdn.net/key_768/article/details/104125782