^_^ 搞事情~ 要趁早! demo生成器还不能满足你? 需要service 需要 Controller吗?
1~ 核心动力
package com.momathink.tpp; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import com.jfinal.kit.Kv; import com.jfinal.kit.PathKit; import com.jfinal.template.Engine; /*** * jfinal魔板引擎 * @author dufuzhong */ public class JfEnjoy { /** * 根据具体魔板生成文件 * @param templateFileName 模板文件名称 * @param kv 渲染参数 * @param filePath 输出目录 * @return */ public boolean render(String templateFileName, Kv kv, StringBuilder filePath) { BufferedWriter output = null; try { String baseTemplatePath = new StringBuilder(PathKit.getRootClassPath()) .append("/") .append(PathKit.getPackagePath(this)) .append("/tpl") .toString(); File file = new File(filePath.toString()); File path = new File(file.getParent()); if ( ! path.exists() ) { path.mkdirs(); } output = new BufferedWriter(new FileWriter(file)); Engine.use() .setBaseTemplatePath(baseTemplatePath) .getTemplate(templateFileName) .render(kv, output); return true; } catch (IOException e) { e.printStackTrace(); return false; }finally{ try { if( output != null ) output.close(); } catch (IOException e) {} } } /** * 根据自定义内容生成文件 * @param data 自定义内容 * @param filePath 输出目录 * @return */ public boolean render(String data, StringBuilder filePath) { BufferedWriter output = null; try { File file = new File(filePath.toString()); File path = new File(file.getParent()); if ( ! path.exists() ) { path.mkdirs(); } output = new BufferedWriter(new FileWriter(file)); output.write(data); return true; } catch (IOException e) { e.printStackTrace(); return false; }finally{ try { if( output != null ) output.close(); } catch (IOException e) {} } } }
2~ 业务开跑
package com.momathink.tpp; import java.util.List; import javax.sql.DataSource; import com.demo.common.model._JFinalDemoGenerator; import com.jfinal.kit.Kv; import com.jfinal.kit.PathKit; import com.jfinal.plugin.activerecord.dialect.MysqlDialect; import com.jfinal.plugin.activerecord.generator.DataDictionaryGenerator; import com.jfinal.plugin.activerecord.generator.MetaBuilder; import com.jfinal.plugin.activerecord.generator.TableMeta; /** * 代码生成器 * @author dufuzhong */ public class JfGenerator { public static final JfGenerator me = new JfGenerator(); protected final JfEnjoy jfEngine = new JfEnjoy(); protected Kv tablemetaMap = null; protected String packageBase = "com.momathink"; protected String srcFolder = "src"; protected String viewFolder = "_view"; protected String basePath = ""; public JfGenerator setPackageBase(String packageBase){ this.packageBase = packageBase; return this; } public JfGenerator setBasePath(String basePath){ this.basePath = basePath; return this; } public JfGenerator setSrcFolder(String srcFolder){ this.srcFolder = srcFolder; return this; } public JfGenerator setViewFolder(String viewFolder){ this.viewFolder = viewFolder; return this; } protected class DataGenerator extends DataDictionaryGenerator { public DataGenerator(DataSource dataSource, String dataDictionaryOutputDir) { super(dataSource, dataDictionaryOutputDir); } public void rebuildColumnMetas(List<TableMeta> tableMetas) { super.rebuildColumnMetas(tableMetas); } }; public TableMeta getTableMeta(String tableName){ if( tablemetaMap == null ){ DataSource dataSource = _JFinalDemoGenerator.getDataSource(); MetaBuilder metaBuilder = new MetaBuilder(dataSource); metaBuilder.setDialect(new MysqlDialect()); //metaBuilder.addExcludedTable(_JFinalDemoGenerator.excludedTable); List<TableMeta> tableMetas = metaBuilder.build(); new DataGenerator(dataSource, null).rebuildColumnMetas(tableMetas); if (tableMetas.size() == 0) { System.out.println("TableMeta 数量为 0,不生成任何文件"); return null; } Kv kv = Kv.create(); for (TableMeta tableMeta : tableMetas) { kv.set(tableMeta.name, tableMeta); } tablemetaMap = kv; } return (TableMeta) tablemetaMap.get(tableName); } /** * 生成手脚架代码 */ public JfGenerator allRender(String className, String tableName) { return javaRender(className, tableName).htmlRender(className, tableName); } /** * java 代码生成 * */ public JfGenerator javaRender(String className, String tableName) { //刷新 映射对象 _JFinalDemoGenerator.main(null); controller(className); validator(className); service(className , tableName); return this; } private String toClassNameSmall(String className) { return new StringBuffer(className.substring(0, 1).toLowerCase()).append(className.substring(1)).toString(); } private String toPackages(String className) { return new StringBuffer(packageBase).append(".").append(basePath) .append(".").append(className.toLowerCase()).toString(); } /** * 生成Controller * @param className 类名称 */ public void controller(String className){ String packages = toPackages(className); String classNameSmall = toClassNameSmall(className); jfEngine.render("/java/controller.html", Kv.by("package", packages) .set("className", className) .set("classNameSmall", classNameSmall) .set("basePath", basePath ) , new StringBuilder() .append(System.getProperty("user.dir")) .append("/") .append(srcFolder) .append("/") .append(packages.replace(".", "/")) .append("/") .append(className) .append("Controller.java") ); } /** * 生成validator * @param className 类名称 */ public void validator(String className){ String packages = toPackages(className); String classNameSmall = toClassNameSmall(className); jfEngine.render("/java/validator.html", Kv.by("package", packages) .set("className", className) .set("classNameSmall", classNameSmall) , new StringBuilder() .append(System.getProperty("user.dir")) .append("/") .append(srcFolder) .append("/") .append(packages.replace(".", "/")) .append("/") .append(className) .append("Validator.java") ); } /** * 生成Service * @param className 类名称 * @param tableName 表名 */ public void service(String className, String tableName){ String packages = toPackages(className); String classNameSmall = toClassNameSmall(className); jfEngine.render("/java/service.html", Kv.by("package", packages) .set("className", className) .set("classNameSmall", classNameSmall) .set("tableName", tableName) , new StringBuilder() .append(System.getProperty("user.dir")) .append("/") .append(srcFolder) .append("/") .append(packages.replace(".", "/")) .append("/") .append(className) .append("Service.java") ); } /** * @param className * @param tableName * */ public JfGenerator htmlRender(String className, String tableName){ TableMeta tablemeta = getTableMeta(tableName); htmlList(className, tablemeta); return this; } //页面的生成一般定制比较多..就来个简单的吧 public void htmlList(String className, TableMeta tablemeta){ String packages = toPackages(className); String classNameSmall = toClassNameSmall(className); String basePathUrl = basePath.replace('.', '/'); jfEngine.render("/html/list.html", Kv.by("tablemeta", tablemeta) .set("package", packages) .set("className", className) .set("classNameSmall", classNameSmall) .set("basePath", basePathUrl ) , new StringBuilder() .append(PathKit.getWebRootPath()) .append("/") .append(viewFolder) .append("/") .append(basePathUrl) .append("/") .append(classNameSmall) .append("/") .append(classNameSmall) .append("List.html") ); } // ... 继续扩展吧~ }
3~ 爽的不要不要~ 放两三个模版事例吧... 我这边的模版和业务关联的比较多了, 几乎没什么通用的了,,,, 自己业务扩展吧~
package #(package); import com.jfinal.aop.Before; import com.jfinal.core.Controller; import com.demo.common.model.#(className); /** * #(className) 管理 * 描述: * */ public class #(className)Controller extends Controller { //private static final Log log = Log.getLog(#(className)Controller.class); static #(className)Service srv = #(className)Service.me; /** * 列表 * /#(basePath)/#(classNameSmall)/list */ public void list() { setAttr("page", srv.paginate(getParaToInt("p", 1), 40)); render("#(classNameSmall)List.html"); } /** * 准备添加 * /#(basePath)/#(classNameSmall)/add */ public void add() { render("#(classNameSmall)Add.html"); } /** * 保存 * /#(basePath)/#(classNameSmall)/save */ @Before({#(className)Validator.class}) public void save() { srv.save(getModel(#(className).class)); renderJson("isOk", true); } /** * 准备更新 * /#(basePath)/#(classNameSmall)/edit */ public void edit() { #(className) #(classNameSmall) = srv.findById(getParaToInt("id")); setAttr("#(classNameSmall)", #(classNameSmall)); render("#(classNameSmall)Edit.html"); } /** * 更新 * /#(basePath)/#(classNameSmall)/update */ @Before(#(className)Validator.class) public void update() { srv.update(getModel(#(className).class)); renderJson("isOk", true); } /** * 查看 * /#(basePath)/#(classNameSmall)/view */ public void view() { #(className) #(classNameSmall) = srv.findById(getParaToInt("id")); setAttr("#(classNameSmall)", #(classNameSmall)); render("#(classNameSmall)View.html"); } /** * 删除 * /#(basePath)/#(classNameSmall)/delete */ public void delete() { srv.delete(getParaToInt("id")); renderJson("isOk", true); } }
package #(package); import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.Page; import com.demo.common.model.#(className); /** * #(className) 管理 * 描述: */ public class #(className)Service { //private static final Log log = Log.getLog(#(className)Service.class); public static final #(className)Service me = new #(className)Service(); private final #(className) dao = new #(className)().dao(); /** * 列表-分页 */ public Page<#(className)> paginate(int pageNumber, int pageSize) { return dao.paginate(pageNumber, pageSize, "SELECT * ", "FROM #(tableName)"); } /** * 保存 */ public void save(#(className) #(classNameSmall)) { #(classNameSmall).save(); } /** * 更新 */ public void update(#(className) #(classNameSmall)) { #(classNameSmall).update(); } /** * 查询 */ public #(className) findById(int #(classNameSmall)Id) { return dao.findFirst("select * from #(tableName) where id=?", #(classNameSmall)Id); } /** * 删除 */ public void delete(int #(classNameSmall)Id) { Db.update("delete from #(tableName) where id=?", #(classNameSmall)Id); } }
// 其他模版就不放了, 自己扩展吧~~
再放个页面的吧 , 比较有代表性
/tpl/html/list.html
#("#")@adminLayout() #("#")define main() <div class="admin-content-box"> <!-- 列表 --> <div class="jf-admin-panel-box"> <div> <button class="layui-btn" onclick="showFancyBox('/#(basePath)/#(classNameSmall)/add">添加#(tablemeta.remarks)</button> </div> <table class="layui-table" lay-skin="line"> <thead> <tr> #for(x : tablemeta.columnMetas) <th>#(x.remarks)</th> #end <th>操作</th> </tr> </thead> <tbody> #("#")for(x : page.list) <tr> #for(x : tablemeta.columnMetas) <td>#("#")(x.#(x.name))</td> #end <td> <span onclick="showFancyBox('/#(basePath)/#(classNameSmall)/view?id=#("#")(x.id)')">查看</span> <span onclick="showFancyBox('/#(basePath)/#(classNameSmall)/edit?id=#("#")(x.id)')">更新</span> <span onclick="showFancyBox('/#(basePath)/#(classNameSmall)/delete?id=#("#")(x.id)')">删除</span> </td> </tr> #("#")end </tbody> </table> #("#")@paginate(accountPage.pageNumber, accountPage.totalPage, "/#(basePath)/#(classNameSmall)/list?p=") </div> </div> #("#")end #("#")define css() #("#")end #("#")define js() <script type="text/javascript"> </script> #("#")end
这个功能包建在外面, 再Build Path进去, 算是可插拔哦~
码云:
https://git.oschina.net/bean80/tpp.git
public static void main(String[] args) {
String className = "Blog";
String tableName = "blog";
JfGenerator.me
.setPackageBase("com")
.setBasePath("demo")
//.setViewFolder("")
.javaRender(className, tableName)
.htmlRender(className, tableName);
System.out.println("---------OK-刷新一下项目吧---------");
}
}