2018/7/10
一.数据库关系模式讨论:
经过讨论,
1.新增message 表和修改一些表的属性
二.自动化生成 get , set function
package com.createClass;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
/*
* 只需创建一个类,里面声明private变量
* 每一个private变量为一行,即private int num;形式
* 不用写set get方法
* 调用次方法,即可自动在文件中生成 public get set 方法
* 调用Createclass 的readfile方法即可实现对单个文件的修改
*/
public class CreateClass {
private String className;
private ArrayList<String> properties; //每一条都是public [static] String abc 格式,需要一个解析函数
private String fileName; //java文文件路径
private ArrayList<String> packages;
private String packageName;// 该类所在的 package ;类似于 com.package 的形式
public CreateClass() {
// TODO Auto-generated constructor stub
properties = new ArrayList<String>();
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//
File f = new File("F:\\MyFiles\\myeclipseWorkspace\\LastTest\\src\\com\\dbmap");
String[] fl = f.list();
for (String fn:fl){
CreateClass cc = new CreateClass();
//cc.properties.add("private String str1");
//cc.properties.add("public int num");
cc.readFile("F:\\MyFiles\\myeclipseWorkspace\\LastTest\\src\\com\\dbmap\\"+fn);
System.out.println("&"+fn+"&");
for (String s:cc.properties){
System.out.println("%"+s+"%");
}
}
//cc.create("e:/Test.java");
//
// String str=" boo: and:foo ";
// String []re = str.split(" ");
// System.out.println(re.length);
// for (String s:re){
// System.out.println(s);
// }
//
}
public void writeFile(File file) throws IOException{
FileWriter fw = new FileWriter(file);
fw.write("package "+packageName+";\r\n\r\n");
fw.close();
}
private void importPacakges(FileWriter fw){
}
private void setTrivialFunctions(FileWriter fw) throws IOException{
for (String property : properties){
String[]compoments = parseProperty(property);
String propertyName = compoments[compoments.length-1];
String propertyType = compoments[compoments.length-2];
String getFunction="\tpublic "
+ propertyType +" "
+"get"
+(propertyName.charAt(0)+"").toUpperCase()+propertyName.substring(1)
+"(){\r\n"
+"\t\treturn this."+propertyName+";\r\n"
+"\t}\r\n";
String setFunction="\tpublic "
+"void "
+"set"
+(propertyName.charAt(0)+"").toUpperCase()+propertyName.substring(1)
+"("+propertyType+" "+propertyName+"){\r\n"
+"\t\tthis."+propertyName+" = "+propertyName+";\r\n"
+"\t}\r\n";
fw.write(getFunction);
fw.write(setFunction);
}
}
//返回 属性的各个成分,在setTrivial里用到的是最后一个(变量名)成分 和倒数第二个(变量类型)
private String [] parseProperty (String property){
return property.split(" ");
}
public void create(String filename) throws IOException{
fileName=filename;
File javafile = new File(fileName);
if(!javafile.exists())
javafile.createNewFile();
FileWriter fw = new FileWriter(javafile);
setTrivialFunctions(fw);
fw.close();
}
//输入文件名filename的没有定义get set 函数的文件
//输出 文件名为filename 的定义了set get 函数的文件
public void readFile(String fileName) throws IOException{
File javafile = new File(fileName);
BufferedReader br = new BufferedReader(new FileReader(javafile));
String line=null;
int kuohao_count = 0;
boolean firstkuohao = true;
String content = "";
//遍历查找所有private变量并且将class的最后一个大括号之前的文本记录下来存在content里面
while((line=br.readLine())!=null){
if(line.indexOf('{')!=-1)
{
kuohao_count++;
if(firstkuohao)firstkuohao=false;
}
if(line.indexOf('}')!=-1)kuohao_count--;
if(kuohao_count>=1||firstkuohao)
content+=line+"\r\n";
if(line.indexOf("private")!=-1){
if(line.indexOf('(')==-1&&line.indexOf(';')!=-1){
int i = 0;
for (;i<line.length();i++){
if(line.charAt(i)>='a'&&line.charAt(i)<='z'||line.charAt(i)>='A'&&line.charAt(i)<='Z')break;
}
line = line.substring(i);
//去掉注释
for (;i<line.length();i++){
if(line.charAt(i)=='/')break;
}
line = line.substring(0,i);
String property = line.replaceAll("\t\t", "\t").replaceAll("\t", " ").replaceAll(";", "").replaceAll(" ", " ");
properties.add(property);
}
}
}
br.close();
//File f = new File(fileName);
FileWriter fw = new FileWriter(javafile);
//首先把之前存的content写进文件,不做处理
fw.write(content);
//然后把所有要写的set,get方法写进文件
setTrivialFunctions(fw);
//最后给clas加上右括号
fw.write("}\r\n");
fw.close();
}
}
运行结果如图:
三.自动化生成hibernate mapping
package com.createClass;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
/*需要知道的是 类所在的绝对路径和src文件夹下的相对路径
* hibername 配置文件的路径
* xml妹子文件的父文件夹的绝对路径和在双src下的相对路径
*
*
*/
public class CreateDbmXML {
public CreateDbmXML() {
// TODO Auto-generated constructor stub
}
private String content = "";
private String end = "</hibernate-mapping>";
private String hibernatecfgContent = "";
private FileWriter hibernatecfgwriter;
public void process() throws IOException {
//
// String classFileName = "";
// BufferedReader br = new BufferedReader(new FileReader(classFileName));
//
// String line;
// String content = "";
//
// while ((line = br.readLine()) != null) {
//
// if (line.indexOf("<class") != -1)
// break;
// content += line + "\r\n";
//
// }
//
// br.close();
//以这个为模板对其他类进行映射
getModle("F:/MyFiles/myeclipseWorkspace/LastTest/src/dbmap/budget.dbm.xml");
//获取hibernatecfg文件的文本和输出流
gethibernatecfg("F:/MyFiles/myeclipseWorkspace/LastTest/src/hibernate.cfg.xml");
//获取要映射的java类所在的包
File f = new File("F:/MyFiles/myeclipseWorkspace/LastTest/src/Bean");
String[] l = f.list();
for (String s:l){
// 对每一个类进行处理
processFile("Bean", "F:/MyFiles/myeclipseWorkspace/LastTest/src/Bean/"+s, "F:/MyFiles/myeclipseWorkspace/LastTest/src/dbmap","dbmap");
}
//写上最后的结尾并关闭输出流
hibernatecfgwriter.write("</session-factory>\r\n");
hibernatecfgwriter.write("</hibernate-configuration>\r\n");
hibernatecfgwriter.close();
}
private FileWriter gethibernatecfg(String cfgpath) throws IOException{
BufferedReader br = new BufferedReader(new FileReader(cfgpath));
String line = "";
while((line = br.readLine())!=null){
if(line.indexOf("<mapping")!=-1)continue;
if(line.indexOf("</session-factory>")!=-1)break;
hibernatecfgContent+=line+"\r\n";
}
hibernatecfgwriter = new FileWriter(cfgpath);
hibernatecfgwriter.write(hibernatecfgContent);
return hibernatecfgwriter;
}
private void processFile(String packagepath,String filename,String xmlpath,String xmlrelativePath) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
// String content = "";
ArrayList<String>properties = new ArrayList<String>();
ArrayList<String>keys = new ArrayList<String>();
while ((line = br.readLine()) != null) {
if (line.indexOf("private") != -1){
String property = getPropertyByParseLine(line);
if(property.indexOf(" id")!=-1){
String key =( property.split(" "))[0];
keys.add(key);
}else
properties.add(property);
}
}
br.close();
//获取类名
String[] strs;
if(filename.indexOf("\\")!=-1)
strs = filename.split("/");
else{
strs = filename.split("/");
}
String classname = strs[strs.length-1].split(".java")[0];
String classnametoLower = classname.toLowerCase();
String xmlname = xmlpath+"\\"+classnametoLower+".xml";
String xmlrelativename =xmlrelativePath+"/"+ classnametoLower+".xml";
hibernatecfgwriter.write("<mapping resource=\""+xmlrelativename+"\" />\r\n");
FileWriter fw = new FileWriter(xmlname);
fw.write(content);
String classLine = "<class name=\""+packagepath+"."+classname+
"\" table=\""+classnametoLower+"\">\r\n";
/*
* <class name="Bean.School" table="school">
*/
fw.write(classLine);
addproperties(keys,properties,fw);
String classEnd = "</class>\r\n";
fw.write(classEnd);
fw.write(end);
fw.close();
}
private String getPropertyByParseLine(String line){
String id="";
int i = 0;
for (;i<line.length();i++){
if(line.charAt(i)=='/'){
id=" id";
break;
}
}
line = line.substring(0,i);
line = line.replaceAll("\t\t", "\t").replaceAll(";", "").replaceAll(" ", " ");
String str []= line.split(" " );
return str[str.length-1]+id;
}
private void addproperties(ArrayList<String> keys , ArrayList<String> properties , FileWriter fw) throws IOException{
if(keys.size()>1){
String idline = "<composite-id>\r\n";
fw.write(idline);
for (String key :keys){
String keyline="<key-property name=\""+key+"\"/>\r\n";
fw.write(keyline);
}
String idend = "</composite-id>\r\n";
fw.write(idend);
}
else{
String keyline="<id name=\""+keys.get(0)+"\"/>\r\n";
fw.write(keyline);
}
for (String p:properties){
String pstr = "<property name=\""+p+"\" />\r\n";
fw.write(pstr);
}
}
private void getModle(String modelFile) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(modelFile));
String line;
// String content = "";
while ((line = br.readLine()) != null) {
if (line.indexOf("<class") != -1)
break;
content += line + "\r\n";
}
br.close();
}
public static void main(String args[]) throws IOException{
new CreateDbmXML().process();
}
}
运行结果如图:
四.自动化生成用于进行数据库操作的class_dao文件
package com.createClass;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
/*需要知道的是 类所在的绝对路径和src文件夹下的相对路径
* hibername 配置文件的路径
* xml妹子文件的父文件夹的绝对路径和在双src下的相对路径
* 通过该类的process函数可以完成以下工作
* 1)根据Bean 里面的类的属性和后面注释是否为id
* 在相应的mapping配置文件里创建mapping
* 2)没创建一个mapping文件 , 就在hibernate.cfg.xml 配置文件
* 里导入入一条,即记录resourse 为该产生层的mapping配置文件
*
*/
public class CreateDbmXML {
public CreateDbmXML() {
// TODO Auto-generated constructor stub
}
private String content = "";
private String end = "</hibernate-mapping>";
private String hibernatecfgContent = "";
private FileWriter hibernatecfgwriter;
public void process() throws IOException {
getModle("F:/MyFiles/myeclipseWorkspace/LastTest/src/dbmap/budget.dbm.xml");
//获取hibernatecfg文件的文本和输出流
gethibernatecfg("F:/MyFiles/myeclipseWorkspace/LastTest/src/hibernate.cfg.xml");
//获取要映射的java类所在的包
File f = new File("F:/MyFiles/myeclipseWorkspace/LastTest/src/Bean");
String[] l = f.list();
for (String s:l){
// 对每一个类进行处理
processFile("Bean", "F:/MyFiles/myeclipseWorkspace/LastTest/src/Bean/"+s, "F:/MyFiles/myeclipseWorkspace/LastTest/src/dbmap","dbmap");
}
//写上最后的结尾并关闭输出流
hibernatecfgwriter.write("</session-factory>\r\n");
hibernatecfgwriter.write("</hibernate-configuration>\r\n");
hibernatecfgwriter.close();
}
private FileWriter gethibernatecfg(String cfgpath) throws IOException{
BufferedReader br = new BufferedReader(new FileReader(cfgpath));
String line = "";
while((line = br.readLine())!=null){
if(line.indexOf("<mapping")!=-1)continue;
if(line.indexOf("</session-factory>")!=-1)break;
hibernatecfgContent+=line+"\r\n";
}
hibernatecfgwriter = new FileWriter(cfgpath);
hibernatecfgwriter.write(hibernatecfgContent);
return hibernatecfgwriter;
}
private void processFile(String packagepath,String filename,String xmlpath,String xmlrelativePath) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
// String content = "";
ArrayList<String>properties = new ArrayList<String>();
ArrayList<String>keys = new ArrayList<String>();
while ((line = br.readLine()) != null) {
if (line.indexOf("private") != -1){
String property = getPropertyByParseLine(line);
if(property.indexOf(" id")!=-1){
String key =( property.split(" "))[0];
keys.add(key);
}else
properties.add(property);
}
}
br.close();
//获取类名
String[] strs;
if(filename.indexOf("\\")!=-1)
strs = filename.split("/");
else{
strs = filename.split("/");
}
String classname = strs[strs.length-1].split(".java")[0];
String classnametoLower = classname.toLowerCase();
String xmlname = xmlpath+"\\"+classnametoLower+".xml";
String xmlrelativename =xmlrelativePath+"/"+ classnametoLower+".xml";
hibernatecfgwriter.write("<mapping resource=\""+xmlrelativename+"\" />\r\n");
FileWriter fw = new FileWriter(xmlname);
fw.write(content);
String classLine = "<class name=\""+packagepath+"."+classname+
"\" table=\""+classnametoLower+"\">\r\n";
fw.write(classLine);
addproperties(keys,properties,fw);
String classEnd = "</class>\r\n";
fw.write(classEnd);
fw.write(end);
fw.close();
}
private String getPropertyByParseLine(String line){
String id="";
int i = 0;
for (;i<line.length();i++){
if(line.charAt(i)=='/'){
id=" id";
break;
}
}
line = line.substring(0,i);
line = line.replaceAll("\t\t", "\t").replaceAll(";", "").replaceAll(" ", " ");
String str []= line.split(" " );
return str[str.length-1]+id;
}
private void addproperties(ArrayList<String> keys , ArrayList<String> properties , FileWriter fw) throws IOException{
if(keys.size()>1){
String idline = "<composite-id>\r\n";
fw.write(idline);
for (String key :keys){
String keyline="<key-property name=\""+key+"\"/>\r\n";
fw.write(keyline);
}
String idend = "</composite-id>\r\n";
fw.write(idend);
}
else{
String keyline="<id name=\""+keys.get(0)+"\"/>\r\n";
fw.write(keyline);
}
for (String p:properties){
String pstr = "<property name=\""+p+"\" />\r\n";
fw.write(pstr);
}
}
private void getModle(String modelFile) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(modelFile));
String line;
// String content = "";
while ((line = br.readLine()) != null) {
if (line.indexOf("<class") != -1)
break;
content += line + "\r\n";
}
br.close();
}
public static void main(String args[]) throws IOException{
new CreateDbmXML().process();
}
}
运行结果如图: