如果题,当 _MappingKit.java 与Model不在同一级目录时,生成的_MappingKit.java 文件会提示需要引包。
//指定model的包名 String modelPackageName = "com.demo.common.model"; //指定_MappingKit.java的包名 String mappingKitPackageName = "com.demo.utils"; //指定 MappingKitOutputDir String mappingKitOutputDir = ROOT_PATH + "com/demo/utils"; Generator generator = new Generator(dataSource, baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir); generator.setMappingKitOutputDir(mappingKitOutputDir); generator.setMappingKitPackageName(mappingKitPackageName);
我知道,像我这样,喜欢把model跟util分开的人肯定很少。不过,还是提出来,跟大伙探讨一下。
一些想法涉及到源码修改,欢迎指正!
1.修改 MappingKitGenerator 类如下
//在 com.jfinal.plugin.activerecord.generator.MappingKitGenerator 类中新增一个属性 //该属性可以重载一个构造函数进行赋值 protected String baseModelPackageName; //在generate方法中注入baseModelPackageName public void generate(List<TableMeta> tableMetas) { System.out.println("Generate MappingKit file ..."); System.out.println("MappingKit Output Dir: " + this.mappingKitOutputDir); Kv data = Kv.by("mappingKitPackageName", this.mappingKitPackageName); data.set("mappingKitClassName", this.mappingKitClassName); data.set("tableMetas", tableMetas); //设置 baseModelPackageName data.set("baseModelPackageName", this.baseModelPackageName); String ret = this.engine.getTemplate(this.template).renderToString(data); this.writeToFile(ret); }
2.修改 mapping_kit_template.jf 模板如下
package #(mappingKitPackageName); import com.jfinal.plugin.activerecord.ActiveRecordPlugin; //在此处设置并引入model包 import #(baseModelPackageName).*; /** * Generated by JFinal, do not modify this file. test * <pre> * Example: * public void configPlugin(Plugins me) { * ActiveRecordPlugin arp = new ActiveRecordPlugin(...); * #(mappingKitClassName).mapping(arp); * me.add(arp); * } * </pre> */ public class #(mappingKitClassName) { public static void mapping(ActiveRecordPlugin arp) { #for (tableMeta : tableMetas) #if (tableMeta.primaryKey.contains(",")) // Composite Primary Key order: #(tableMeta.primaryKey) #end arp.addMapping("#(tableMeta.name)", "#(tableMeta.primaryKey)", #(tableMeta.modelName).class); #end } }
项目:JFinal