目录
1.创建springboot项目
2.环境准备
2.1.数据库
2.2.引入依赖
2.3.修改application.properties配置文件
2.4.创建代码生成器MybatisPlusGenerator.java
2.5.创建SwaggerConfig.java配置文件
2.6.创建分页配置类
2.7.创建类:统一响应的json数据
2.8.条件查询中所需要的条件
3.前端准备
3.1.引入前端所需要的 js,css文件(在代码包中)
3.2.前端页面布局及CRUD
4.后台代码书写
4.1.运行代码生成器MybatisPlusGenerator.java
4.1.TbZoneController.java中编写CRUD
5.项目总览
5.1.包
5.2.最终效果图
1.创建springboot项目
...
2.环境准备
2.1.数据库
DROP TABLE IF EXISTS `tb_zone`; CREATE TABLE `tb_zone` ( `id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '主键', `gmt_create` datetime(0) NOT NULL COMMENT '创建时间', `gmt_modified` datetime(0) NULL DEFAULT NULL COMMENT '修改时间', `is_deleted` int(10) NOT NULL COMMENT '逻辑删除(0:正常;1:删除)', `is_disable` int(11) NOT NULL COMMENT '状态(0:正常;1:禁用)', `zone_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '名称', `zone_desc` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '描述', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '专区' ROW_FORMAT = Compact; -- ---------------------------- -- Records of tb_zone -- ---------------------------- INSERT INTO `tb_zone` VALUES ('1', '2022-09-02 10:00:34', '2022-09-22 13:52:11', 0, 0, '农副产品', '瓜果蔬菜 无公害 无污染'); INSERT INTO `tb_zone` VALUES ('2', '2018-07-04 10:45:07', '2022-09-02 09:18:39', 0, 0, '海鲜水产', '龙虾 螃蟹 生蚝 种类齐全'); INSERT INTO `tb_zone` VALUES ('3', '2018-07-04 10:45:07', '2022-09-02 11:13:36', 0, 0, '香甜水果', '葡梨浆果 多汁鲜物'); INSERT INTO `tb_zone` VALUES ('4', '2018-07-04 10:45:07', '2021-06-21 10:58:07', 0, 0, '有机蔬菜', '无公害 脆嫩 新鲜润胃'); INSERT INTO `tb_zone` VALUES ('5', '2018-07-04 10:45:07', '2021-06-21 10:58:07', 0, 0, '粮油专区', ' 非转基因 调和油 植物油');
2.2.引入依赖
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.12.RELEASE com.example CS1 0.0.1-SNAPSHOT CS1 CS1 1.8 com.baomidou mybatis-plus-boot-starter 3.5.1 com.baomidou mybatis-plus-generator 3.5.1 org.apache.velocity velocity-engine-core 2.3 org.freemarker freemarker 2.3.29 io.github.jianzhichun spring-boot-starter-swagger2 0.0.1 com.github.xiaoymin swagger-bootstrap-ui 1.9.6 org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.3.1 mysql mysql-connector-java runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.mybatis.spring.boot mybatis-spring-boot-starter-test 2.3.1 test org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok 2.3.修改application.properties配置文件
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus?serverTimezone=Asia/Shanghai&characterEncoding=UTF8 spring.datasource.username=root spring.datasource.password=123456789 mybatis-plus.mapper-locations=classpath:/mapper/*.xml mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
2.4.创建代码生成器MybatisPlusGenerator.java
package com.wqg; 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 MybatisPlusGenerator { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/mybatisplus?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai"; String projectPath = "D:\\java1\\reggie\\CS1"; FastAutoGenerator.create(url, "root", "123456789") .globalConfig(builder -> { builder.author("WQG") // 设置作者 .enableSwagger() // 开启 swagger 模式 .fileOverride() // 覆盖已生成文件 .outputDir(projectPath + "/src/main/java"); // 指定输出目录 }) .packageConfig(builder -> { builder.parent("com.wqg") // 设置父包名 .moduleName("zone") // 设置父包模块名 .pathInfo(Collections.singletonMap(OutputFile.mapperXml, projectPath + "/src/main/resources/mapper/"));// 设置mapperXml生成路径 }) .strategyConfig(builder -> { builder.entityBuilder().enableLombok();//启用lombok builder.mapperBuilder().enableMapperAnnotation().build();//启用映射器注释 builder.controllerBuilder().enableHyphenStyle() // 开启驼峰转连字符 .enableRestStyle(); // 开启生成@RestController 控制器 builder.addInclude("tb_zone"); // 设置需要生成的表名 }) .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板 // 开始处理 .execute(); } }
2.5.创建SwaggerConfig.java配置文件
package com.wqg.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration //表示类似与配置文件 public class SwaggerConfig { @Bean //加在方法上,表示吧方法的返回结果交于spring容器来管理该对象. 里面封装了接口文档的信息, public Docket docket(){ Docket docket=new Docket(DocumentationType.SWAGGER_2) .groupName("qy165") .apiInfo(getInfo()) .select() //只为com.aaa.controller包下的类生成接口文档 .apis(RequestHandlerSelectors.basePackage("com.wqg")) .build(); return docket; } private ApiInfo getInfo(){ Contact DEFAULT_CONTACT = new Contact("张三", "http://www.jd.com", "[email protected]"); ApiInfo apiInfo= new ApiInfo("员工管理系统API文档", "员工管理系统API文档", "1.5", "localhost:8081/doc.html", DEFAULT_CONTACT, "AAA志远网络有限公司", "http://www.aaa.com"); return apiInfo; } }
2.6.创建分页配置类
@Configuration public class MybatisPlusConfig { //分页配置类 /** * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除) */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
2.7.创建类:统一响应的json数据
@NoArgsConstructor @AllArgsConstructor @Data @ApiModel(value = "统一响应的json数据") public class Result { //表示状态码 @ApiModelProperty(value = "状态码 200表示成功 500表示服务器错误 401表示权限不足") private Integer code; //消息提示 @ApiModelProperty(value = "响应的消息内容") private String msg; //响应的数据内容 @ApiModelProperty(value = "响应的数据") private Object data; }
2.8.条件查询中所需要的条件
@Data @NoArgsConstructor @AllArgsConstructor @ApiModel(value = "条件查询") public class ZoneVo { @ApiModelProperty("状态(0:正常;1:禁用)") private Integer isDisable; @ApiModelProperty("名称") private String zoneName; }
3.前端准备
3.1.引入前端所需要的 js,css文件(在代码包中)
3.2.前端页面布局及CRUD
产品管理 查询 添加 正常 删除 修改 删除 4.后台代码书写
4.1.运行代码生成器MybatisPlusGenerator.java
一键生成
4.1.TbZoneController.java中编写CRUD
package com.wqg.zone.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.wqg.zone.entity.TbZone; import com.wqg.zone.service.ITbZoneService; import com.wqg.zone.vo.Result; import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; /** *
* 专区 前端控制器 *
* * @author WQG * @since 2023-06-25 */ @RestController @RequestMapping("/zone/tb-zone") public class TbZoneController { @Autowired private ITbZoneService tbZoneService; @ApiModelProperty @ApiOperation(value = "分页条件查询") @PostMapping("list") public Result list2(Integer current , Integer pageSize , @RequestBody TbZone tbZone){ //分页 IPagepage = new Page<>(current,pageSize); //查询条件构造器 QueryWrapper wrapper = new QueryWrapper<>(); if (StringUtils.hasText(tbZone.getZoneName())){ wrapper.like("zone_name",tbZone.getZoneName()); } if (tbZone.getIsDisable() != null){ wrapper.eq("is_disable",tbZone.getIsDisable()); } IPage userIPage = tbZoneService.page(page,wrapper); return new Result(200,"查询成功",userIPage); } //删除 @ApiModelProperty @ApiOperation(value = "删除") @GetMapping("deleteById") public Result deleteById(String id){ boolean i = tbZoneService.removeById(id); return i == true ? new Result(200, "删除成功", null) : new Result(500, "删除失败", null); } //添加 @ApiModelProperty @ApiOperation(value = "添加") @PostMapping("add") public Result add(@RequestBody TbZone tbZone){ boolean save = tbZoneService.save(tbZone); return save == true ? new Result(200, "添加成功", null) : new Result(500, "添加失败", null); } //修改 @ApiModelProperty @ApiOperation(value = "修改") @PostMapping("update") public Result update(@RequestBody TbZone tbZone){ boolean b = tbZoneService.updateById(tbZone); return b == true ? new Result(200, "修改成功", null) : new Result(500, "修改失败", null); } //根据id修改状态 @ApiModelProperty @ApiOperation(value = "根据id修改状态") @PostMapping("updateIsDisable") public Result updateIsDisable(String id , Integer isDisable){ UpdateWrapper wrapper = new UpdateWrapper<>(); wrapper.eq("id",id); wrapper.set("is_disable",isDisable); boolean update = tbZoneService.update(wrapper); return update == true ? new Result(200, "状态修改成功", null) : new Result(500, "状态修改失败", null); } } 5.项目总览
5.1.包
5.2.最终效果图