关于jeecg-boot常用注解分类总结
关于jeecg-boot常用注解分类总结
jeecg-boot使用有lombok注解,swagger注解,springboot注解,自定义注解,导入导出Excel注解
lombok注解
1.@Data注解在类上,会为类的所有属性自动生成setter/getter、equals、canEqual、hashCode、toString方法,如为final属性,则不会为该属性生成setter方法。
也可以使用@Getter/@Setter注解添加到对应的属性上,则只生成对应属性的get/set方法;
2.@EqualsAndHashCode(callSuper = false) ,不调用父类的属性,那么子类属性里面的相同的话,那hashcode的值就相同;
3.@Accessors(chain = true):chain的中文含义是链式的,设置为true,则setter方法返回当前对象,如果不加,则返回当前属性;
swagger注解
https://blog.csdn.net/wyb880501/article/details/79576784
在实体类Eneity上
- @ApiModel()用于类
表示对类进行说明,用于参数用实体类接收 - @ApiModelProperty()用于方法,字段
- @ApiIgnore()用于类,方法,方法参数
表示这个方法或者类被忽略
在接口类上 - @Api()用于类;
表示标识这个类是swagger的资源 - @ApiOperation()用于方法;
表示一个http请求的操作 - @ApiParam()用于方法,参数,字段说明;
表示对参数的添加元数据(说明或是否必填等) - @ApiImplicitParam() 用于方法
表示单独的请求参数 - @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam
@Data //lomlok注解
@TableName("students") //maybatis-plus的注解
@EqualsAndHashCode(callSuper = false)//lomlok生成的equals,hashCode注解
@Accessors(chain = true)
@ApiModel(value="students对象", description="学生类") //Swager注解
public class Students {
/**id*/
@TableId(type = IdType.UUID)
@ApiModelProperty(value = "id")
private java.lang.String id;
/**学生信息*/
@Excel(name = "学生信息", width = 15)
@ApiModelProperty(value = "学生信息")
private java.lang.String name;
/**年龄*/
@Excel(name = "年龄", width = 15)
@ApiModelProperty(value = "年龄")
private java.lang.Integer age;
/**性别(0女1男)*/
@Excel(name = "性别(0女1男)", width = 15)
@ApiModelProperty(value = "性别(0女1男)")
private java.lang.Integer sex;
/**学号*/
@Excel(name = "学号", width = 15)
@ApiModelProperty(value = "学号")
private java.lang.String studentNumber;
/**创建时间*/
@Excel(name = "创建时间", width = 20, format = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "创建时间")
private java.util.Date createTime;
/**创建人*/
@Excel(name = "创建人", width = 15)
@ApiModelProperty(value = "创建人")
private java.lang.String createName;
/**修改时间*/
@Excel(name = "修改时间", width = 20, format = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "修改时间")
private java.util.Date updateTime;
/**修改人*/
@Excel(name = "修改人", width = 15)
@ApiModelProperty(value = "修改人1111")
private java.lang.String updateName;
}
@Slf4j
@Api(tags="学生类")
@RestController
@RequestMapping("/text/students")
public class StudentsController {
@Autowired
private IStudentsService studentsService;
@AutoLog(value = "学生类-分页列表查询")
@ApiOperation(value="学生类-分页列表查询", notes="学生类-分页列表查询")
@GetMapping(value = "/list")
public Result<IPage<Students>> queryPageList(Students students,
@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
HttpServletRequest req) {
Result<IPage<Students>> result = new Result<IPage<Students>>();
QueryWrapper<Students> queryWrapper = QueryGenerator.initQueryWrapper(students, req.getParameterMap());
Page<Students> page = new Page<Students>(pageNo, pageSize);
IPage<Students> pageList = studentsService.page(page, queryWrapper);
result.setSuccess(true);
result.setResult(pageList);
return result;
}
@AutoLog(value = "学生类-添加")
@ApiOperation(value="学生类-添加", notes="学生类-添加")
@PostMapping(value = "/add")
@RequiresPermissions("students:add")
public Result<Students> add(@RequestBody Students students) {
Result<Students> result = new Result<Students>();
try {
studentsService.save(students);
result.success("添加成功!");
} catch (Exception e) {
log.error(e.getMessage(),e);
result.error500("操作失败");
}
return result;
}
自定义注解
@AutoLog
在需要记录日志信息的方法上添加@AutoLog注解,通过配置的切面类,即可插入数据库对应的日志信息
@Aspect
@Component
public class AutoLogAspect {
@Autowired
private ISysLogService sysLogService;
@Pointcut("@annotation(org.jeecg.common.aspect.annotation.AutoLog)")
public void logPointCut() {
}
@Around("logPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
long beginTime = System.currentTimeMillis();
//执行方法
Object result = point.proceed();
//执行时长(毫秒)
long time = System.currentTimeMillis() - beginTime;
//保存日志
saveSysLog(point, time);
return result;
}
private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
SysLog sysLog = new SysLog();
AutoLog syslog = method.getAnnotation(AutoLog.class);
if(syslog != null){
//注解上的描述,操作日志内容
sysLog.setLogContent(syslog.value());
sysLog.setLogType(syslog.logType());
}
//请求的方法名
String className = joinPoint.getTarget().getClass().getName();
String methodName = signature.getName();
sysLog.setMethod(className + "." + methodName + "()");
//请求的参数
Object[] args = joinPoint.getArgs();
try{
String params = JSONObject.toJSONString(args);
sysLog.setRequestParam(params);
}catch (Exception e){
}
//获取request
HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
//设置IP地址
sysLog.setIp(IPUtils.getIpAddr(request));
//获取登录用户信息
LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
if(sysUser!=null){
sysLog.setUserid(sysUser.getUsername());
sysLog.setUsername(sysUser.getRealname());
}
//耗时
sysLog.setCostTime(time);
sysLog.setCreateTime(new Date());
//保存系统日志
sysLogService.save(sysLog);
}
}
@Dict
在生成的实体类的属性上添加@Dict注解
/**
* 日志类型(1登录日志,2操作日志)
*/
@Dict(dicCode = "log_type")
private Integer logType;
在前台查询时,会返回logType(对应的数据库的值),同时返回logType_dictText,为通过切面解析到的文本值;
@PermissionData
此注解是用来进行数据权限控制
1在前台页面对指定菜单添加数据规则
2配置角色授予权限规则
3.在进行查询的方法上添加 @PermissionData(pageComponent=“text/StudentsList”)注解