主要以:EasyCode代码生成工具, pagehelper分页工具,swagger测试工具类,为主
目录
e.1.生成aop包来处理分页,2.config包来处理swagger测试
1.swagger的注解使用,改变其页面的显示语言;主要是为了让前端或测试看的更明白;
一、项目创建
1.版本选择
以下jdk版本是以jdk11为主,jdk17略有不同.jdk17,可以选择3.0.5,最高版本
a.springBoot版本是2.7.10的;
b.选择spring web--Mybatis--Redis--生成
2.配置相关文件
pom.文件
因为依赖都具有适合的版本,所以如果用了以下的某些,可能不行,建议全部引用;
<?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.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>property-system</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>property-system</name>
<description>property-system</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</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-aop</artifactId>
</dependency>
<!--swagger:接口测试工具-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
a.连接数据库,EasyCode生成代码
用EasyCode生成代码时;需要配置
b.appLication.yml.核心文件配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///student
username: root
password: root
mybatis:
mapper-locations: classpath:mapper/*.xml
logging:
level:
cn.hx.property.dao: debug
c.启动类:配置映射到dao路径
@SpringBootApplication
@MapperScan("com.example.propert.dao")
public class PropertySystemApplication {
public static void main(String[] args) {
SpringApplication.run(PropertySystemApplication.class, args);
}
}
d.实体下的Vo层的:ReturnDean,作统一返回
package com.example.demo01.entity.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiParam;
@ApiModel("统一返回")
public class ReturnBean {
@ApiParam("成功与否")
private boolean success;
@ApiParam("成功与否")
private String message;
@ApiParam("返回的数据")
private Object data;
private ReturnBean() {
}
public static ReturnBean ofReturnBean(boolean success,String message){
ReturnBean returnBean = new ReturnBean();
returnBean.setMessage(message);
returnBean.setSuccess(success);
return returnBean;
}
//重载
public static ReturnBean ofReturnBean(boolean success,String message,Object data){
ReturnBean returnBean = ofReturnBean(success, message);
returnBean.setData(data);
return returnBean;
}
//对于查询来说都是成功
public static ReturnBean ofSuccessReturnBean(Object data) {
ReturnBean returnBean = ofSuccessReturnBean();
returnBean.setData(data);
return returnBean;
}
public static ReturnBean ofSuccessReturnBean() {
return ofReturnBean(true, "成功");
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
e.1.生成aop包来处理分页,2.config包来处理swagger测试
目录如下:
:
1.PageAspect
package com.example.propert.aop;
import com.github.pagehelper.PageHelper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* aop,对象目标对象进行增强
*/
@Component
@EnableAspectJAutoProxy
@Aspect
public class PageAspect {
@Around("execution(* com.example.propert.service.impl.*.queryAll(..))")
public Object pageAop(ProceedingJoinPoint joinPoint) throws Throwable {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
String page = request.getParameter("page");
String size = request.getParameter("size");
String orderBy = request.getParameter("orderBy");
if (StringUtils.hasText(page) && StringUtils.hasText(size)) {
PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(size), StringUtils.hasText(orderBy) ? orderBy : null);
}
Object proceed = joinPoint.proceed();
return proceed;
}
}
2.SwaggerConfig
package com.example.propert.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration//声明是一个配置类
public class SwaggerConfig {
@Bean//添加到容器
public Docket getDocket(){
ApiInfo apiInfo = new ApiInfoBuilder().title("物业管理系统").description("这是一个小区物业管理系统,目前正在开发").build();
return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo).select()
.apis(RequestHandlerSelectors.basePackage("com.example.propert.controller"))
.paths(PathSelectors.ant("/**")).build();
}
}
f:模板设置
a.以下三个模板分别复制--应用,有些轻微报错需要手动修改,
问题:1.RrturnBean的导包问题;
2.Service接口中的--queryAllByLimit--方法依然存在,需要手动删除,controller中的findAllcai
1.service接口
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Service"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表服务接口
*
* @author $!author
* @since $!time.currTime()
*/
public interface $!{tableName} {
/**
* 通过ID查询单条数据
*
* @param $!pk.name 主键
* @return 实例对象
*/
$!{tableInfo.name} queryById($!pk.shortType $!pk.name);
/**
* 查询多条数据
* @return 对象列表
*/
List<$!{tableInfo.name}> queryAll($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 新增数据
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return 实例对象
*/
$!{tableInfo.name} insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 修改数据
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return 实例对象
*/
$!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 通过主键删除数据
*
* @param $!pk.name 主键
* @return 是否成功
*/
boolean deleteById($!pk.shortType $!pk.name);
}
2.serviceImpl实现类
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "ServiceImpl"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service.impl;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表服务实现类
*
* @author $!author
* @since $!time.currTime()
*/
@Service("$!tool.firstLowerCase($!{tableInfo.name})Service")
public class $!{tableName} implements $!{tableInfo.name}Service {
@Resource
private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao;
/**
* 通过ID查询单条数据
*
* @param $!pk.name 主键
* @return 实例对象
*/
@Override
public $!{tableInfo.name} queryById($!pk.shortType $!pk.name) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.queryById($!pk.name);
}
/**
* 查询多条数据
* @return 对象列表
*/
@Override
public List<$!{tableInfo.name}> queryAll($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.queryAll($!tool.firstLowerCase($!{tableInfo.name}));
}
/**
* 新增数据
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return 实例对象
*/
@Override
public $!{tableInfo.name} insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.insert($!tool.firstLowerCase($!{tableInfo.name}));
return $!tool.firstLowerCase($!{tableInfo.name});
}
/**
* 修改数据
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return 实例对象
*/
@Override
public $!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.update($!tool.firstLowerCase($!{tableInfo.name}));
return this.queryById($!{tool.firstLowerCase($!{tableInfo.name})}.get$!tool.firstUpperCase($pk.name)());
}
/**
* 通过主键删除数据
*
* @param $!pk.name 主键
* @return 是否成功
*/
@Override
public boolean deleteById($!pk.shortType $!pk.name) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteById($!pk.name) > 0;
}
}
3.controller层
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Controller"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/controller"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}controller;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表控制层
*
* @author $!author
* @since $!time.currTime()
*/
@RestController
@RequestMapping("$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
/**
* 服务对象
*/
@Resource
private $!{tableInfo.name}Service $!{tool.firstLowerCase($!{tableInfo.name})}Service;
/**
* 通过主键查询单条数据
*/
@GetMapping("findById")
public ReturnBean selectOne(Long id) {
return ReturnBean.ofSuccessReturnBean($!{tool.firstLowerCase($!{tableInfo.name})}Service.queryById(id));
}
/**
* 分页查询数据 参数为 page size orderBy
*/
@GetMapping("findAll")
public ReturnBean findAll($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)) {
List<$!{tableInfo.name}> list = $!{tool.firstLowerCase($!{tableInfo.name})}Service.queryAll($!tool.firstLowerCase($tableInfo.name));
if (list instanceof Page<$!{tableInfo.name}> page) {
return ReturnBean.ofSuccessReturnBean(new PageInfo<>(page));
}
return ReturnBean.ofSuccessReturnBean(list);
}
@PostMapping("add")
public ReturnBean add($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)) {
$!{tool.firstLowerCase($!{tableInfo.name})}Service.insert($!tool.firstLowerCase($tableInfo.name));
return ReturnBean.ofSuccessReturnBean($!tool.firstLowerCase($tableInfo.name));
}
@PostMapping("update")
public ReturnBean update($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)) {
$!{tool.firstLowerCase($!{tableInfo.name})}Service.update($!tool.firstLowerCase($tableInfo.name));
return ReturnBean.ofSuccessReturnBean($!tool.firstLowerCase($tableInfo.name));
}
@PostMapping("deleteById")
public ReturnBean deleteById(Long id) {
$!{tool.firstLowerCase($!{tableInfo.name})}Service.deleteById(id);
return ReturnBean.ofSuccessReturnBean();
}
}
G:测试
运行项目后,浏览器访问该路径,进行测试,,如果想要中文的话,其实可以直接翻译成中文;
http://localhost:8080/swagger-ui/index.html
H:Test测试
@SpringBootTest
class PropertySystemApplicationTests {
@Resource
MenuDao menuDao;
@Test
void contextLoads() throws JsonProcessingException {
Menu menu = new Menu();
menu.setName("管理");
PageHelper.startPage(2,3,"menu_id desc");
List<Menu> menus = menuDao.queryAll(menu);
ObjectMapper objectMapper = new ObjectMapper();
System.out.println(menus);
System.out.println(menus.getClass());
System.out.println(objectMapper.writeValueAsString(new PageInfo<>(menus)));
}
}
二、细节注意
1.swagger的注解使用,改变其页面的显示语言;主要是为了让前端或测试看的更明白;
@Api(tags = "系统日志")--:只能用在类上
@ApiOperation("根据id查询部门")--:可以用在类上和方法上
@ApiImplicitParams({@ApiImplicitParam(name = "page",value = "当前页"), @ApiImplicitParam(name = "size",value = "页面大小"), @ApiImplicitParam(name = "orderBy",value = "排序规则")})--:可以用在参数里面;
2.QueryAll(User user)
注:EasyCode生成的QueryAll比较好,可以通过,某一个值来进行查询,其实分页上也上也就沿用了他,不用queryAllByLimit的,或者也可以,实现模糊查询是比较好的拼接例:
(and name like concat('%',#{name},'%')
源代码.例:
<select id="queryAll" resultMap="DeptMap">
select
dept_id, parent_id, name, order_num, del_flag
from student.sys_dept
<where>
<if test="deptId != null">
and dept_id = #{deptId}
</if>
<if test="parentId != null">
and parent_id = #{parentId}
</if>
<if test="name != null and name != ''">
and name like concat('%',#{name},'%')
</if>
<if test="orderNum != null">
and order_num = #{orderNum}
</if>
<if test="delFlag != null">
and del_flag = #{delFlag}
</if>
</where>
</select>
总结
基本完成了后端的所有代码块.