Spring Boot实战教程:从入门到项目搭建
创作时间:
作者:
@小白创作中心
Spring Boot实战教程:从入门到项目搭建
引用
CSDN
1.
https://blog.csdn.net/android157/article/details/139355902
随着技术的快速发展和数字化转型的深入推进,软件开发领域迎来了前所未有的变革。在众多开发框架中,Spring Boot凭借其"约定大于配置"的核心理念和快速开发的能力,迅速崭露头角,成为当今企业级应用开发的首选框架之一。本文将为广大开发者提供一本系统、全面且实用的学习指南,深入解析Spring Boot的核心特性和最佳实践,并通过大量的实战案例,帮助读者快速掌握Spring Boot的应用开发技巧,从而能够高效、稳定地构建出符合业务需求的Web应用。
一、创建Springboot项目
- 创建Maven工程
- 导入spring-boot-stater-web起步依赖
- 编写Controller
- 提供启动类
二、手动创建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
热门推荐
早期肝病蜘蛛痣一般长在哪里
武汉一日游攻略:经典路线及景点详解
选题策划:从理论到实践的全方位指南
冠状病毒症状中的头痛该如何应对
完善WPS文档的大纲与结构
食品添加剂对食品安全和营养价值的影响研究
云南高原特色现代农业:从“云品出山”到“云品出海”
荆州古城墙:建筑技艺与历史文化的完美融合
中国数字文化中“二”的含义
家庭医生推荐的常备药物 您备齐了吗
如何了解装修市场的流行趋势?这种流行趋势如何影响家居装饰风格?
吉他拾音器安装全攻略:从准备到测试的详细指南
如何提高商铺的出租效益?这种效益的提升有哪些方法?
HTML文件如何导入公众号
如何利用按分钟显示技术提升数据分析效率?
县委书记向躺平式干部宣战:为实干者撑腰,为担当者鼓劲
年末大扫除,这些清洁剂使用需注意!
本周短剧爆款18招:亲情剧拼催泪,复仇剧比暗黑,逆袭萌娃显神通
如何了解一个城市的宜居情况?了解后如何做出居住决策?
消失”的日本人形机器人
一到夏天就发作,汗疱疹该怎么办?
《红楼梦》里的绝世美食,终于有本书说清楚了
C反应蛋白:炎症的敏感指标与健康管理的桥梁
未成年人琐事起冲突,法官“调解+教育”护成长
曹丕篡汉与其对后世的深远影响
学针灸要多长时间
股票至少多少股起购?一文详解股票交易基本规则
兔子科学喂养指南:牧草、兔粮、饮水全解析
打野猪妹出装天赋加点攻略:英雄联盟打野猪妹的最佳装备、天赋和加点选择攻略
使用OpenCV进行检测、跟踪移动物体