Vue前端+SpringBoot后端基础框架搭建及接⼝调⽤基本流程⽬录
准备⼯作
IDE:idea, vscode
数据库:mysql5.7, mysqlworkbench
前端:vue, element ui, axios
后端:springboot, mybatis
⼀、数据库创建
由于暂时实现简单的接⼝调⽤,因此只建⽴了⼀个表t_user:
user_id user_name user_password phone_number 1admin12345610086
2test12345610010⼆、SpringBoot后端搭建
springboot项⽬的构建在这⾥就不赘述了,构建的时候记得选上spring web依赖(没有选也没关系,后⾯再导⼊是⼀样的)。
项⽬⽬录结构:
⼤致的构建顺序:controller->service->serviceImpl->dao->mapper
1. 导⼊依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"xsi="/2001/XMLSchema-instance"
schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId&le</groupId>
<artifactId>airportserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>airportserver</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--        mybatis依赖,⼿动添加-->
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!--        mysql驱动,⼿动添加--->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--        fastjson,⼿动添加--->
<!-- mvnrepository/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass&le.airportserver.AirportserverApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2. 配置项
spring:
datasource:
url: jdbc:mysql://localhost:3306/testdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8 username: root
password:123456
mybatis:
mapper-Locations: classpath:/mapper/*.xml
server:
port:8088
3. 实体类构建
User.java
public class User {
private int id;
private String username;
private String password;
private String phoneNum;
}
构造⽅法和setter / getter已省去,若需要请⾃动补全。
4. Controller层定义
UserController.java
@RestController
@RequestMapping("/login")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/getUser")
public JSONObject getUser(@RequestBody JSONObject requestJson){
User(requestJson);
}
}
5. Service层及其实现
UserService.java
public interface UserService {
JSONObject getUser(JSONObject requestJson);
}
UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public JSONObject getUser(JSONObject requestJson){
return CommonUtil.User(requestJson));
}
}
6. DAO层
UserDao.java
@Repository
public interface UserDao {
JSONObject getUser(JSONObject requestJson);
}
7. mapper
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-///DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="ample.airportserver.dao.UserDao">
<select id="getUser"resultType="com.alibaba.fastjson.JSONObject"parameterType="com.alibaba.fastjson.JSONObject">        SELECT
springboot结构user_id userId,
user_name username,
user_password password,
phone_number phoneNum
FROM t_user
WHERE user_name = #{username} AND user_password = #{password}
</select>
</mapper>
8. ⽤到的⼯具类
CommonUtil.java

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。