2022-10-08 17:33:31 +08:00
package com.rzyc.config ;
import com.common.utils.DateUtils ;
import com.common.utils.RandomNumber ;
2022-10-10 15:57:14 +08:00
import com.rzyc.controller.EmergencyController ;
2022-10-08 17:33:31 +08:00
import com.zaxxer.hikari.HikariConfig ;
import com.zaxxer.hikari.HikariDataSource ;
import java.lang.reflect.Method ;
import java.sql.* ;
/ * *
2022-10-10 15:57:14 +08:00
* 工具
2022-10-08 17:33:31 +08:00
* 获取方法中的注解参数 , 插入数据库
* @author Xuwanxin
* @date 2022 / 10 / 8
* /
2022-10-10 15:57:14 +08:00
public class GovMethodSignature {
2022-10-08 17:33:31 +08:00
public static void main ( String [ ] args ) {
2022-10-10 15:57:14 +08:00
//反射controller获取所有方法
Method [ ] methods = EmergencyController . class . getMethods ( ) ;
2022-10-08 17:33:31 +08:00
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 ( ) ;
2022-10-10 15:57:14 +08:00
PreparedStatement preparedStatement = con . prepareStatement ( " INSERT INTO `authority_key`(id,parent_resource,auth_key,category,create_time,modify_time,`name`) VALUES (?,?,?,?,?,?,?); " ) ;
2022-10-08 17:33:31 +08:00
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 ( ) ) {
String str = name . substring ( name . indexOf ( " : " ) + 1 , name . length ( ) ) ;
2022-10-10 15:57:14 +08:00
ResultSet rs = statement . executeQuery ( " select auth_key from authority_key where auth_key =' " + str + " ' " ) ;
2022-10-08 17:33:31 +08:00
//取数据
if ( rs . next ( ) ) {
2022-10-10 15:57:14 +08:00
} else {
2022-10-08 17:33:31 +08:00
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 ( ) ) ;
2022-10-10 15:57:14 +08:00
preparedStatement . setString ( 7 , annotation . name ( ) ) ;
2022-10-08 17:33:31 +08:00
preparedStatement . addBatch ( ) ;
2022-10-10 15:57:14 +08:00
}
2022-10-08 17:33:31 +08:00
}
preparedStatement . executeBatch ( ) ;
}
}
long endTime = System . currentTimeMillis ( ) ;
con . commit ( ) ;
System . out . println ( " 用时: " + ( endTime - startTime ) ) ;
//关闭connection
con . close ( ) ;
} catch ( SQLException e ) {
e . printStackTrace ( ) ;
}
}
}