版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fancheng614/article/details/81037797
本代码生成器只生成了实体类和一些service文件。
这个代码生成器其实就是使用Java读取txt文件,然后根据txt文件里面的字段生成想要的Java文件。
直接创建相应的txt文件,运行Java文件,就可以生成要想的实体类和service文件。
代码生成器代码:
package com.mfc.read;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadTxt {
public static void main(String[] args) {
String fileName = "D:\\test.txt";
File file1 = new File("D:\\motion\\entity");
File file2 = new File("D:\\motion\\service");
File file3 = new File("D:\\motion\\serviceImpl");
file1.mkdirs();
file2.mkdirs();
file3.mkdirs();
readFileByLines(fileName);
}
/**
* @param fileName 文件完整路径
* 读取txt文件
*/
private static void readFileByLines(String fileName){
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
//生成实体类
getEntity(reader);
reader.close();
} catch (Exception e) {
e.printStackTrace();
} finally{
if(reader != null)
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* 生成实体类
* */
private static void getEntity(BufferedReader reader) throws IOException{
String tempString = null;
String stringEntity = "";
String className = "";
String stringService = "";
String stringServiceImpl = "";
int line = 1;
List<String> attribute = new ArrayList<String>();
List<String> attributeName = new ArrayList<String>();
while((tempString = reader.readLine()) != null){
if(!"end".equals(tempString)){
if(tempString.indexOf(" ") == -1){
stringEntity += "public Class "+tempString + "{\r\n";
stringService += "public interface " + tempString + "Service extends BaseService<"+tempString+"> {\r\n";
stringService += "}";
stringServiceImpl += "public class " + tempString + "ServiceImpl extends BaseServiceImpl<"+tempString+"> implements "+tempString+"Service {\r\n";
stringServiceImpl += "}";
className = tempString;
}else{
String[] temp = tempString.split(" ");
stringEntity += " //" + temp[2] + "\r\n";
stringEntity += " @Column(value=\""+temp[3]+"\", length="+temp[4]+")\r\n";
stringEntity += " private " + temp[0] + " " + temp[1] + ";\r\n";
attribute.add(temp[1]);
attributeName.add(temp[0]);
}
}else{
for (int i = 0; i < attribute.size(); i++) {
String newAttr = toUpperCaseFirstOne(attribute.get(i));
stringEntity += " public void get" + newAttr + "(){\r\n";
stringEntity += " return "+attribute.get(i)+";\r\n";
stringEntity += " }\r\n";
stringEntity += " public void set" + newAttr + "("+attributeName.get(i)+" "+attribute.get(i)+"){\r\n";
stringEntity += " this. " + attribute.get(i) + " = " + attribute.get(i) + ";\r\n";
stringEntity += " }\r\n";
}
stringEntity += "}";
saveStringToTxt(stringEntity, "D:\\motion\\entity\\"+className+".java");
saveStringToTxt(stringService, "D:\\motion\\service\\"+className+"Service.java");
saveStringToTxt(stringServiceImpl, "D:\\motion\\serviceImpl\\"+className+"ServiceImpl.java");
stringService = "";
stringServiceImpl = "";
stringEntity = "";
attribute = new ArrayList<String>();
attributeName = new ArrayList<String>();
}
line++;
}
}
/**
* @param string 要保存的字符串
* @param filePath 保存路径
*/
private static void saveStringToTxt(String string,String filePath){
try {
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(string.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//首字母转大写
public static String toUpperCaseFirstOne(String s){
if(Character.isUpperCase(s.charAt(0))){
return s;
}else{
return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
}
}
}
txt文件样板:
Student
String id 主键 id 40
String name 姓名 name 100
end
teacher
String id 主键 id 40
String tName 姓名 t_name 100
String address 地址 address 100
String add 添加属性 add 100
end