package com.rzyc.config; import com.common.utils.DateUtils; import com.common.utils.RandomNumber; import com.rzyc.controller.EmergencyController; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import org.springframework.web.bind.annotation.RequestMapping; import java.io.File; import java.lang.reflect.Method; import java.sql.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 工具 * 获取方法中的注解参数,插入数据库 * @author Xuwanxin * @date 2022/10/8 */ public class GovMethodSignature { public static void main(String[] args) throws ClassNotFoundException { String [] packageName = {"inventory-gov/src/main/java/com/rzyc/controller"}; List> classes = new ArrayList<>(); HashMap classNames = scanForPackageName(packageName); for (Map.Entry next: classNames.entrySet()) { try { classes.add(Class.forName(next.getValue())); } catch (ClassNotFoundException e) { e.printStackTrace(); } } for (Class c:classes) { //反射获取所有方法 Method[] methods = c.getMethods(); RequestMapping requestMapping = c.getAnnotation(RequestMapping.class); if (null != requestMapping && null != requestMapping.value()[0]) { String controllerName = requestMapping.value()[0]; insertAnnotation(controllerName, methods); } } } public static HashMap scanForPackageName(String [] path){ HashMap classNames = new HashMap<>(); String fileName = null; for (String s:path) { //根据传入文件夹路径创建File对象 File dir = new File(s); //检查是否为文件夹 if (dir.isDirectory()){ //遍历文件夹内的文件 for (File f : dir.listFiles()){ if (f.isDirectory()){ for (File f2 : f.listFiles()){ //获取文件名,并删除后缀 fileName = f2.getName(); try { fileName = fileName.substring(0,fileName.lastIndexOf(".")); }catch (Exception e){ System.err.println(fileName); } //添加到结果中 String filePath = f2.getPath().substring(f2.getPath().indexOf("java")+5,f2.getPath().length()).replace("\\",".").replace(".java",""); classNames.put(fileName,filePath); continue; } }else { //获取文件名,并删除后缀 fileName = f.getName(); try { fileName = fileName.substring(0,fileName.lastIndexOf(".")); }catch (Exception e){ System.err.println(fileName); } //添加到结果中 String filePath = f.getPath().substring(f.getPath().indexOf("java")+5,f.getPath().length()).replace("\\",".").replace(".java",""); classNames.put(fileName,filePath); continue; } } } } return classNames; } private static HikariDataSource buildingSource() { //配置文件 HikariConfig hikariConfig = new HikariConfig(); //mysql hikariConfig.setJdbcUrl("jdbc:mysql://121.40.106.103:3306/inventory_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false"); hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver"); hikariConfig.setUsername("rzyc"); hikariConfig.setPassword("admin@rzyc2022.com##"); hikariConfig.addDataSourceProperty("minimumIdle", "3"); hikariConfig.addDataSourceProperty("maximumPoolSize", "10"); hikariConfig.addDataSourceProperty("maxLifetime", "30000"); HikariDataSource ds = new HikariDataSource(hikariConfig); return ds; } private static void insertAnnotation(String controllerName,Method[] methods) { try { //创建connection Connection con = buildingSource().getConnection(); Statement statement = con.createStatement(); PreparedStatement preparedStatement = con.prepareStatement("INSERT INTO `authority_key`(id,parent_resource,auth_key,category,create_time,modify_time,`name`) VALUES (?,?,?,?,?,?,?);"); con.setAutoCommit(false); long startTime = System.currentTimeMillis(); //遍历所有方法 for (Method m : methods) { //判断方法是否有MethodAnnotation注解 if (m.isAnnotationPresent(MethodAnnotation.class)) { MethodAnnotation annotation = m.getAnnotation(MethodAnnotation.class); for (String name : annotation.authorizations()) { ResultSet rs = statement.executeQuery("select auth_key from authority_key where auth_key ='"+name+"'"); //取数据 if (rs.next()) { } else { preparedStatement.setString(1, RandomNumber.getUUid()); preparedStatement.setString(2,null); preparedStatement.setString(3,name); preparedStatement.setString(4,controllerName); preparedStatement.setString(5, DateUtils.getNowDateTimeStr()); preparedStatement.setString(6,DateUtils.getNowDateTimeStr()); preparedStatement.setString(7,annotation.name()); preparedStatement.addBatch(); } } preparedStatement.executeBatch(); } } long endTime = System.currentTimeMillis(); con.commit(); System.out.println("用时:" + (endTime-startTime)); //关闭connection con.close(); } catch (SQLException e) { e.printStackTrace(); } } }