从零开始创建一个项目,springBoot+mybatisPlus+mysql+swagger+maven

本文详细介绍了如何从零开始创建一个项目,包括配置SpringBoot、mybatisPlus、MySQL等技术栈,以及数据库表结构设计、依赖管理、类生成和注解应用等内容。

一,前提

从零开始创建一个项目,绑定了数据库

用到的技术栈:springBoot+mybatisPlus+mysql+swagger+maven

二,创建项目步骤

1,创建项目

在这里插入图片描述

在这里插入图片描述

创建出来的项目结构如图所示

在这里插入图片描述

2,修改配置文件

因为我比较习惯yml语言,这里我就使用yml

在这里插入图片描述

application.yml配置如下:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: ****
    driver-class-name: com.mysql.jdbc.Driver
  thymeleaf:
    prefix: classpath:/template/  #所有的动态页面打包后的位置
    suffix: .html  #添加后缀

# mybatis-plus相关配置
mybatis-plus:
  # xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
  mapper-locations: classpath:mapper/*.xml
  # 以下配置均有默认值,可以不设置
  global-config:
    db-config:
      #主键类型 AUTO:"数据库ID自增" INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
      id-type: auto
      #字段策略 IGNORED:"忽略判断"  NOT_NULL:"非 NULL 判断")  NOT_EMPTY:"非空判断"
      field-strategy: NOT_EMPTY
      #数据库类型
      db-type: MYSQL
  configuration:
    # 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
    map-underscore-to-camel-case: true
    # 如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
    call-setters-on-nulls: true
    # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl



3,引入相关依赖

在这个环节中,我多次启动失败都是因为版本太高,所以下面的依赖是我调整完之后,
可以正常启动的依赖配置。

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.1</version>
        <relativePath/>
         <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>projectA</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>projectA</name>
    <description>projectA</description>
    <!-- jdk配置信息,最好加上,因为如果不加上,每次我启动一次项目,项目的jdk版本就变成19,
    然后一直报错,这个地方永久配置jdk-->
    <properties>
        <java.version>17</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- mybatisPlus 核心库 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- druid依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.0</version>
        </dependency>
        <!-- swagger依赖 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
         <!-- mysql依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
		<!-- lombok依赖 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.2</version>
        </dependency>
    </dependencies>

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

</project>

4,准备表数据

这里我准备了四张表


CREATE TABLE `student` (
  `student_no` varchar(3) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '学号',
  `name` varchar(8) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '姓名',
  `sex` varchar(2) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '性别',
  `birthday` date DEFAULT NULL COMMENT '出生日期',
  `stu_class` varchar(5) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '班级',
  PRIMARY KEY (`student_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

INSERT INTO test.student
(student_no, name, sex, birthday, stu_class)
VALUES('101', '李军', '男', '1976-02-20', '95033');
INSERT INTO test.student
(student_no, name, sex, birthday, stu_class)
VALUES('103', '陆君', '男', '1974-06-03', '95031');
INSERT INTO test.student
(student_no, name, sex, birthday, stu_class)
VALUES('105', '匡明', '男', '1975-10-02', '95031');
INSERT INTO test.student
(student_no, name, sex, birthday, stu_class)
VALUES('107', '王丽', '女', '1976-01-23', '95033');
INSERT INTO test.student
(student_no, name, sex, birthday, stu_class)
VALUES('108', '曾华', '男', '1977-09-01', '95033');
INSERT INTO test.student
(student_no, name, sex, birthday, stu_class)
VALUES('109', '王芳', '女', '1975-02-10', '95031');

CREATE TABLE `score` (
  `student_no` varchar(3) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '学号',
  `course_no` varchar(5) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '课程编号',
  `score` decimal(4,1) DEFAULT NULL COMMENT '分数',
  PRIMARY KEY (`student_no`,`course_no`),
  KEY `Cno` (`course_no`),
  CONSTRAINT `Score_ibfk_1` FOREIGN KEY (`student_no`) REFERENCES `student` (`student_no`),
  CONSTRAINT `Score_ibfk_2` FOREIGN KEY (`course_no`) REFERENCES `course` (`course_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

INSERT INTO test.score
(student_no, course_no, score)
VALUES('101', '3-105', 64.0);
INSERT INTO test.score
(student_no, course_no, score)
VALUES('101', '6-166', 85.0);
INSERT INTO test.score
(student_no, course_no, score)
VALUES('103', '3-105', 92.0);
INSERT INTO test.score
(student_no, course_no, score)
VALUES('103', '3-245', 86.0);
INSERT INTO test.score
(student_no, course_no, score)
VALUES('105', '3-105', 88.0);
INSERT INTO test.score
(student_no, course_no, score)
VALUES('105', '3-245', 75.0);
INSERT INTO test.score
(student_no, course_no, score)
VALUES('107', '3-105', 91.0);
INSERT INTO test.score
(student_no, course_no, score)
VALUES('107', '6-166', 79.0);
INSERT INTO test.score
(student_no, course_no, score)
VALUES('108', '3-105', 78.0);
INSERT INTO test.score
(student_no, course_no, score)
VALUES('108', '6-166', 81.0);
INSERT INTO test.score
(student_no, course_no, score)
VALUES('109', '3-105', 76.0);
INSERT INTO test.score
(student_no, course_no, score)
VALUES('109', '3-245', 68.0);

CREATE TABLE `student` (
  `student_no` varchar(3) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '学号',
  `name` varchar(8) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '姓名',
  `sex` varchar(2) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '性别',
  `birthday` date DEFAULT NULL COMMENT '出生日期',
  `stu_class` varchar(5) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '班级',
  PRIMARY KEY (`student_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

INSERT INTO test.student
(student_no, name, sex, birthday, stu_class)
VALUES('101', '李军', '男', '1976-02-20', '95033');
INSERT INTO test.student
(student_no, name, sex, birthday, stu_class)
VALUES('103', '陆君', '男', '1974-06-03', '95031');
INSERT INTO test.student
(student_no, name, sex, birthday, stu_class)
VALUES('105', '匡明', '男', '1975-10-02', '95031');
INSERT INTO test.student
(student_no, name, sex, birthday, stu_class)
VALUES('107', '王丽', '女', '1976-01-23', '95033');
INSERT INTO test.student
(student_no, name, sex, birthday, stu_class)
VALUES('108', '曾华', '男', '1977-09-01', '95033');
INSERT INTO test.student
(student_no, name, sex, birthday, stu_class)
VALUES('109', '王芳', '女', '1975-02-10', '95031');
CREATE TABLE `teacher` (
  `teacher_no` varchar(3) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `name` varchar(4) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `sex` varchar(2) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `birthday` date DEFAULT NULL,
  `title` varchar(6) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `depart` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  PRIMARY KEY (`teacher_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

INSERT INTO test.teacher
(teacher_no, name, sex, birthday, title, depart)
VALUES('804', '李诚', '男', '1958-12-02', '副教授', '计算机系');
INSERT INTO test.teacher
(teacher_no, name, sex, birthday, title, depart)
VALUES('825', '王萍', '女', '1972-05-05', '助教', '计算机系');
INSERT INTO test.teacher
(teacher_no, name, sex, birthday, title, depart)
VALUES('831', '刘冰', '女', '1977-08-14', '助教', '电子工程系');
INSERT INTO test.teacher
(teacher_no, name, sex, birthday, title, depart)
VALUES('856', '张旭', '男', '1969-03-12', '讲师', '电子工程系');

5,创建相关类

这里我推荐一个好用的插件mybatisX,可以自动创建相关类,很省事

在这里插入图片描述

先连接好数据库,刷新表

在这里插入图片描述

配置生成的类基本信息

在这里插入图片描述
在这里插入图片描述

都给我生成好了

在这里插入图片描述

6,相关注解添加

启动类处理一下


@SpringBootApplication
@MapperScan(basePackages = "com.example.mapper")
public class ProjectApplication {
    public static void main(String[] args) {
        try {
            SpringApplication.run(ProjectApplication.class, args);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

其他注解添加

在这里插入图片描述
在这里插入图片描述

7,项目可以安全启动了

在这里插入图片描述

8,其他文章

永久解决 Intellij idea 报错:Error : java 不支持发行版本5

Maven [ERROR] 不再支持源选项 5,请使用 7 或更高版本的解决办法

SpringBoot教程(十六) | SpringBoot集成swagger(全网最全)

SpringBoot集成swagger2报错‘apiDocumentationScanner‘ defined in URL

SpringBoot 整合MyBatisPlus

Error creating bean with name ‘**ServiceImpl‘: Unsatisfied dependency expressed through field ‘baseM

IDEA设置自动导入包

下班咯,还待完善,敬请期待。。。

<think>嗯,用户明确提出了想从零开始学习SpringBoot+Vue全栈开发的需求,而且特别强调了“前后端分离”这个关键词。从之前的对话历史看,用户已经接触过一些相关资料引用(虽然系统提示那些不是真实需求),但能感觉到用户是个刚入行的开发者,可能对复杂的项目搭建有畏难情绪。用户可能真正需要的是:一个能降低学习门槛的路线图,最好有明确的学习步骤和资源推荐。ta提到的“从零开始”暗示需要基础指导,而“全栈开发教程”说明希望有系统性材料。我注意到四点关键信息:1)用户是初学者(引用中多次提到刚入行小伙伴困难)2)需要整合前后端知识3)需要项目实战导向4)可能需要规避常见坑点。因此回复要避免纯理论,重点放在“如何做”。从技术角度,SpringBoot+Vue全栈涉及几个核心模块:后端框架、前端框架、通信协议、工程化部署。每个模块都要给出具体实施建议。比如SpringBoot的依赖注入这块对新手容易困惑,应该提醒重点学习@Autowired;而Vue的axios封装又是前后端联调的关键点。资源推荐需要分层:快速入门文档(如Vue官方指南)、系统教程(王松的实战书)、持续学习途径(尚硅谷视频)。特别要加入SpringBoot的版本选择建议,因为3.x版本和2.x的配置差异可能导致新手卡住。项目实战示例采用电商管理后台是因为:1)功能模块覆盖全面(权限/商品管理必备)2)社区资源丰富3)能体现CRUD和状态管理典型场景。特意避开太复杂的支付或即时通讯功能。最后的避坑指南是新手的刚需,比如CORS和路由冲突都是实际开发中高频问题。部署部分强调容器化因为现在企业主流用Docker部署SpringBoot,而Vue的Nginx配置是前后端分离标配。补充学习建议时想到用户可能有时间焦虑,所以强调里程碑划分,两周一个周期比较合理。调试工具推荐Chrome插件是因为图形化界面比命令行更友好。额外提供的思维导图能帮用户建立知识图谱。</think> 以下是针对**SpringBoot+Vue全栈开发**的完整学习路径与实战指南,结合前后端分离核心要点,帮助你高效从零搭建项目👇 --- ### **一、基础环境准备** 1. **后端环境** - JDK 17(SpringBoot 3.x要求JDK 17+) - Maven 3.8+ 或 Gradle 8.x - IDE:IntelliJ IDEA(推荐)或 VS Code 2. **前端环境** - Node.js 18.x + npm 9.x - Vue CLI 5.x:`npm install -g @vue/cli` - IDE:VS Code + Volar插件 --- ### **二、后端开发(SpringBoot)** #### **核心步骤** ```java // 1. 创建SpringBoot项目Spring Initializr) 依赖选择:Spring Web, Spring Data JPA, MySQL Driver, Lombok // 2. 配置文件:application.yml server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/db_name?useSSL=false username: root password: 123456 jpa: hibernate: ddl-auto: update // 3. 实体类示例(JPA) @Entity @Data // Lombok注解 public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; private String email; } ``` #### **关键技能点** 1. RESTful API设计:`@RestController`, `@GetMapping`, `@PostMapping` 2. 跨域配置:`@CrossOrigin` 或全局配置类 3. 统一响应封装:`ResponseEntity<T>` 或自定义Result类 4. 分页查询:`Pageable` + `PageRequest` --- ### **三、前端开发(Vue3 + TypeScript)** #### **项目初始化** ```bash vue create frontend # 选择:Vue3, TypeScript, Vue Router, Pinia, ESLint cd frontend npm install axios pinia-plugin-persist ``` #### **核心目录结构** ``` src/ ├── api/ # 接口封装 ├── store/ # Pinia状态管理 ├── router/ # 路由配置 ├── views/ # 页面组件 └── utils/ # 工具类(如request.ts) ``` #### **关键代码示例** ```typescript // 1. 封装Axios请求 (utils/request.ts) const service = axios.create({ baseURL: "http://localhost:8080/api", timeout: 5000 }); // 2. Pinia状态管理 export const useUserStore = defineStore('user', { state: () => ({ token: localStorage.getItem('token') || '' }), actions: { async login(username: string, password: string) { const res = await service.post('/login', {username, password}); this.token = res.data.token; } } }); // 3. 路由守卫拦截(router/index.ts) router.beforeEach((to) => { const store = useUserStore(); if (to.meta.requiresAuth && !store.token) return '/login'; }); ``` --- ### **四、前后端联调关键点** 1. **接口对接** - 使用Swagger生成API文档:`springdoc-openapi` - Postman测试后端接口 2. **跨域问题** 后端配置(全局): ```java @Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("http://localhost:8081") // Vue端口 .allowedMethods("*"); } } ``` 3. **状态保持** - JWT鉴权:后端生成token,前端存localStorage - Axios拦截器自动携带token: ```typescript service.interceptors.request.use(config => { config.headers.Authorization = `Bearer ${store.token}`; return config; }); ``` --- ### **五、项目部署方案** 1. **后端** - 打包:`mvn clean package` → 生成`target/*.jar` - 运行:`java -jar your-app.jar --spring.profiles.active=prod` 2. **前端** - 打包:`npm run build` → 生成`dist`目录 - Nginx配置: ```nginx server { listen 80; location / { root /path/to/dist; index index.html; try_files $uri $uri/ /index.html; # 支持Vue路由history模式 } location /api { proxy_pass http://localhost:8080; # 反向代理到后端 } } ``` --- ### **六、推荐学习资源** 1. **系统教程** - 书籍:《Spring Boot+Vue全栈开发实战》(王松著)[^1] - 视频:[尚硅谷SpringBoot2+Vue3整合实战](https://www.bilibili.com/video/BV1U44y1W77D) 2. **开源项目参考** - GitHub:[elunez/eladmin](https://github.com/elunez/eladmin)(权限管理系统) - Gitee:[SpringBoot-Vue-Quick](https://gitee.com/smallc/SpringBoot-Vue-Quick) 3. **脚手架工具** - [Spring Initializr](https://start.spring.io/) + [Vite](https://vitejs.dev/)(现代前端构建工具) --- ### **避坑指南** 1. 前后端分离session共享问题 → 用JWT替代Session 2. Vue路由刷新404 → Nginx配置`try_files` 3. 生产环境数据库配置 → 使用`application-prod.yml`隔离配置 > **学习建议**:从单表CRUD开始(如用户管理),逐步增加权限控制、文件上传、第三方登录等模块[^2][^4]。遇到问题时优先查阅[Spring官方文档](https://spring.io/)和[Vue官方文档](https://vuejs.org/)。 --- **附:技术栈全景图** ```mermaid graph LR A[前端] --> B[Vue3+TS] A --> C[Pinia状态管理] A --> D[Axios请求库] E[后端] --> F[SpringBoot3] E --> G[Spring Data JPA] E --> H[Spring Security] E --> I[MySQL/Redis] ``` --- ### 相关问题 1. SpringBoot如何整合Redis实现缓存功能? 2. Vue3中Pinia与Vuex的区别及适用场景? 3. 前后端分离项目如何解决跨域问题? 4. JWT认证在SpringBoot中的具体实现步骤? 5. 如何优化SpringBoot+Vue项目的首次加载速度?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值