代码都将上传至git 仓库地址
转载请注明出处 https://blog.csdn.net/lioncatch/article/details/114395105
第一步
创建一个数据库加一张表(表注释和列注释都最好加上)
这里随便建一张表
/*
Navicat Premium Data Transfer
Source Server : adsfd
Source Server Type : MySQL
Source Server Version : 80019
Source Host : localhost:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 80019
File Encoding : 65001
Date: 05/03/2021 14:29:02
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for crop_image
-- ----------------------------
DROP TABLE IF EXISTS `crop_image`;
CREATE TABLE `crop_image` (
`id` int(0) NOT NULL AUTO_INCREMENT COMMENT '主键',
`crop_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '作物名称',
`crop_image_sort_id` int(0) NOT NULL COMMENT '作物分类id',
`crop_image_address` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '作物图片地址',
PRIMARY KEY (`id`) USING BTREE,
INDEX `secondConn`(`crop_image_sort_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1555 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of crop_image
-- ----------------------------
INSERT INTO `crop_image` VALUES (1, '黑麦', 1, '黑麦.jpg');
INSERT INTO `crop_image` VALUES (2, '小麦02', 1, '小麦02.jpg');
INSERT INTO `crop_image` VALUES (3, '小麦11', 1, '小麦11.jpg');
INSERT INTO `crop_image` VALUES (4, '小麦12', 1, '小麦12.jpg');
INSERT INTO `crop_image` VALUES (5, '小麦20', 1, '小麦20.jpg');
INSERT INTO `crop_image` VALUES (6, '小麦3', 1, '小麦3.jpg');
INSERT INTO `crop_image` VALUES (7, '小麦-968', 1, '小麦-968.jpg');
INSERT INTO `crop_image` VALUES (8, '小麦-96W1130', 1, '小麦-96W1130.jpg');
INSERT INTO `crop_image` VALUES (9, '小麦-98-5335-56', 1, '小麦-98-5335-56.jpg');
INSERT INTO `crop_image` VALUES (10, '小麦-COMPTON-1', 1, '小麦-COMPTON-1.jpg');
INSERT INTO `crop_image` VALUES (11, '小麦EA60103', 1, '小麦EA60103.jpg');
INSERT INTO `crop_image` VALUES (12, '小麦EA60104', 1, '小麦EA60104.jpg');
INSERT INTO `crop_image` VALUES (13, '小麦EA60109', 1, '小麦EA60109.jpg');
INSERT INTO `crop_image` VALUES (14, '小麦EA60110', 1, '小麦EA60110.jpg');
INSERT INTO `crop_image` VALUES (15, '小麦EA60113', 1, '小麦EA60113.jpg');
INSERT INTO `crop_image` VALUES (16, '小麦EA60122', 1, '小麦EA60122.jpg');
INSERT INTO `crop_image` VALUES (17, '小麦EA60123', 1, '小麦EA60123.jpg');
INSERT INTO `crop_image` VALUES (774, '杏-04', 50, 'TN_杏-04.jpg');
INSERT INTO `crop_image` VALUES (775, '杏-串枝红杏1', 50, 'TN_杏-串枝红杏1.jpg');
INSERT INTO `crop_image` VALUES (776, '杏-串枝红杏2', 50, 'TN_杏-串枝红杏2.jpg');
INSERT INTO `crop_image` VALUES (777, '杏-国家李杏资源圃', 50, 'TN_杏-国家李杏资源圃.jpg');
SET FOREIGN_KEY_CHECKS = 1;
开始对这张表生成增删改查
第二步
创建一个springboot工程
目录:
添加进common包(代码git自取)
加依赖,加4个
完整依赖
<?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.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mybaties-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<!--代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<!-- 模板引擎依赖 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
<!--swagger升级版-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
加入生成包
加入模板
yml配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
忘了加mysql包了这里是8.0的mysql所以加上去
来到这CodeGenerator里面,我们改点东西
65行,把这个改对了,包括下面的密码
78行,把你的包名设置的和你列表的一样
132行目录改下改成你commons目录里的BaseBean
改完了,突然发现少个工具类,额,复制上去
开始运行CodeGenerator
将我们的表名输入
回车,ok了
直接运行应用
额出错了,原因是没生成@mapper,我再加上一个
好了
再运行
好了浏览器输入http://localhost:8080/doc.html 测试下
这个页面是方便测试用的swagger升级版knight4j,没学过的可以搜下
额没显示出来,忘了knight还有个配置类
加上
修改下里面的配置,路径搞对了!
再启动 访问 ok了
测试下吧
我要查cropName为下面高亮的那一条
用get
ok查出来了,
后续的增删改和分页我就不测试了,有问题及时留言,我总会看到的,可能1-2天回复,有其他修改需求需要改模板的不清楚的也可以留言,这次示例代码上传到git上了地址在最上头,这个模板还不完全,因为分页查询我返回值没设置返回总页数不合理有点,还有返回格式的问题,后续会持续更新代码,更新到gitee上(下载得快一点),萌新新写的模板希望不要多喷