前言
需求:有一张表,数据如下。SRV_DESC字段值为查询列表信息,需要修改为如图示的描述信息。示例:查询列表信息,修改后为单据状态-查询列表信息。
SRV_URL字段值为控制层中的@GetMapping、@PostMapping的值。示例 :@GetMapping("/102020029")。根据此值修改SRV_DESC的值。
由于数据量过大,所以搞成了页面操作方式。直接传参数类名、类路径、需要追加的值即可。
下面直接上代码:(代码过于简陋,还请大佬勿喷)
public Result findList(String param, String classNamePath, String className) throws ClassNotFoundException {
// String classNamePath = "com.xxx.sys.controller."; // String className = "BtnController"; Class<?> clazz = Class.forName(classNamePath + className); //得到字节码文件 【只需要更改controller类名】 // Class<?> clazz = BtnController.class; //得到方法 Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { //判断是否存在GetMapping注释 boolean present1 = method.isAnnotationPresent(GetMapping.class); if (present1) { //得到requestMapping注释 GetMapping annotation = method.getAnnotation(GetMapping.class); //输出 annotation RequestMapping包含的信息(headers=[], name=, path=[], value=[toTicket], produces=[], method=[], params=[], consumes=[]) //System.err.println(annotation); //得到value数组 String[] value = annotation.value(); for (String string2 : value) { //输出value值 System.out.println("Get: " + string2.replace("/","")); SrvObj obj = new SrvObj(); obj.setSrvUrl(string2.replace("/","")); // srvObjService.updateSrvObjDesc(param, obj); } } //判断是否存在PostMapping注释 boolean present2 = method.isAnnotationPresent(PostMapping.class); if (present2) { //得到PostMapping注释 PostMapping annotation = method.getAnnotation(PostMapping.class); //得到value数组 String[] value = annotation.value(); for (String string2 : value) { //输出value值 System.out.println("Post: " + string2.replace("/","")); SrvObj obj = new SrvObj(); obj.setSrvUrl(string2.replace("/","")); // srvObjService.updateSrvObjDesc(param, obj); } } } return Result.success(); }
public int updateSrvObjDesc(String param, SrvObj srvObj){ QueryWrapper<SrvObj> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("SRV_URL", srvObj.getSrvUrl()); SrvObj srvObj1 = srvObjMapper.selectOne(queryWrapper); int affectRows = 0; if(srvObj1 != null){ srvObj.setSrvDesc(param + srvObj1.getSrvDesc()); UpdateWrapper<SrvObj> updateWrapper=new UpdateWrapper<>(); updateWrapper.eq("SRV_URL",srvObj.getSrvUrl()); affectRows = srvObjMapper.update(srvObj, updateWrapper); if(affectRows>0){ System.out.println("更新成功"); }else{ System.out.println("更新失败"); } } return affectRows; }
简单记录下