package com.zztlj.gbkh.generator;
import javax.sql.DataSource;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.activerecord.generator.Generator;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.druid.DruidPlugin;
/**
* GeneratorDemo
*/
public class GeneratorDemo {
public static DataSource getDataSource() {
Prop p = PropKit.use("config.properties");
// C3p0Plugin c3p0Plugin = new C3p0Plugin(p.get("jdbcUrl"), p.get("user"), p.get("password"));
// c3p0Plugin.setDriverClass("oracle.jdbc.driver.OracleDriver");
// c3p0Plugin.start();
// return c3p0Plugin.getDataSource();
//配置数据库连接池插件
DruidPlugin dp = new DruidPlugin(p.get("jdbcUrl"), p.get("user"), p.get("password"));
dp.setDriverClass("oracle.jdbc.driver.OracleDriver");
dp.setTimeBetweenConnectErrorMillis(30*1000);
dp.start();
return dp.getDataSource();
}
public static void main(String[] args) {
// base model 所使用的包名
String baseModelPackageName = "com.zztlj.gbkh.model.base";
// base model 文件保存路径
String baseModelOutputDir = PathKit.getWebRootPath() + "/../src/com/zztlj/gbkh/model/base";
// model 所使用的包名 (MappingKit 默认使用的包名)
String modelPackageName = "com.zztlj.gbkh.model";
// model 文件保存路径 (MappingKit 与 DataDictionary 文件默认保存路径)
String modelOutputDir = baseModelOutputDir + "/..";
// 创建生成器
Generator gernerator = new Generator(getDataSource(), baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir);
// 设置数据库方言
gernerator.setDialect(new OracleDialect());
// 添加不需要生成的表名
gernerator.addExcludedTable("TABLE_3");
// 设置是否在 Model 中生成 dao 对象
gernerator.setGenerateDaoInModel(true);
// 设置是否生成字典文件
gernerator.setGenerateDataDictionary(true);
// 设置需要被移除的表名前缀用于生成modelName。例如表名 "osc_user",移除前缀 "osc_"后生成的model名为 "User"而非 OscUser
//gernerator.setRemovedTableNamePrefixes("t_");
// 生成
gernerator.generate();
}
}
如果表中设置的默认值,则生成时报错:
Exception in thread "main" java.lang.RuntimeException: java.sql.SQLException: 流已被关闭
at com.jfinal.plugin.activerecord.generator.DataDictionaryGenerator.rebuildColumnMetas(DataDictionaryGenerator.java:190)
at com.jfinal.plugin.activerecord.generator.DataDictionaryGenerator.generate(DataDictionaryGenerator.java:58)
at com.jfinal.plugin.activerecord.generator.Generator.generate(Generator.java:227)
at com.zztlj.gbkh.generator.GeneratorDemo.main(GeneratorDemo.java:57)
Caused by: java.sql.SQLException: 流已被关闭
at oracle.jdbc.driver.LongAccessor.getBytesInternal(LongAccessor.java:156)
at oracle.jdbc.driver.LongAccessor.getBytes(LongAccessor.java:126)
at oracle.jdbc.driver.LongAccessor.getString(LongAccessor.java:201)
at oracle.jdbc.driver.T4CLongAccessor.getString(T4CLongAccessor.java:427)
at oracle.jdbc.driver.OracleResultSetImpl.getString(OracleResultSetImpl.java:1251)
at oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:494)
at com.jfinal.plugin.activerecord.generator.DataDictionaryGenerator.rebuildColumnMetas(DataDictionaryGenerator.java:169)
... 3 more