# 1. 项目概述
项目是基于SpringBoot+Vue前后端分离的仓库管理系统
后端:SpringBoot + MybatisPlus
前端:Node.js + Vue + element-ui
数据库:mysql
# 2. 创建后端项目
## 2.1 创建模块
1. 创建项目模块
2. 导入项目依赖
pom.xml
```xml
<?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.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wms</groupId>
<artifactId>Warehouse-System</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Warehouse-System</name>
<description>Warehouse management system</description>
<properties>
<java.version>11</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--mybatisPlus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!--代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>spring-boot-starter-swagger</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
```
## 2.2 加入MybatisPlus支持
1. 导入依赖
```xml
<!--mybatisPlus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
```
2. 创建并连接数据库
![QQ截图20230102150422](https://img.yiqiangshiyia.cn/blog/QQ%E6%88%AA%E5%9B%BE20230102150422.png)
3. 配置端口和数据源
application.yml
```yml
server:
port: 8002
spring:
datasource:
url: jdbc:mysql://localhost:3306/wms?useUnicode=true&characterEncoding=utf-8&serveTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
```
4. 编写实体类
```java
@Data
public class User {
private int id;
private String no;
private String name;
private String password;
private int sex;
private int roleId;
private String phone;
private String isvalid;
}
```
5. 编写Mapper接口
```java
@Mapper
public interface UserMapper extends BaseMapper<User> {
public List<User> selectAll();
}
```
6. 编写Service接口
```java
public interface UserService extends IService<User> {
public List<User> selectAll();
}
```
7. 编写Service实现类
```java
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Autowired
private UserMapper userMapper;
public List<User> selectAll(){
return userMapper.selectAll();
}
}
```
8. 编写配置文件
UserMapper.xml
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wms.mapper.UserMapper">
<select id="selectAll" resultType="com.wms.entity.User">
select * from user
</select>
</mapper>
```
9. 编写测试代码
```java
@RestController
public class testController {
@Autowired
private UserService userService;
@GetMapping
public List<User> test(){
return userService.selectAll();
}
}
```
10. 测试结果
![QQ截图20230102161434](https://img.yiqiangshiyia.cn/blog/QQ%E6%88%AA%E5%9B%BE20230102161434.png)
# 3. 代码生成器
> 简化开发:删除之前编写的实体类、接口、实现类、配置文件以及测试类,利用MyBatisPlus代码生成器自动生成代码
1. 导入依赖
> 注意:MybatisPlus版本用3.4.1,3.5版本的MybatisPlus会报错!
```xml
<!--代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
```
2. 编写代码生成器
> 参考MybatisPlus官网:https://baomidou.com/pages/d357af/
```java
package com.wms.common;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CodeGenerator {
/**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("linsuwen");
gc.setOpen(false);
gc.setSwagger2(true); //实体属性 Swagger2 注解
gc.setBaseResultMap(true); // XML ResultMap
gc.setBaseColumnList(true); // XML columList
//去掉service接口首字母的I, 如DO为User则叫UserService