好友
阅读权限10
听众
最后登录1970-1-1
|
本帖最后由 GrootYu 于 2021-10-13 15:33 编辑
非常老的一个框架,开发起来非常困难,写了个通用基础代码生成的工具,其实就是字符串的各种替换。
每天都敲一样的代码,真的很烦。
源码跟jar包都放上去了,有兴趣的可以看看,就是写着玩的。
[Java] 纯文本查看 复制代码 public static void main(String[] args) throws Exception {
String packagePath = "com.wellhope.cms.aaaa";
String tableName = "CMS_COLUMN_INFO";
String beanName = "ColumnInfo";
String basePath = "C:\\Users\\Groot\\Desktop\\export\\"+packagePath;
List<ItemInfo> list = new ArrayList<ItemInfo>();
ItemInfo item1 = new ItemInfo();
item1.setColumnCode("COLUMN_CODE");
item1.setColumnName("目录编码");
list.add(item1);
ItemInfo item2 = new ItemInfo();
item2.setColumnCode("COLUMN_NAME");
item1.setColumnName("目录名称");
list.add(item2);
ItemInfo item3 = new ItemInfo();
item3.setColumnCode("ID");
item3.setColumnName("目录ID");
list.add(item3);
for(ItemInfo item:list){
String columnCode = item.getColumnCode();
String columnName = item.getColumnName();
String isQry = item.getIsQry();
String isQryLike = item.getIsQryLike();
String isDel = item.getIsDel();
boolean flag = true;
StringBuffer columnCodebuffer = new StringBuffer(columnCode.toLowerCase());
while(flag){
if(columnCodebuffer.indexOf("_")>0){
int i = columnCodebuffer.indexOf("_");
String s = columnCodebuffer.substring(i+1, i+2).toUpperCase();
columnCodebuffer = columnCodebuffer.replace(i, i+2,s);
}else{
item.setAttrCode(columnCodebuffer.toString());
System.out.println(columnCodebuffer);
flag = false;
}
}
}
createHandler(list,beanName,basePath,packagePath);
createPageBean(list,beanName,basePath,packagePath);
createInfo(list,beanName,basePath,packagePath);
createHome(list,beanName,basePath,packagePath);
createConnector(list,beanName,basePath,tableName,packagePath);
}
public static void create(String packagePath,String tableName,String beanName,String basePath,List<ItemInfo> list) throws Exception{
for(ItemInfo item:list){
String columnCode = item.getColumnCode();
boolean flag = true;
StringBuffer columnCodebuffer = new StringBuffer(columnCode.toLowerCase());
while(flag){
if(columnCodebuffer.indexOf("_")>0){
int i = columnCodebuffer.indexOf("_");
String s = columnCodebuffer.substring(i+1, i+2).toUpperCase();
columnCodebuffer = columnCodebuffer.replace(i, i+2,s);
}else{
item.setAttrCode(columnCodebuffer.toString());
System.out.println(columnCodebuffer);
flag = false;
}
}
}
basePath = basePath+File.separator+packagePath;
createHandler(list,beanName,basePath,packagePath);
createPageBean(list,beanName,basePath,packagePath);
createInfo(list,beanName,basePath,packagePath);
createHome(list,beanName,basePath,packagePath);
createUtil(list,beanName,basePath,packagePath);
createConnector(list,beanName,basePath,tableName,packagePath);
}
private static void createHandler(List<ItemInfo> list,String beanName,String basePath,String packagePath) throws Exception{
String javaContent = readFile("handler.json");
javaContent = javaContent.replaceAll("##BEAN_NAME##", beanName);
javaContent = javaContent.replaceAll("##packageName##", packagePath);
createFile(basePath+File.separator+"handlers"+File.separator+beanName+"Handler.java",javaContent);
}
private static void createPageBean(List<ItemInfo> list,String beanName,String basePath,String packagePath) throws Exception{
String javaContent = readFile("pageBean.json");
javaContent = javaContent.replaceAll("##BEAN_NAME##", beanName);
javaContent = javaContent.replaceAll("##packageName##", packagePath);
createFile(basePath+File.separator+"handlers"+File.separator+beanName+"PageBean.java",javaContent);
}
private static void createInfo(List<ItemInfo> list,String beanName,String basePath,String packagePath) throws Exception{
String javaContent = readFile("info.json");
String attrBlock = "";
for(ItemInfo info:list){
attrBlock += "private String "+info.getAttrCode()+";//"+info.getColumnName()+"";
attrBlock += "\r";
}
javaContent = javaContent.replaceAll("##BEAN_NAME##", beanName);
javaContent = javaContent.replaceAll("##packageName##", packagePath);
javaContent = javaContent.replaceAll("##attrBlock##", attrBlock);
createFile(basePath+File.separator+"domains"+File.separator+beanName+".java",javaContent);
}
private static void createHome(List<ItemInfo> list,String beanName,String basePath,String packagePath) throws Exception{
String javaContent = readFile("home.json");
String getParamBlock = "";
for(ItemInfo info:list){
getParamBlock += "String "+info.getAttrCode()+" = qryParam.get(\""+info.getAttrCode()+"\");";
getParamBlock += "\r";
}
String setParamBlock = "";
for(ItemInfo info:list){
if("id".equals(info.getAttrCode())){
setParamBlock += "info.setId(BaseUtil.getRealUUID());";
setParamBlock += "\r";
continue;
}
setParamBlock += "info.set"+topUp(info.getAttrCode())+"("+info.getAttrCode()+");";
setParamBlock += "\r";
}
javaContent = javaContent.replaceAll("##BEAN_NAME##", beanName);
javaContent = javaContent.replaceAll("##packageName##", packagePath);
javaContent = javaContent.replaceAll("##getParamBlock##", getParamBlock);
javaContent = javaContent.replaceAll("##setParamBlock##", setParamBlock);
createFile(basePath+File.separator+"domains"+File.separator+beanName+"Home.java",javaContent);
}
private static void createConnector(List<ItemInfo> list,String beanName,String basePath,String tableName,String packagePath) throws Exception{
String javaContent = readFile("connector.json");
String dataListSql = "String sql = \"SELECT T1.* FROM "+tableName+" T1 WHERE 1=1 \";";
String dataCountSql = "String sql = \"SELECT COUNT(1) C FROM "+tableName+" T1 WHERE 1=1 \";";
String getParamBlock = "";
for(ItemInfo info:list){
getParamBlock += "String "+info.getAttrCode()+" = qryParam.get(\""+info.getAttrCode()+"\");";
getParamBlock += "\r";
}
String setParamBlock = "";
for(ItemInfo info:list){
setParamBlock += "info.set"+topUp(info.getAttrCode())+"(rs.getString(\""+info.getColumnCode()+"\"));";
setParamBlock += "\r";
}
String qryOneSql = "String sql = \"SELECT T1.* FROM "+tableName+" T1 WHERE 1=1 \";";
for(ItemInfo info:list){
if("1".equals(info.getIsQry())){
qryOneSql += "\r";
qryOneSql += "if(BaseUtil.isNotEmpty("+info.getAttrCode()+")){";
qryOneSql += "\r";
qryOneSql += "sql += \" AND T1."+info.getColumnCode()+"='\"+"+info.getAttrCode()+"+\"' \";";
qryOneSql += "\r";
qryOneSql += "}";
}
}
String insertSql = "String sql = \"INSERT INTO " + tableName + "( \"";
for(ItemInfo info:list){
insertSql += "\r";
insertSql += "+\" "+info.getColumnCode()+",\"";
}
insertSql = cutLastStr(insertSql,",");
insertSql += "\r";
insertSql += "+\")VALUES( \"";
for(ItemInfo info:list){
insertSql += "\r";
insertSql += "+\" '\"+info.get"+topUp(info.getAttrCode())+"()+\"',\"";
}
insertSql = cutLastStr(insertSql,",");
insertSql += "\r";
insertSql += "+\")\";";
String deleteSql = "String sql = \"DELETE FROM "+tableName+" WHERE ID IN( '\"+ids+\"')\";";
javaContent = javaContent.replaceAll("##BEAN_NAME##", beanName);
javaContent = javaContent.replaceAll("##packageName##", packagePath);
javaContent = javaContent.replaceAll("##getParamBlock##", getParamBlock);
javaContent = javaContent.replaceAll("##dataListSql##", dataListSql);
javaContent = javaContent.replaceAll("##setParamBlock##", setParamBlock);
javaContent = javaContent.replaceAll("##dataCountSql##", dataCountSql);
javaContent = javaContent.replaceAll("##qryOneSql##", qryOneSql);
javaContent = javaContent.replaceAll("##insertSql##", insertSql);
javaContent = javaContent.replaceAll("##deleteSql##", deleteSql);
createFile(basePath+File.separator+"connectors"+File.separator+beanName+"Connector.java",javaContent);
}
private static void createUtil(List<ItemInfo> list,String beanName,String basePath,String packagePath) throws Exception{
String javaContent = readFile("util.json");
javaContent = javaContent.replaceAll("##BEAN_NAME##", beanName);
javaContent = javaContent.replaceAll("##packageName##", packagePath);
createFile(basePath+File.separator+"common"+File.separator+"BaseUtil.java",javaContent);
}
public static void createFile(String filePath,String content) throws Exception{
File jsonFile = new File(filePath);
File parentFile = jsonFile.getParentFile();
if (null != parentFile && !parentFile.exists()) {
parentFile.mkdirs(); // 创建文件夹
}
jsonFile.createNewFile();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8"));
// char[] charArray = content.toCharArray();
// char flag = '\r';
// for (int j = 0; j < charArray.length; j++) {
// if(charArray[j] == flag){
// bufferedWriter.write("\r");
// }else{
// bufferedWriter.write(charArray[j]);
// }
// }
bufferedWriter.write(content);
bufferedWriter.flush();
bufferedWriter.close();
}
public static String readFile(String jsonFileName) throws Exception {
// System.out.println(Main.class.getClassLoader().getResource("").getPath());
// System.out.println(Main.class.getClassLoader().getResource("").getPath()+"tools/main/json/");
// File file = new File(Main.class.getClassLoader().getResource("").getPath()+"tools/main/json/"+jsonFileName);
// String encoding = "UTF-8";
// StringBuilder builder = new StringBuilder();
// FileInputStream fis = null;
// InputStreamReader isr = null;
// BufferedReader br = null;
// try {
// fis = new FileInputStream(file);
// isr = new InputStreamReader(fis, encoding);
// br = new BufferedReader(isr);
// String line = null;
// while ((line = br.readLine()) != null) {
// builder.append("\n");
// builder.append(line);
// }
// } catch (Exception e) {
// } finally {
// if (br != null) {
// br.close();
// }
// if (isr != null) {
// isr.close();
// }
// if (fis != null) {
// fis.close();
// }
// }
// String testString = Main.class.getClassLoader().getResource("").getPath()+"tools/main/json/";
InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("tools/main/json/"+jsonFileName);
String jsonStr = inputStreamToString(inputStream);
return jsonStr;
// return testString+"@@@@@@@@@@@@@@@@@"+jsonStr+"########################"+builder.toString();
}
private static String inputStreamToString(InputStream inputStream) {
StringBuffer buffer = new StringBuffer();
InputStreamReader inputStreamReader;
try {
inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append("\n");
buffer.append(str);
}
// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return buffer.toString();
}
//首字母大写
private static String topUp(String str){
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
private static String cutLastStr(String str,String a){
return new StringBuffer(str).replace(str.lastIndexOf(a), str.lastIndexOf(a)+1, "").toString();
}
|
免费评分
-
参与人数 1 | 吾爱币 +7 |
热心值 +1 |
收起
理由
|
苏紫方璇
| + 7 |
+ 1 |
欢迎分析讨论交流,吾爱破解论坛有你更精彩! |
查看全部评分
|