There are a lot of functions to automatically scan the package registration in ssh and other frameworks. When there are many classes, we can reduce the workload a lot.
According to this idea, I wrote a Simple plug-in for automatically registering the Model by scanning the package.
Under scan classes, the classes that inherit the Model are registered in ActiveRecordPlugin. The database table name uses Luo Feng by default.
For example, MobileBind. Java is mapped to the mobieBind table. In addition, you can select the rules of all uppercase and lowercase letters.
01
/**
02
* Configure the plug-in
03
*/
04
Public void configPlugin (Plugins me ){
05
DruidPlugin druidPlugin = new DruidPlugin. DruidBuilder (getProperty ("url"), getProperty ("username "),
06
GetProperty ("password"). build ();
07
Me. add (druidPlugin );
08
ActiveRecordPlugin arp = new ActiveRecordPlugin (druidPlugin );
09
Arp. setShowSql (true );
10
SqlReporter. setLogger (true );
11
AutoScanPlugin autoScanPligin = new AutoScanPlugin (arp, TableNameStyle. LOWER );
12
Me. add (autoScanPligin); // You must register the model plug-in before ActiveRecordPlugin.
13
Me. add (arp );
14
}
01
Package com. jfinal. plugin. autoscan;
02
Import java. io. File;
03
Import java.net. URL;
04
Import java. util. List;
05
06
Import com. cloud. pxxt. Config;
07
Import com. jfinal. plugin. IPlugin;
08
Import com. jfinal. plugin. activerecord. ActiveRecordPlugin;
09
Import com. jfinal. plugin. activerecord. Model;
10
Import com. jfinal. util. StringKit;
11
12
/**
13 www.2cto.com
* Automatic package Scanning
14
*/
15
Public class AutoScanPlugin implements IPlugin {
16
Private ActiveRecordPlugin arp;
17
18
Private TableNameStyle tableNameStyle;
19
20
Public AutoScanPlugin (ActiveRecordPlugin arp ){
21
This. arp = arp;
22
}
23
Public AutoScanPlugin (ActiveRecordPlugin arp, TableNameStyle tableNameStyle ){
24
This. arp = arp;
25
This. tableNameStyle = tableNameStyle;
26
}
27
28
@ Override
29
Public boolean start (){
30
Try {
31
RegistModel (AutoScanPlugin. class. getResource ("/"). getFile (), "classes /");
32
} Catch (Exception e ){
33
Throw new RuntimeException (e );
34
}
35
Return true;
36
}
37
38
@ Override
39
Public boolean stop (){
40
Return true;
41
}
42
43
Private void registModel (String fileDir, String pre) throws Exception {
44
List <File> classList = FileSearcher. findFiles (fileDir, "*. class ");
45
For (File classFile: classList ){
46
String className = getClassName (classFile, pre );
47
Class clazz = Class. forName (className );
48
If (clazz. getSuperclass () = Model. class ){
49
String tableName = clazz. getSimpleName ();
50
If (tableNameStyle = TableNameStyle. UP ){
51
TableName = tableName. toUpperCase ();
52
} Else if (tableNameStyle = TableNameStyle. LOWER ){
53
TableName = tableName. toLowerCase ();
54
} Else {
55
TableName = StringKit. firstCharToLowerCase (tableName );
56
57
}
58
Arp. addMapping (tableName, clazz );
59
}
60
}
61
}
62
63
Private static String getClassName (File classFile, String pre ){
64
String objStr = classFile. toString (). replaceAll ("\\\\","/");
65
String className;
66
ClassName = objStr. substring (objStr. indexOf (pre) + pre. length (), objStr. indexOf (". class "));
67
If (className. startsWith ("/")){
68
ClassName = className. substring (className. indexOf ("/") + 1 );
69
}
70
Return className. replaceAll ("/",".");
71
}
72
73
}
1
Package com. jfinal. plugin. autoscan;
2
3
Public enum TableNameStyle {
4
UP, LOWER
5
}
There are other methods for automatic detection. For example, if we agree that the Model object uses xxModel. java, we can find the Model directly according to the file name rule during the detection.
Currently, this method is used to load a class based on whether the parent class of this class is Mode. The cost is higher.
Some problems ., in this way, the automatically detected table name ing rules are sufficient. If the database shows that the rules are not very good, you may need to expand several other policies indicated by the database survival based on the class name, for example, oracle databases have many underlined naming methods.
In addition, this method cannot detect the Model in the jar package. It will be implemented later.
The above provides you with some ideas.
Although JFinal itself is enough to reduce the amount of simple code, we can also reduce the number of written code ~
Author: Desperate skin