Jfinal配置自动生成model 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 package com.demo.common.model;import javax.sql.DataSource;import com.jfinal.kit.PathKit;import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect;import com.jfinal.plugin.activerecord.generator.Generator;import com.jfinal.plugin.druid.DruidPlugin;public class _JFinalDemoGenerator { public static DataSource getDataSource () { DruidPlugin druidPlugin = new DruidPlugin( "jdbc:postgresql://localhost:5444/console1?currentSchema=console1" , "username" , "password" ); druidPlugin.start(); return druidPlugin.getDataSource(); } public static void main (String[] args) { String baseModelPackageName = "com.model.base" ; String baseModelOutputDir = PathKit.getWebRootPath() + "/src/main/java/com/model/base" ; String modelPackageName = "com.model" ; String modelOutputDir = baseModelOutputDir + "/.." ; Generator generator = new Generator(getDataSource(), baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir); generator.setGenerateRemarks(true ); generator.setDialect(new PostgreSqlDialect()); generator.setGenerateChainSetter(false ); generator.addExcludedTable("adv" ); generator.setGenerateDaoInModel(false ); generator.setGenerateDataDictionary(false ); generator.setMetaBuilder(new SelfMetaBuilder(getDataSource())); generator.setGenerateChainSetter(true ); generator.generate(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 package com.demo.common.model;import java.sql.ResultSet;import java.sql.SQLException;import java.util.List;import javax.sql.DataSource;import com.jfinal.plugin.activerecord.generator.MetaBuilder;import com.jfinal.plugin.activerecord.generator.TableMeta;public class SelfMetaBuilder extends MetaBuilder { public SelfMetaBuilder (DataSource dataSource) { super (dataSource); } @Override protected void buildTableNames (List<TableMeta> ret) throws SQLException { ResultSet rs = getTablesResultSet(); while (rs.next()) { String tableName = rs.getString("TABLE_NAME" ); if (!tableName.startsWith("blog_" )) { System.out.println("Skip table :" + tableName); continue ; } if (isSkipTable(tableName)) { System.out.println("Skip table :" + tableName); continue ; } if (filterPredicate != null && filterPredicate.test(tableName)) { System.out.println("Skip table :" + tableName); continue ; } TableMeta tableMeta = new TableMeta(); tableMeta.name = tableName; tableMeta.remarks = rs.getString("REMARKS" ); tableMeta.modelName = buildModelName(tableName); tableMeta.baseModelName = buildBaseModelName(tableMeta.modelName); ret.add(tableMeta); } rs.close(); } }
Jfinal 初始化 1 2 3 4 5 6 7 8 9 10 public class DemoConfig extends JFinalConfig { public void configPlugin (Plugins me) { DruidPlugin dp = new DruidPlugin("jdbc:mysql://localhost/db_name" , "userName" , "password" ); me.add(dp); ActiveRecordPlugin arp = new ActiveRecordPlugin(dp); me.add(arp); arp.addMapping("user" , User.class); arp.addMapping("article" , "article_id" , Article.class); } }
更多: 官方文档5.2 ActiveRecordPlugin