系列文章:
SpringBoot + Vue前后端分離項目實戰(zhàn) || 一:Vue前端設(shè)計
SpringBoot + Vue前后端分離項目實戰(zhàn) || 二:Spring Boot后端與數(shù)據(jù)庫連接
SpringBoot + Vue前后端分離項目實戰(zhàn) || 三:Spring Boot后端與Vue前端連接
SpringBoot + Vue前后端分離項目實戰(zhàn) || 四:用戶管理功能實現(xiàn)
SpringBoot + Vue前后端分離項目實戰(zhàn) || 五:用戶管理功能后續(xù)
B站視頻講解:2023全網(wǎng)最簡單但實用的SpringBoot+Vue前后端分離項目實戰(zhàn)
不想看視頻可瀏覽此文章筆記,比較詳細
新建Spring后臺項目
IDEA file->new->project
選擇 Spring Initializr
此處不加依賴,直接FINISH
添加依賴
首先確認本地maven
已經(jīng)配置好,點擊File -> Settings
查看Maven
找到pom.xml
文件
直接復(fù)制此處需要的依賴
<?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.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ums</groupId>
<artifactId>x-admin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>x-admin</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
<!-- freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
遇到爆紅,點擊右側(cè)Maven
中刷新
按鈕,等待IDEA下載依賴即可,多刷新幾次,等待
新建數(shù)據(jù)庫
用navicat
鏈接本地數(shù)據(jù)庫,新建一個數(shù)據(jù)庫xdb
新建一個 123.txt
文檔,將以下SQL命令復(fù)制進去后,改名為123.sql
CREATE TABLE `x_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(100) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`phone` varchar(20) DEFAULT NULL,
`status` int(1) DEFAULT NULL,
`avatar` varchar(200) DEFAULT NULL,
`deleted` INT(1) DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
insert into `x_user` (`id`, `username`, `password`, `email`, `phone`, `status`, `avatar`, `deleted`) values('1','admin','123456','super@aliyun.com','18677778888','1','https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif','0');
insert into `x_user` (`id`, `username`, `password`, `email`, `phone`, `status`, `avatar`, `deleted`) values('2','zhangsan','123456','zhangsan@gmail.com','13966667777','1','https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif','0');
insert into `x_user` (`id`, `username`, `password`, `email`, `phone`, `status`, `avatar`, `deleted`) values('3','lisi','123456','lisi@gmail.com','13966667778','1','https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif','0');
insert into `x_user` (`id`, `username`, `password`, `email`, `phone`, `status`, `avatar`, `deleted`) values('4','wangwu','123456','wangwu@gmail.com','13966667772','1','https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif','0');
insert into `x_user` (`id`, `username`, `password`, `email`, `phone`, `status`, `avatar`, `deleted`) values('5','zhaoer','123456','zhaoer@gmail.com','13966667776','1','https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif','0');
insert into `x_user` (`id`, `username`, `password`, `email`, `phone`, `status`, `avatar`, `deleted`) values('6','songliu','123456','songliu@gmail.com','13966667771','1','https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif','0');
CREATE TABLE `x_role` (
`role_id` int(11) NOT NULL AUTO_INCREMENT,
`role_name` varchar(50) DEFAULT NULL,
`role_desc` varchar(100) DEFAULT NULL,
PRIMARY KEY (`role_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
insert into `x_role` (`role_id`, `role_name`, `role_desc`) values('1','admin','超級管理員');
insert into `x_role` (`role_id`, `role_name`, `role_desc`) values('2','hr','人事專員');
insert into `x_role` (`role_id`, `role_name`, `role_desc`) values('3','normal','普通員工');
CREATE TABLE `x_menu` (
`menu_id` int(11) NOT NULL AUTO_INCREMENT,
`component` varchar(100) DEFAULT NULL,
`path` varchar(100) DEFAULT NULL,
`redirect` varchar(100) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`title` varchar(100) DEFAULT NULL,
`icon` varchar(100) DEFAULT NULL,
`parent_id` int(11) DEFAULT NULL,
`is_leaf` varchar(1) DEFAULT NULL,
`hidden` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`menu_id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4;
insert into `x_menu`(`menu_id`,`component`,`path`,`redirect`,`name`,`title`,`icon`,`parent_id`,`is_leaf`,`hidden`) values (1,'Layout','/user','/user/list','userManage','用戶管理','userManage',0,'N',0),(2,'user/user','list',NULL,'userList','用戶列表','userList',1,'Y',0),(3,'user/role','role',NULL,'roleList','角色列表','role',1,'Y',0),(4,'user/permission','permission',NULL,'permissionList','權(quán)限列表','permission',1,'Y',0);
CREATE TABLE `x_user_role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`role_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;
insert into `x_user_role` (`id`, `user_id`, `role_id`) values('1','1','1');
CREATE TABLE `x_role_menu` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`role_id` int(11) DEFAULT NULL,
`menu_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;
右擊xdb
選擇運行SQL文件
找到123.sql
文件后運行
運行成功
IDEA 連接數(shù)據(jù)庫
打開src\main\resources\application.yml
,如果沒有該文件,只有application.property
文件,則直接改其后綴為.yml
寫入代碼
server:
port: 9999 # 端口號
spring:
datasource:
username: root # 數(shù)據(jù)庫名
password: 1234 # 數(shù)據(jù)庫密碼
url: jdbc:mysql:///xdb
redis:
port: 6379
host: localhost
logging:
level:
com.ums: debug
IDEA 自動創(chuàng)建類實體
先新建一個package:sys
,空包,生成的代碼會放在此處
用Mybatis-plus
來自動創(chuàng)建類實體的java代碼
在src\test
下新建一個java類:CodeGenerator
然后寫上如下代碼,代碼中有解釋
package com.ums;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.Collections;
public class CodeGenerator {
public static void main(String[] args) {
String url = "jdbc:mysql:///xdb"; // 與配置文件 一致
String username = "root";
String password = "1234";
String author = "anthony";
String moduleName = "sys"; // 系統(tǒng)管理的代碼包
String mapperLocation = "D:\\VueProj\\x-admin\\src\\main\\resources\\mapper\\" + moduleName ;
String tables = "x_menu,x_role,x_role_menu,x_user,x_user_role"; // 與數(shù)據(jù)庫中的表名一致,逗號隔開
FastAutoGenerator.create(url, username, password)
.globalConfig(builder -> {
builder.author(author) // 設(shè)置作者
// .enableSwagger() // 開啟 swagger 模式
// .fileOverride() // 覆蓋已生成文件
.outputDir("D:\\VueProj\\x-admin\\src\\main\\java"); // 指定輸出目錄
})
.packageConfig(builder -> {
builder.parent("com.ums") // 設(shè)置父包名
.moduleName(moduleName) // 設(shè)置父包模塊名
.pathInfo(Collections.singletonMap(OutputFile.xml, mapperLocation)); // 設(shè)置mapperXml生成路徑
})
.strategyConfig(builder -> {
builder.addInclude(tables) // 設(shè)置需要生成的表名
.addTablePrefix("x_"); // 設(shè)置過濾表前綴, x_menu 生成的類實體無 x_ 前綴
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默認的是Velocity引擎模板
.execute();
}
}
點擊run
生成代碼
定義數(shù)據(jù)傳遞至前端的格式
上一節(jié)說過,需要記錄下前端登錄的響應(yīng)數(shù)據(jù)格式
現(xiàn)在,后端來構(gòu)造此格式
{"code":20000,"data":{"token":"admin-token"}}
在src\main
下新建一個package: common
然后新建package:vo
再新建一個java類Result
寫入代碼,有注釋解釋文章來源:http://www.zghlxwxcb.cn/news/detail-512472.html
package com.ums.common.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
private Integer code; // 成功失敗的代碼,此處定義2000為成功,2001為失敗
private String message; // 消息,里面包含數(shù)據(jù)data
private T data; // 未定義的數(shù)據(jù)形式
// 此處重載了四個 success 方法,有些能夠返回數(shù)據(jù),有的只返回代碼或信息
public static <T> Result<T> success() {
return new Result<>(20000,"success",null);
}
public static <T> Result<T> success(T data) {
return new Result<>(20000,"success",data);
}
public static <T> Result<T> success(T data, String message) {
return new Result<>(20000,message,data);
}
public static <T> Result<T> success(String message) {
return new Result<>(20000,message,null);
}
public static<T> Result<T> fail(){
return new Result<>(20001,"fail",null);
}
public static<T> Result<T> fail(Integer code){
return new Result<>(code,"fail",null);
}
public static<T> Result<T> fail(Integer code, String message){
return new Result<>(code,message,null);
}
public static<T> Result<T> fail( String message){
return new Result<>(20001,message,null);
}
}
測試文章來源地址http://www.zghlxwxcb.cn/news/detail-512472.html
- 先在主程序
src\main\java
中的XAdminApplication
中加入注解@MapperScan("com.ums.*.mapper")
- 進入
UserController
寫測試代碼package com.ums.sys.controller; import com.ums.common.vo.Result; import com.ums.sys.entity.User; import com.ums.sys.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * <p> * 前端控制器 * </p> * * @author anthony * @since 2023-06-16 */ @RestController @RequestMapping("/user") public class UserController { @Autowired private IUserService userService; @GetMapping("/all") public Result<List<User>> getAllUser() { List<User> list = userService.list(); return Result.success(list,"查詢成功"); } }
- 運行主程序
進入瀏覽器輸入http://localhost:9999/user/all
顯示數(shù)據(jù)則成功
此處數(shù)據(jù)格式也與前端對接得上
到了這里,關(guān)于SpringBoot + Vue前后端分離項目實戰(zhàn) || 二:Spring Boot后端與數(shù)據(jù)庫連接的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!