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 ;
2022-10-11 15:21:09 +08:00
import org.springframework.web.bind.annotation.RequestMapping ;
2022-10-08 17:33:31 +08:00
2022-10-11 15:21:09 +08:00
import java.io.File ;
2022-10-08 17:33:31 +08:00
import java.lang.reflect.Method ;
import java.sql.* ;
2022-10-11 15:21:09 +08:00
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
2022-10-08 17:33:31 +08:00
/ * *
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-11 15:21:09 +08:00
public static void main ( String [ ] args ) throws ClassNotFoundException {
String [ ] packageName = { " inventory-gov/src/main/java/com/rzyc/controller " } ;
List < Class < ? > > classes = new ArrayList < > ( ) ;
HashMap < String , String > classNames = scanForPackageName ( packageName ) ;
for ( Map . Entry < String , String > next : classNames . entrySet ( ) ) {
try {
classes . add ( Class . forName ( next . getValue ( ) ) ) ;
} catch ( ClassNotFoundException e ) {
e . printStackTrace ( ) ;
}
}
2022-10-08 17:33:31 +08:00
2022-10-11 15:21:09 +08:00
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 < String , String > scanForPackageName ( String [ ] path ) {
HashMap < String , String > 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 ;
2022-10-08 17:33:31 +08:00
}
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 ;
}
2022-10-11 15:21:09 +08:00
private static void insertAnnotation ( String controllerName , Method [ ] methods ) {
2022-10-08 17:33:31 +08:00
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 ( ) ) {
2022-10-11 15:21:09 +08:00
ResultSet rs = statement . executeQuery ( " select auth_key from authority_key where auth_key =' " + name + " ' " ) ;
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
preparedStatement . setString ( 1 , RandomNumber . getUUid ( ) ) ;
preparedStatement . setString ( 2 , null ) ;
2022-10-11 15:21:09 +08:00
preparedStatement . setString ( 3 , name ) ;
preparedStatement . setString ( 4 , controllerName ) ;
2022-10-08 17:33:31 +08:00
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 ( ) ;
}
}
2022-10-11 15:21:09 +08:00
2022-10-08 17:33:31 +08:00
}