wiki.xsx.jg.core.TableGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of JG-All Show documentation
Show all versions of JG-All Show documentation
a generator, from the database tables convert to the Java classes or from the Java classes convert to the database tables.
support mysql, oracle, sqlserver and postgresql.
package wiki.xsx.jg.core;
import wiki.xsx.jg.annotation.Id;
import wiki.xsx.jg.annotation.Ignore;
import wiki.xsx.jg.annotation.IsNotNull;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
* 数据表生成器
*/
public class TableGenerator {
/**
* 生成数据表
* @param classNames 类名,多个逗号分隔
* @param driverClassName 数据库驱动名称
* @param dbUrl 数据库地址
* @param userName 数据库用户名
* @param password 数据库密码
* @param rootPath 包的根路径
* @param packageName 完整包名,如com.xsx.test
* @throws Exception 异常信息
*/
public static void run(
String classNames,
String driverClassName,
String dbUrl,
String userName,
String password,
String rootPath,
String packageName
) throws Exception{
DatabaseService service = DatabaseAdapter.getServiceInstance(
driverClassName,
dbUrl,
userName,
password,
null,
false
);
setClassInfo(getClassNameList(classNames, rootPath, packageName), service.getDatabase());
service.createTables(service.getDatabase().getTableSet());
}
/**
* 根据类名称或包获取完整类名称集合
* @param classNames 类名称,多个逗号分隔
* @param rootPath 包的根路径
* @param packageName 完整包名,如com.xsx.test
* @return 返回完整类名集合
*/
private static List getClassNameList(String classNames, String rootPath, String packageName){
List list = new ArrayList<>();
if (classNames!=null&&classNames.trim().length()>0){
String names[] = classNames.split(",");
for (String name : names){
StringBuilder classNameBuilder = new StringBuilder();
if (packageName!=null&&packageName.trim().length()>0){
classNameBuilder.append(packageName).append(".").append(name);
}else{
classNameBuilder.append(name);
}
list.add(classNameBuilder.toString());
}
}else{
StringBuilder pathBuilder = new StringBuilder(rootPath);
if (packageName!=null&&packageName.trim().length()>0){
String dirs[] = packageName.split("\\.");
for (String dir : dirs){
pathBuilder.append(File.separator).append(dir);
}
}
File fileDir = new File(pathBuilder.toString());
if (fileDir.isDirectory()){
String names[] = fileDir.list();
for (String name : names){
if (name.endsWith(".java")){
StringBuilder classNameBuilder = new StringBuilder();
if (packageName!=null&&packageName.trim().length()>0){
classNameBuilder.append(packageName).append(".").append(name.substring(0, name.lastIndexOf(".java")));
}else{
classNameBuilder.append(name.substring(0, name.lastIndexOf(".java")));
}
list.add(classNameBuilder.toString());
}
}
}
}
return list;
}
/**
* 根据完整类名称集合设置数据库表信息
* @param classNameList 完整类名称
* @param database 数据库对象
* @throws ClassNotFoundException 异常信息
*/
private static void setClassInfo(List classNameList, Database database) throws ClassNotFoundException{
for (String className : classNameList){
Class> tempClass = Class.forName(className);
if (tempClass.getAnnotation(Ignore.class)!=null){
continue;
}
Table table = new Table(tempClass.getSimpleName().toLowerCase(), null);
while (tempClass!=null&&!tempClass.getName().equalsIgnoreCase("java.lang.object")){
Field fields[] = tempClass.getDeclaredFields();
for (Field field : fields){
Ignore ignore = field.getAnnotation(Ignore.class);
if (ignore==null){
Column column = new Column();
column.setName(field.getName());
column.setJavaType(field.getType());
Id id = field.getAnnotation(Id.class);
if (id!=null){
column.setIsPrimaryKey(true);
}else{
IsNotNull isNotNull = field.getAnnotation(IsNotNull.class);
if (isNotNull!=null){
column.setIsNotNull(true);
}
}
table.getColumnMap().put(column.getName(), column);
}
}
tempClass = tempClass.getSuperclass();
}
database.getTableMap().put(table.getName(), table);
}
}
}