问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Spring Boot实战教程:从入门到项目搭建

创作时间:
作者:
@小白创作中心

Spring Boot实战教程:从入门到项目搭建

引用
CSDN
1.
https://blog.csdn.net/android157/article/details/139355902

随着技术的快速发展和数字化转型的深入推进,软件开发领域迎来了前所未有的变革。在众多开发框架中,Spring Boot凭借其"约定大于配置"的核心理念和快速开发的能力,迅速崭露头角,成为当今企业级应用开发的首选框架之一。本文将为广大开发者提供一本系统、全面且实用的学习指南,深入解析Spring Boot的核心特性和最佳实践,并通过大量的实战案例,帮助读者快速掌握Spring Boot的应用开发技巧,从而能够高效、稳定地构建出符合业务需求的Web应用。

一、创建Springboot项目

  1. 创建Maven工程
  2. 导入spring-boot-stater-web起步依赖
  3. 编写Controller
  4. 提供启动类

二、手动创建SpringBoot工程


三、编写配置文件application.properties

删除application.properties配置文件,新建application.yml或application.yaml配置文件【两者区别请自行查询】

四、编写Controller

启动项目后在控制台会显示配置的端口

可以根据需要将pom文件中的jdk17改为jdk8【注意mybatis等三方依赖库的版本也需要降低】

五、提供启动类

六、启动服务,在浏览器调用http://localhost:8080/hello

页面返回Hello World~表示调用成功,项目搭建正常

------------接下来就可以进行业务相关接口开发了------------

七、执行sql语句【在navicat、idea或者dos窗口执行sql语句】

-- 创建数据库
create database big_event;
-- 使用数据库
use big_event;
-- 用户表
create table user (
                      id int unsigned primary key auto_increment comment 'ID',
                      username varchar(20) not null unique comment '用户名',
                      password varchar(32)  comment '密码',
                      nickname varchar(10)  default '' comment '昵称',
                      email varchar(128) default '' comment '邮箱',
                      user_pic varchar(128) default '' comment '头像',
                      create_time datetime not null comment '创建时间',
                      update_time datetime not null comment '修改时间'
) comment '用户表';
-- 分类表
create table category(
                         id int unsigned primary key auto_increment comment 'ID',
                         category_name varchar(32) not null comment '分类名称',
                         category_alias varchar(32) not null comment '分类别名',
                         create_user int unsigned not null comment '创建人ID',
                         create_time datetime not null comment '创建时间',
                         update_time datetime not null comment '修改时间',
                         constraint fk_category_user foreign key (create_user) references user(id) -- 外键约束
);
-- 文章表
create table article(
                        id int unsigned primary key auto_increment comment 'ID',
                        title varchar(30) not null comment '文章标题',
                        content varchar(10000) not null comment '文章内容',
                        cover_img varchar(128) not null  comment '文章封面',
                        state varchar(3) default '草稿' comment '文章状态: 只能是[已发布] 或者 [草稿]',
                        category_id int unsigned comment '文章分类ID',
                        create_user int unsigned not null comment '创建人ID',
                        create_time datetime not null comment '创建时间',
                        update_time datetime not null comment '修改时间',
                        constraint fk_article_category foreign key (category_id) references category(id),-- 外键约束
                        constraint fk_article_user foreign key (create_user) references user(id) -- 外键约束
)

八、整合mysql

九、整合mybatis

推荐使用Mybatis-plus,而且建议只使用Mybatis-plus的Mapper规范,Service依旧按照Mybatis的规范【具体实施参照本文第三十二项】。

mybatis中文网:http://www.mybatis.cn/

mybatis的mapper.xml映射文件存放位置说明:
https://www.jb51.net/program/30707796h.htm

十、配置application.yml文件

server:
  port: 8080
  servlet:
    context-path: /big-event2
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/big_event?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true
    username: root
    password: 123456
  redis:
    host: localhost
    port: 6379
#    下划线命名字段和驼峰命名属性的赋值问题【https://blog.csdn.net/Zong_0915/article/details/127453933】
mybatis:
  configuration:
    map-underscore-to-camel-case: true

十一、在包名下新建业务分类包

十二、通用实体类

返回结果实体类:

package com.source.bigevent2.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
   
    private Integer code;//状态码0成功;1失败
    private String msg;//提示信息
    private T data;//响应数据
    public static Result success() {
   
        return new Result(0, "操作成功", null);
    }
    public static <E> Result<E> success(E data) {
   
        return new Result<>(0, "操作成功", data);
    }
    public static Result<String> error(String msg) {
   
        return new Result<>(1, msg, null);
    }
}

分页返回结果实体类:

package com.source.bigevent2.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
//分页返回结果对象
@Data
© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号
Spring Boot实战教程:从入门到项目搭建