wiki.xsx.jg.core.CommonService 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.log.Logger;
import java.sql.*;
import java.util.Properties;
import java.util.Set;
/**
* 通用数据库实现类
*/
public class CommonService{
/**
* 根据数据库服务实现类设置表的信息
* @param service 数据库服务实现类
* @throws SQLException 异常信息
*/
public static void setTableInfo(
DatabaseService service
) throws SQLException {
Database database = service.getDatabase();
System.setProperty("jdbc.drivers", database.getDriverClassName());
Properties properties = new Properties();
properties.put("user", database.getUserName());
properties.put("password", database.getPassword());
properties.put("remarksReporting", "true");
try (Connection connection = DriverManager.getConnection(database.getUrl(), properties)){
DatabaseMetaData dbmd = connection.getMetaData();
for (Table table : database.getTableMap().values()){
ResultSet result = dbmd.getColumns(null, database.getSchema(), table.getName().toUpperCase(), null);
while (result.next()){
Column column = new Column();
column.setName(result.getString("COLUMN_NAME").toLowerCase());
column.setType(result.getString("TYPE_NAME").toUpperCase());
column.setLengh(result.getInt("COLUMN_SIZE"));
column.setScale(
result.getString("DECIMAL_DIGITS")==null?
0:result.getInt("DECIMAL_DIGITS")
);
column.setJavaType(service.getTypeClass(column.getType(), column.getLengh(), column.getScale()));
// 此处直接判断为null会抛异常,原因不明
column.setDefaultValue(result.getString("COLUMN_DEF"));
// 再次设值
column.setDefaultValue(column.getDefaultValue()!=null?column.getDefaultValue().trim():"NULL");
column.setIsAutoIncrement(result.getString("IS_AUTOINCREMENT").equalsIgnoreCase("YES")?true:false);
column.setIsNotNull(result.getInt("NULLABLE")==0?true:false);
column.setRemark(result.getString("REMARKS")==null?"":result.getString("REMARKS"));
table.getColumnMap().put(column.getName(), column);
}
result.close();
}
}
}
/**
* 根据数据库表集合创建数据表
* @param tableSet 数据库对象
* @param service 数据库服务实现类
* @throws SQLException 异常信息
*/
public static void createTables(
Set tableSet,
DatabaseService service
) throws SQLException {
Database database = service.getDatabase();
System.setProperty("jdbc.drivers", database.getDriverClassName());
try (
Connection connection = DriverManager.getConnection(database.getUrl(), database.getUserName(), database.getPassword());
PreparedStatement statement = connection.prepareStatement(service.testQuery())
){
for (Table table : tableSet){
StringBuilder dropSql = new StringBuilder(service.getDropTableSQL(table.getName()));
StringBuilder createSql = new StringBuilder(service.getCreateTableSQL(table));
statement.execute(dropSql.toString());
Logger.info("SQL: " + dropSql.toString());
statement.execute(createSql.toString());
Logger.info("SQL: " + createSql.toString() + "\n");
}
// for (Map.Entry>>>> classInfo : classInfoMap.entrySet()){
// StringBuilder className = new StringBuilder(classInfo.getKey());
// Map>>> fieldMap = classInfo.getValue();
// List