2016-09-21 12:50

@JFinal public static DataSource getDataSource() {
Prop p = PropKit.use("a_little_config.txt");
C3p0Plugin c3p0Plugin = new C3p0Plugin(p.get("jdbc.url").trim(), p.get("jdbc.username").trim(), p.get("jdbc.password").trim(), p.get("jdbc.driverClassName").trim());
ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
arp.setDialect(new SqlServerDialect());
c3p0Plugin.start();
arp.start();
return c3p0Plugin.getDataSource();
}

public static void main(String[] args) {
// base model 所使用的包名
String baseModelPackageName = "com.chine.model.base";
// base model 文件保存路径
String baseModelOutputDir = PathKit.getWebRootPath() + "/../src/com/chine/model/base";

// model 所使用的包名 (MappingKit 默认使用的包名)
String modelPackageName = "com.chine.model";
// model 文件保存路径 (MappingKit 与 DataDictionary 文件默认保存路径)
String modelOutputDir = baseModelOutputDir + "/..";

// 创建生成器
Generator gernerator = new Generator(getDataSource(), baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir);
// 设置数据库方言
//gernerator.setDialect(new MysqlDialect());
gernerator.setDialect(new SqlServerDialect());
// 添加不需要生成的表名
//gernerator.addExcludedTable("adv");
// 设置是否在 Model 中生成 dao 对象
gernerator.setGenerateDaoInModel(true);
// 设置是否生成字典文件
gernerator.setGenerateDataDictionary(false);
// 设置需要被移除的表名前缀用于生成modelName。例如表名 "osc_user",移除前缀 "osc_"后生成的model名为 "User"而非 OscUser
gernerator.setRemovedTableNamePrefixes("t_");
// 生成
gernerator.generate();
}

数据库是sql server2008R2自动生成时总是报下面的错误:

Build TableMeta ...
Exception in thread "main" java.lang.RuntimeException: com.microsoft.sqlserver.jdbc.SQLServerException: 对象名 'CHECK_CONSTRAINTS' 无效。
at com.jfinal.plugin.activerecord.generator.MetaBuilder.build(MetaBuilder.java:102)
at com.jfinal.plugin.activerecord.generator.Generator.generate(Generator.java:210)
at com.chine.model.GeneratorModel.main(GeneratorModel.java:54)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 对象名 'CHECK_CONSTRAINTS' 无效。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1635)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:865)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:762)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:6276)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1793)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:184)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:159)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeQuery(SQLServerStatement.java:661)
at com.mchange.v2.c3p0.impl.NewProxyStatement.executeQuery(NewProxyStatement.java:327)
at com.jfinal.plugin.activerecord.generator.MetaBuilder.buildColumnMetas(MetaBuilder.java:216)
at com.jfinal.plugin.activerecord.generator.MetaBuilder.build(MetaBuilder.java:97)
... 2 more