package com.mindant.generator.action; import cn.hutool.db.Db; import cn.hutool.db.Entity; import cn.hutool.db.ds.simple.SimpleDataSource; import com.mindant.generator.action.config.WebGeneratorConfig; import com.mindant.generator.action.model.GenQo; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * 代码生成器,可以生成实体,dao,service,controller,html,js * * @author stylefeng * @Date 2017/5/21 12:38 */ public class GunsCodeBatchGenerator { public static void main(String[] args) throws Exception { // 数据库名 String dbName = "xlzx"; // 数据库连接 String url = "jdbc:mysql://127.0.0.1:3306/" + dbName + "?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true"; // 用户名 String userName = "root"; // 密码 String password = "123456"; // 查询数据库表名、注释 String tableSql = "SELECT TABLE_NAME AS tableName,TABLE_COMMENT AS tableComment FROM information_schema.`TABLES` WHERE TABLE_SCHEMA = '" + dbName + "'" + " AND TABLE_NAME NOT LIKE 'sys_%'" + " ORDER BY TABLE_NAME ASC"; // 创建数据源 SimpleDataSource ds = new SimpleDataSource(url, userName, password); // 使用数据源连接数据库 Db db = Db.use(ds); List list = db.query(tableSql); // 查询表名、注释 LinkedHashMap tableCommentMap = list.stream().collect(Collectors.toMap(e -> e.getStr("tableName"), e -> e.getStr("tableComment"), (e1, e2) -> e1, LinkedHashMap::new)); // 执行SQL查询 for (String k : tableCommentMap.keySet()) { // 打印数据表名 System.out.println("\"" + k + "\","); // System.out.println(k + ":" + tableCommentMap.get(k)); } String[] tableNameArray = new String[]{ // "hr_org_approver", // "hr_organization", // "hr_position", "t_version" }; for (String tableName : tableNameArray) { genTableModel(dbName, url, userName, password, tableName, tableCommentMap); } } public static void genTableModel(String dbName, String url, String userName, String password, String tableName, Map tableCommentMap) { /* * guns的生成器: * guns的代码生成器可以生成controller,html页面,页面对应的js */ // GunsGeneratorConfig gunsGeneratorConfig = new GunsGeneratorConfig(); // gunsGeneratorConfig.doMpGeneration(); // gunsGeneratorConfig.doGunsGeneration(); // 代码生成的查询参数 GenQo genQo = new GenQo(); //数据库url genQo.setUrl(url); // 数据库用户名 genQo.setUserName(userName); // 数据库密码 genQo.setPassword(password); // 表名 genQo.setTableName(tableName); // 作者 genQo.setAuthor("luodangjia"); // 数据库名 genQo.setDbName(dbName); // 项目包 genQo.setProjectPackage("cn.stylefeng.guns"); genQo.setModuleName("business"); // 代码生成位置 genQo.setProjectPath("D:\\study\\PsychologicalCounseling\\common-buiness"); // 用户信息 genQo.setBizName(tableCommentMap.get(tableName)); genQo.setControllerSwitch(true); genQo.setEntitySwitch(true); genQo.setServiceSwitch(true); genQo.setDaoSwitch(true); genQo.setIgnoreTabelPrefix("t_"); WebGeneratorConfig webGeneratorConfig = new WebGeneratorConfig(genQo); webGeneratorConfig.doMpGeneration(); webGeneratorConfig.doGunsGeneration(); } }