使用工具:IDEA2018,Mysql8.0(用的是PHP study自带的mysql8.0,安装方便),Tomcat9.0

首先新建一个springboot项目

Springboot增删改时序图 基于springboot的增删改查_Springboot增删改时序图

点next

Springboot增删改时序图 基于springboot的增删改查_User_02

next

Springboot增删改时序图 基于springboot的增删改查_spring_03

就建成项目了

修改配置文件

然后新建application.yml和application-dev.yml(原来的application.properties可以删除,spring boot会把application.yml解析成application.properties)

application.yml 

spring:
  profiles:
    active: dev

application-dev.yml

server:
  port: 8080

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

mybatis:
  mapper-locations: classpath:mapping/*Mapping.xml
  type-aliases-package: com.example.entity

#showSql
logging:
  level:
    com:
      example:
        mapper: debug

两个文件的意思是:

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

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

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

还有在配置文件中不要添加中文注释,会报错

接下来把启动文件(DemoApplication)移到com.example下,而且springboot的启动类不能放在java目录下!!!必须要个包将它包进去

否则会报错误:

Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
  • 然后开始新建实体类实现业务流程,创建包controller、entity、mapper、service。resources下创建mapping文件夹,用于写sql语句,也可以用注解的方式直接写在mapper文件里。下面直接贴代码
CREATE TABLE `user` (
  `id` int(32) NOT NULL AUTO_INCREMENT,
  `userName` varchar(32) NOT NULL,
  `passWord` varchar(50) NOT NULL,
  `realName` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

Springboot增删改时序图 基于springboot的增删改查_spring_04

  • entity.java
package com.example.entity;

/**
 * Created with IntelliJ IDEA.
 * User: LYP_PC
 * Date: 2020/2/8
 * Time: 17:31
 * Description: No Description
 */
public class User {
    private Integer id;
    private String userName;
    private String passWord;
    private String realName;

    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;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                ", realName='" + realName + '\'' +
                '}';
    }
}
  • UserConteoller.java
package com.example.controller;

import com.example.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;

/**
 * Created with IntelliJ IDEA.
 * User: LYP_PC
 * Date: 2020/2/8
 * Time: 17:46
 * Description: No Description
 */
@RestController
@RequestMapping("/testBoot")
public class UserController {

    @Autowired
    private UserService userService;

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

UserService.java

package com.example.service;

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

/**
 * Created with IntelliJ IDEA.
 * User: LYP_PC
 * Date: 2020/2/8
 * Time: 17:55
 * Description: No Description
 */
@Service
public class UserService {

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

UserMapper.java

package com.example.mapper;

import com.example.entity.User;
import org.springframework.stereotype.Repository;

/**
 * Created with IntelliJ IDEA.
 * User: LYP_PC
 * Date: 2020/2/8
 * Time: 17:57
 * Description: No Description
 */
@Repository
public interface UserMapper {

    User Sel(int id);
}

Usermapping.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.example.mapper.UserMapper">


    <resultMap id="BaseResultMap" type="com.example.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.example.entity.User">
        select * from user where id = #{id}
    </select>

</mapper>

 最终框架结构

Springboot增删改时序图 基于springboot的增删改查_User_05

完成以上,下面在启动类里加上注解用于给出需要扫描的mapper文件路径@MapperScan("com.example.mapper") 

package com.example;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.example.mapper") //扫描的mapper
@SpringBootApplication
public class DemoApplication {

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

}

最后启动,浏览器输入地址看看吧:http://localhost:8080/testBoot/getUser/1

Springboot增删改时序图 基于springboot的增删改查_User_06

测试成功,就这样基本框架就搭建成功了。

最后给个番外篇如何更改启动时显示的字符拼成的字母,就是更改下图标红框的地方

Springboot增删改时序图 基于springboot的增删改查_Springboot增删改时序图_07


其实很好改,只需要在resources下新建一个txt文件就可以,命名为banner.txt,那这种字符该怎么拼出来呢,下面推荐一个网址,有这种工具,链接传送门:字母转字符。如下:

Springboot增删改时序图 基于springboot的增删改查_bc_08

直接输入要生成的字母,系统会自动转换,然后复制下面转换好的字符到新建的banner.txt文件中,重新启动项目就可以了。

Springboot增删改时序图 基于springboot的增删改查_User_09