package com.rzyc.config; import com.common.utils.DateUtils; import com.common.utils.RandomNumber; import com.rzyc.config.MethodAnnotation; import com.rzyc.controller.PersonalController; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import java.lang.reflect.Method; import java.sql.*; /** * 获取方法中的注解参数,插入数据库 * * @author Xuwanxin * @date 2022/10/8 */ public class MethodSignature { public static void main(String[] args) { //反射获取所有方法 Method[] methods = PersonalController.class.getMethods(); insertAnnotation(methods); } 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(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) VALUES (?,?,?,?,?,?);"); con.setAutoCommit(false); long startTime = System.currentTimeMillis(); if (methods.length>0){ statement.execute("truncate table authority_key"); } //遍历所有方法 for (Method m : methods) { //判断方法是否有MethodAnnotation注解 if (m.isAnnotationPresent(MethodAnnotation.class)) { MethodAnnotation annotation = m.getAnnotation(MethodAnnotation.class); for (String name : annotation.authorizations()) { String str = name.substring(name.indexOf(":")+1,name.length()); /* ResultSet rs = statement.executeQuery("select auth_key from authority_key where auth_key ='"+str+"'"); //取数据 if (rs.next()) { } else {}*/ String category = name.substring(0,name.indexOf(":")); preparedStatement.setString(1, RandomNumber.getUUid()); preparedStatement.setString(2,null); preparedStatement.setString(3,str); preparedStatement.setString(4,category); preparedStatement.setString(5, DateUtils.getNowDateTimeStr()); preparedStatement.setString(6,DateUtils.getNowDateTimeStr()); preparedStatement.addBatch(); } preparedStatement.executeBatch(); } } long endTime = System.currentTimeMillis(); con.commit(); System.out.println("用时:" + (endTime-startTime)); //关闭connection con.close(); } catch (SQLException e) { e.printStackTrace(); } } }