# 从零开始创建SpringBoot项目

SpringBoot2 MyBatis MySQL 从零开始搭建增删改查的Web项目,结构如下图

4JNL8J.png

# 准备工作

  1. MySQL 新建一个tb_user表
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user`  (
  `user_id` int(0) NOT NULL DEFAULT 0 COMMENT '主键ID',
  `name` varchar(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NULL DEFAULT NULL COMMENT '名称',
  `nickname` varchar(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NULL DEFAULT NULL COMMENT '名称',
  `age` int(0) NULL DEFAULT NULL COMMENT '年龄',
  `avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NULL DEFAULT NULL COMMENT '头像',
  `phone` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NULL DEFAULT NULL COMMENT '电话',
  `address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NULL DEFAULT NULL COMMENT '地址',
  `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NULL DEFAULT NULL COMMENT '备注',
  `created_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `updated_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间'
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_520_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES (10002, '马云01', '风清扬', 18, '', '15566886688', '浙江省杭州市', '1964年9月10日生于浙江省杭州市,祖籍浙江省嵊州市谷来镇, 阿里巴巴集团主要创始人', '2021-07-28 22:38:49', NULL);
INSERT INTO `tb_user` VALUES (10003, '马化腾01', 'pony', 16, '', '13900889988', '广东省汕头市潮南区', '1993年获深圳大学理学学士学位。腾讯公司主要创办人之一。现任腾讯公司董事会主席兼首席执行官;', '2021-07-28 22:38:49', NULL);

SET FOREIGN_KEY_CHECKS = 1;
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
  1. UserEntity 实体
@Data
public class UserEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 主键id
     */
    private Integer userId;

    /**
     * 名称
     */
    private String name;

    /**
     * 名称
     */
    private String nickname;

    /**
     * 年龄
     */
    private Integer age;

    /**
     * 头像
     */
    private String avatar;

    /**
     * 电话
     */
    private String phone;

    /**
     * 地址
     */
    private String address;

    /**
     * 备注
     */
    private String remark;

    /**
     * 创建时间
     */
    private Date createdTime;

    /**
     * 更新时间
     */
    private Date updatedTime;

}
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
  1. UserMapper
public interface UserMapper {

    /**
     * 新增
     *
     * @author biubiu
     * @date 2021/08/08
     **/
    int insert(UserEntity userEntity);

    /**
     * 刪除
     *
     * @author biubiu
     * @date 2021/08/08
     **/
    int delete(int id);

    /**
     * 更新
     *
     * @author biubiu
     * @date 2021/08/08
     **/
    int update(UserEntity userEntity);

    /**
     * 查询 根据主键 id 查询
     *
     * @author biubiu
     * @date 2021/08/08
     **/
    UserEntity load(int id);

    /**
     * 查询 分页查询
     *
     * @author biubiu
     * @date 2021/08/08
     **/
    List<UserEntity> pageList(int pageNum, int pageSize);

    /**
     * 查询 分页查询 count
     *
     * @author biubiu
     * @date 2021/08/08
     **/
    int pageListCount(int pageNum, int pageSize);
}
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
  1. 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.biubiu.multidatabase.mapper.UserMapper">

    <resultMap id="BaseResultMap" type="com.biubiu.multidatabase.entity.UserEntity" >
        <result column="user_id" property="userId" />
        <result column="name" property="name" />
        <result column="nickname" property="nickname" />
        <result column="age" property="age" />
        <result column="avatar" property="avatar" />
        <result column="phone" property="phone" />
        <result column="address" property="address" />
        <result column="remark" property="remark" />
        <result column="created_time" property="createdTime" />
        <result column="updated_time" property="updatedTime" />
    </resultMap>

    <sql id="Base_Column_List">
        user_id,
        name,
        nickname,
        age,
        avatar,
        phone,
        address,
        remark,
        created_time,
        updated_time
    </sql>

    <insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id"
            parameterType="com.biubiu.multidatabase.entity.UserEntity">
        INSERT INTO tb_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != userId and '' != userId">
                user_id,
            </if>
            <if test="null != name and '' != name">
                name,
            </if>
            <if test="null != nickname and '' != nickname">
                nickname,
            </if>
            <if test="null != age and '' != age">
                age,
            </if>
            <if test="null != avatar and '' != avatar">
                avatar,
            </if>
            <if test="null != phone and '' != phone">
                phone,
            </if>
            <if test="null != address and '' != address">
                address,
            </if>
            <if test="null != remark and '' != remark">
                remark,
            </if>
            <if test="null != createdTime and '' != createdTime">
                created_time,
            </if>
            <if test="null != updatedTime and '' != updatedTime">
                updated_time
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != userId and '' != userId">
                #{userId},
            </if>
            <if test="null != name and '' != name">
                #{name},
            </if>
            <if test="null != nickname and '' != nickname">
                #{nickname},
            </if>
            <if test="null != age and '' != age">
                #{age},
            </if>
            <if test="null != avatar and '' != avatar">
                #{avatar},
            </if>
            <if test="null != phone and '' != phone">
                #{phone},
            </if>
            <if test="null != address and '' != address">
                #{address},
            </if>
            <if test="null != remark and '' != remark">
                #{remark},
            </if>
            <if test="null != createdTime and '' != createdTime">
                #{createdTime},
            </if>
            <if test="null != updatedTime and '' != updatedTime">
                #{updatedTime}
            </if>
        </trim>
    </insert>

    <delete id="delete" >
        DELETE FROM tb_user
        WHERE user_id = #{id}
    </delete>

    <update id="update" parameterType="com.biubiu.multidatabase.entity.UserEntity">
        UPDATE tb_user
        <set>
            <if test="null != userId and '' != userId">user_user_id = #{userId},</if>
            <if test="null != name and '' != name">name = #{name},</if>
            <if test="null != nickname and '' != nickname">nickname = #{nickname},</if>
            <if test="null != age and '' != age">age = #{age},</if>
            <if test="null != avatar and '' != avatar">avatar = #{avatar},</if>
            <if test="null != phone and '' != phone">phone = #{phone},</if>
            <if test="null != address and '' != address">address = #{address},</if>
            <if test="null != remark and '' != remark">remark = #{remark},</if>
            <if test="null != createdTime and '' != createdTime">created_time = #{createdTime},</if>
            <if test="null != updatedTime and '' != updatedTime">updated_time = #{updatedTime}</if>
        </set>
        WHERE user_id = #{id}
    </update>


    <select id="load" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM tb_user
        WHERE user_id = #{id}
    </select>

    <select id="pageList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM tb_user
        LIMIT #{pageNum}, #{pageSize}
    </select>

    <select id="pageListCount" resultType="java.lang.Integer">
        SELECT count(1)
        FROM tb_user
    </select>

</mapper>
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  1. service层
  • UserService
public interface UserService {
    /**
     * 新增
     */
    public Object insert(UserEntity tbUserEntity);

    /**
     * 删除
     */
    public ReturnT<String> delete(int id);

    /**
     * 更新
     */
    public ReturnT<String> update(UserEntity tbUserEntity);

    /**
     * 根据主键 id 查询
     */
    public UserEntity load(int id);

    /**
     * 分页查询
     */
    public Map<String, Object> pageList(int offset, int pagesize);
}
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
  • UserServiceImpl
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;


    @Override
    public Object insert(UserEntity tbUserEntity) {

        // valid
        if (tbUserEntity == null) {
            return ReturnT.error("必要参数缺失");
        }

        userMapper.insert(tbUserEntity);
        return ReturnT.success();
    }


    @Override
    public ReturnT<String> delete(int id) {
        int ret = userMapper.delete(id);
        return ret > 0 ? ReturnT.success() : ReturnT.error();
    }


    @Override
    public ReturnT<String> update(UserEntity tbUserEntity) {
        int ret = userMapper.update(tbUserEntity);
        return ret > 0 ? ReturnT.success() : ReturnT.error();
    }


    @Override
    public UserEntity load(int id) {
        return userMapper.load(id);
    }


    @Override
    public Map<String, Object> pageList(int pageNum, int pageSize) {

        List<UserEntity> pageList = userMapper.pageList(pageNum, pageSize);
        int totalCount = userMapper.pageListCount(pageNum, pageSize);

        // result
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("pageList", pageList);
        result.put("totalCount", totalCount);

        return result;
    }
}
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
  1. controller
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 新增
     *
     * @author biubiu
     * @date 2021/08/08
     **/
    @RequestMapping("/insert")
    public Object insert(UserEntity tbUser) {
        return userService.insert(tbUser);
    }

    /**
     * 刪除
     *
     * @author biubiu
     * @date 2021/08/08
     **/
    @RequestMapping("/delete")
    public ReturnT<String> delete(int id) {
        return userService.delete(id);
    }

    /**
     * 更新
     *
     * @author biubiu
     * @date 2021/08/08
     **/
    @RequestMapping("/update")
    public ReturnT<String> update(UserEntity tbUser) {
        return userService.update(tbUser);
    }

    /**
     * 查询 根据主键 id 查询
     *
     * @author biubiu
     * @date 2021/08/08
     **/
    @RequestMapping("/load")
    public Object load(int id) {
        return userService.load(id);
    }

    /**
     * 查询 分页查询
     *
     * @author biubiu
     * @date 2021/08/08
     **/
    @RequestMapping("/pageList")
    public Map<String, Object> pageList(@RequestParam(required = false, defaultValue = "0") int pageNum,
                                        @RequestParam(required = false, defaultValue = "10") int pageSize) {
        return userService.pageList(pageNum, pageSize);
    }
}
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
  1. 统一返回实体 common ReturnT
public class ReturnT<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private int code;
    private String message;
    private T data;

    private ReturnT(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }


    public static <T> ReturnT<T> success(T data) {
        return new ReturnT<>(0, "success", data);
    }

    public static <T> ReturnT<T> success() {
        return success(null);
    }

    public static <T> ReturnT<T> error(int code, String message) {
        return new ReturnT<>(code, message, null);
    }

    public static <T> ReturnT<T> error(String message) {
        return error(-1, message);
    }

    public static <T> ReturnT<T> error() {
        return error("error");
    }

}
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
  1. 配置
  • Mapper接口扫描
@MapperScan("com.biubiu.multidatabase.mapper")
@Configuration
public class MyBatisConfig {
}
1
2
3
4
  • 数据源配置
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/biubiu?useUnicode=true&characterEncoding=UTF-8&useSSL=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource

mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:mapper/*.xml
1
2
3
4
5
6
7
8
9
10
11
12
  • 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">
  <parent>
    <artifactId>springboot-demo</artifactId>
    <groupId>com.biubiu</groupId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <artifactId>springboot-multidatabase-demo</artifactId>


  <properties>
    <java.version>1.8</java.version>
    <mybatis.version>2.2.0</mybatis.version>
    <fastjson.version>1.2.76</fastjson.version>
    <druid.version>1.2.6</druid.version>
  </properties>


  <dependencies>
    <!--web启动器-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 添加AOP坐标 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <!--MyBatis框架-->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <!--MySQL驱动-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <!--Druid数据源-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid-spring-boot-starter</artifactId>
      <version>${druid.version}</version>
    </dependency>
    <!--lombok工具-->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>
    <!--fastjson-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>${fastjson.version}</version>
    </dependency>
  </dependencies>

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

</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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  1. 测试没问题 4JdUCF.png