package net.lhcredit.framework.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentMap;
/*
* @time 2018/5/31 16:13
* @author lrs 快速生成代码工具类
* @description
**/
public class FastUtil {
public static String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
// public static String url = "jdbc:mysql://192.168.3.201:3306/lh-outer-api?useUnicode=true&characterEncoding=utf-8";
public static String user = "root";
public static String password = "root";
/**
* 获取数据库链接
*/
public static Connection getConnection(String url,String user,String password)throws Exception{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");// 加载驱动
conn = DriverManager.getConnection(url, user, password);//获取连接
return conn;
}
/**
* 关闭数据库链接
*/
public static List<JSONObject> colseConnection(PreparedStatement pst,ResultSet ret,List<JSONObject> lj)throws Exception{
pst.close();
ret.close();
return lj;
}
/**
* 表名称与表备注获取
* @param conn
* @return
*/
public static List<JSONObject> getTableName(Connection conn)throws Exception{
List<JSONObject> lj = new ArrayList<JSONObject>();
PreparedStatement pst = null;
ResultSet ret = null;
String sql = "SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.tables WHERE table_schema='test' AND table_type='base table';";
pst = FastUtil.getConnection(url,user,password).prepareStatement(sql);//准备执行语句
ret = pst.executeQuery();//执行语句,得到结果集
while (ret.next()){
JSONObject jo = new JSONObject();
String tableName = ret.getString(1);
// 判断代码生成方式,0为跳过某些表,1为专门生成某些表
// if("0".equals(ConstantUtil.getValue("createCodeType"))){// 跳过某些表
// if(tableName.contains("sys_") || tableName.contains("excel_") || tableName.contains("front_") || ConstantUtil.getValue("sysTableName").contains(tableName)){
// continue;
// }
// }else if("1".equals(ConstantUtil.getValue("createCodeType"))){// 专门生成某些表
// if(!ConstantUtil.getValue("createTableName").contains(tableName)){
// continue;
// }
// }
jo.put("tableName",ret.getString(1));
jo.put("tableComment",ret.getString(2));
lj.add(jo);
}
return colseConnection(pst,ret,lj);
}
/**
* 获取列信息
*/
public static List<JSONObject> getColumns(Connection conn,String sql)throws Exception{
List<JSONObject> lj = new ArrayList<JSONObject>();
PreparedStatement pst = null;
ResultSet ret = null;
pst = FastUtil.getConnection(url,user,password).prepareStatement(sql);//准备执行语句
ret = pst.executeQuery();//执行语句,得到结果集
while (ret.next()){
JSONObject jo = new JSONObject();
jo.put("column_name",ret.getString(1));
String dataType = ret.getString(2);
if("decimal".equals(dataType)){
dataType = "double";
}else if("datetime".equals(dataType)){
dataType = "Date";
}else{
//if("varchar".equals(dataType) || "mediumtext".equals(dataType))
dataType = "String";
}
jo.put("data_type",dataType);
jo.put("column_comment",ret.getString(3));
lj.add(jo);
}
return colseConnection(pst,ret,lj);
}
public static List<JSONObject> getTbaleColumn(String tableName)throws Exception{
List<JSONObject> jsonObjectList = new ArrayList<>();
String sql = "SELECT column_name,data_type,column_comment FROM information_schema.columns WHERE table_schema='test' AND table_name='"+tableName+"';";
return getColumns(FastUtil.getConnection(url,user,password),sql);
}
/**
* 一键生成最基础的pagemodel/domain/service/serviceImpl/dao/mapping
* 惊喜不惊喜?刺激不刺激?但你要是把目标URL写错了一个个删除,就更刺激了!!!
* @param targetUrl 目标URL,生成的文件存放地址
* @throws Exception
*/
public static void createFiles(String targetUrl)throws Exception{
List<JSONObject> tableNames = getTableName(FastUtil.getConnection(url,user,password));
System.out.println("开始生成文件");
int j = 0;
for (Object o : tableNames) {
JSONObject jo = (JSONObject)o;
String tableName = jo.getString("tableName");// 表名称
String tableComment = jo.getString("tableComment");// 表注释
String pageModelName = "";// pageModel名称
for (String str : tableName.split("_")) {
pageModelName += str.substring(0,1).toUpperCase()+str.substring(1);
}
ConcurrentMap
j++;
// 生成contrller
createFile(pageModelName,tableName,tableComment,"Controller",targetUrl+"controller/");
// // 生成pagemodel
createFile(pageModelName,tableName,tableComment,"PageModel",targetUrl+"pagemodel/");
// 生成domain
// createFile(pageModelName,tableName,tableComment,"Domain",targetUrl+"domain/");
// // 生成dao
// createFile(pageModelName,tableName,tableComment,"Dao",targetUrl+"dao/");
// // 生成service
// createFile(pageModelName,tableName,tableComment,"Service",targetUrl+"service/");
// // 生成serviceImpl
// createFile(pageModelName,tableName,tableComment,"ServiceImpl",targetUrl+"service/impl/");
// // 生成mapping
// createFile(pageModelName,tableName,tableComment,"Mapping",targetUrl+"mapping/");
}
System.out.println("共计生成"+j+"套文件");
}
/**
* 创建文件
* @param pageModelName 文件名
* @param tableName 表名称
* @param tableComment 表注释
* @param sourceFileName 模板文件
* @param targetUrl 目标文件路径
* @throws Exception 异常
*/
public static void createFile(String pageModelName,String tableName,String tableComment,String sourceFileName,String targetUrl) throws Exception{
// 准备数据
List<JSONObject> columns = getTbaleColumn(tableName);
JSONObject jsonObject = new JSONObject();
jsonObject.put("pageModelName",pageModelName);
String className = pageModelName;
if("Domain".equals(sourceFileName)){
className = "T"+pageModelName;
}else if("Dao".equals(sourceFileName)){
className = pageModelName+"Dao";
}else if("Service".equals(sourceFileName)){
className = pageModelName+"Service";
}else if("ServiceImpl".equals(sourceFileName)){
className = pageModelName+"ServiceImpl";
}else if("Controller".equals(sourceFileName)){
className = pageModelName+"V1_1Controller";
}
jsonObject.put("className",className);
jsonObject.put("tableName",tableName);
jsonObject.put("valName",className.substring(0,1).toLowerCase()+className.substring(1));
jsonObject.put("tableComment",tableComment);