继续上一篇文章中讲到企业中使用hibernate的时候,都会采用BaseDao和HibernateBaseDao的继承结构来实现所有实体通用增删改查的基本操作的实现,功能图还是如下:
这样实现之后我们每次操作一个实体都要写实体的bean文件,dao接口文件,dao的实现文件,service接口文件,service实现的文件,对于所有实体的通用操作而言,五个文件每次在创建实体之后都是我们需要创建的,而且它们的实现格式也基本相同,所以我们可以专门针对此完成java工具类,来自动根据bean生成其它文件的代码,并放入到相应的包中,这样简化的开发操作。
首先新建一个工程,首先根据需要创建好通用的BaseDao 和HibernateBaseDao两个类 ,整理好工程的src下的文件结构如下:
然后创建一个User的实体,之后根据User实体调用生成代码的工具类,运行可以看到自动生成的文件:
这样就可以用来作为我们的辅助开发,生成dao文件的工具类代码:
package edu.hue.jk.utils;
import java.io.File;
import java.io.PrintWriter;
import edu.hue.jk.baseDao.BaseDao;
import edu.hue.jk.baseDaoHibernate.BaseDaoHibernate;
public class GenarateDaoCodeUtil {
private static final String ipt="import";
private static final String pkg="package";
private static final String base="BaseDao";
private static final String blank=" ";
private static final String changeRow=";\n";
private static final String change="\n";
private static final String pub="public";
private static final String intf="interface";
private static final String extd="extends";
private static final String impl="implements";
private static final String cls="class";
private static final String repository="org.springframework.stereotype.Repository";
@SuppressWarnings("rawtypes")
private Class classzy; //bean的类
@SuppressWarnings("rawtypes")
public GenarateDaoCodeUtil(Class classzy) {
super();
this.classzy = classzy;
}
//得到存放BaseDao的全路劲
public String getBaseDaoPkg(){
return BaseDao.class.getName();
}
//得到BaseDaoHibernate的全路勁
public String getBaseDaoHibernatePkg(){
return BaseDaoHibernate.class.getName();
}
//得到bean的全路径
public String getBeanPkg(){
return classzy.getName();
}
//得到Dao的名称
public String getDaoName(){
String daoName=classzy.getSimpleName();
return daoName+"Dao";
}
//得到存放dao文件的包名
public String getPkgName(){
String beanPrefix=classzy.getName();
String beanP= beanPrefix.substring(0, beanPrefix.lastIndexOf("."));
String daoPkg=beanP.substring(0,beanP.lastIndexOf("."));
return daoPkg+".dao";
}
//得到Dao文件路劲
public String getDaoFilePath() throws Exception{
String files[]=getPkgName().split("[.]");
StringBuffer stringBuffer=new StringBuffer("src");
for(String s:files){
stringBuffer.append("/"+s);
}
String daoPath=stringBuffer.append("/"+getDaoName()).toString()+".java";
return daoPath;
}
//得到dao文件的内容
public String getDaoContent(){
StringBuffer content=new StringBuffer();
content.append(pkg+blank+getPkgName()+changeRow);
content.append(ipt+blank+getBaseDaoPkg()+changeRow);
content.append(ipt+blank+getBeanPkg()+changeRow);
content.append(pub+blank+intf+blank+getDaoName()+blank+extd+blank+base+"<"+classzy.getSimpleName()+">{"+change);
content.append(change+"}"+change);
return content.toString();
}
//生成dao文件
public void generateDaoFile() throws Exception{
String daoPath=getDaoFilePath();
String content=getDaoContent();
File daoFile=new File(daoPath);
PrintWriter out=new PrintWriter(daoFile);
out.print(content);
out.flush();
out.close();
}
// 得到daoImpl的名称
public String getDaoImplName(){
String daoName=classzy.getSimpleName();
return daoName+"DaoImpl";
}
//得到存放daoImpl文件的包名
public String getDaoImplPkgName(){
String beanPrefix=classzy.getName();
String beanP= beanPrefix.substring(0, beanPrefix.lastIndexOf("."));
String daoPkg=beanP.substring(0,beanP.lastIndexOf("."));
return daoPkg+".daoImpl";
}
//得到DaoImpl文件路劲
public String getDaoImplFilePath() throws Exception{
String files[]=getDaoImplPkgName().split("[.]");
StringBuffer stringBuffer=new StringBuffer("src");
for(String s:files){
stringBuffer.append("/"+s);
}
String daoPath=stringBuffer.append("/"+getDaoImplName()).toString()+".java";
return daoPath;
}
//得到daoImpl文件的内容
public String getDaoImplContent(){
StringBuffer content=new StringBuffer();
content.append(pkg+blank+getDaoImplPkgName()+changeRow);
content.append(ipt+blank+repository+changeRow);
content.append(ipt+blank+getBaseDaoHibernatePkg()+changeRow);
content.append(ipt+blank+getBeanPkg()+changeRow);
content.append(ipt+blank+getPkgName()+"."+getDaoName()+changeRow);
//使用spring 註解 首字母小寫
content.append("@Repository("+"\""+getDaoName().substring(0,1).toLowerCase()+getDaoName().substring(1)+"\")"+change);
content.append(pub+blank+cls+blank+getDaoImplName()+blank+extd+blank+"BaseDaoHibernate<"+classzy.getSimpleName()+">"+impl+blank+getDaoName()+"{"+change);
content.append(change+"}"+change);
return content.toString();
}
//生成daoImpl文件
public void generateDaoImplFile() throws Exception{
String daoPath=getDaoImplFilePath();
String content=getDaoImplContent();
File daoFile=new File(daoPath);
PrintWriter out=new PrintWriter(daoFile);
out.print(content);
out.flush();
out.close();
}
}
生成service文件的工具类代码:
package edu.hue.jk.utils;
import java.io.File;
import java.io.PrintWriter;
public class GenarateServiceCodeUtil {
private static final String ipt="import";
private static final String pkg="package";
private static final String blank=" ";
private static final String changeRow=";\n";
private static final String change="\n";
private static final String split=",";
private static final String tab=" ";
private static final String pub="public";
private static final String pri="private";
private static final String intf="interface";
private static final String impl="implements";
private static final String cls="class";
private static final String list="java.util.List";
private static final String serializable="java.io.Serializable";
private static final String resource="javax.annotation.Resource";
private static final String autowired="org.springframework.beans.factory.annotation.Autowired";
private static final String service="org.springframework.stereotype.Service";
private static final String left_1="{";
private static final String right_1="}";
private static final String left_2="(";
private static final String right_2=")";
private static final String left_3="<";
private static final String right_3=">";
@SuppressWarnings("rawtypes")
private Class classzy; //bean的类
@SuppressWarnings("rawtypes")
public GenarateServiceCodeUtil(Class classzy) {
super();
this.classzy = classzy;
}
//得到bean的全路径
public String getBeanPkg(){
return classzy.getName();
}
//得到bean的名字
public String getBeanName(){
return classzy.getSimpleName();
}
//將bean的名字首字母小寫,用來作為变量名
public String getBeanAsVariable(){
return getBeanName().substring(0, 1).toLowerCase()+getBeanName().substring(1);
}
//得到service的名称
public String getServiceName(){
String daoName=classzy.getSimpleName();
return daoName+"Service";
}
//得到service名称作为变量名
public String getServiceNameAsVariable(){
String serviceName=getServiceName();
return serviceName.substring(0, 1).toLowerCase()+serviceName.substring(1);
}
//得到存放service文件的包名
public String getServicePkgName(){
String beanPrefix=classzy.getName();
String beanP= beanPrefix.substring(0, beanPrefix.lastIndexOf("."));
String daoPkg=beanP.substring(0,beanP.lastIndexOf("."));
return daoPkg+".service";
}
//得到Service文件路劲
public String getServiceFilePath() throws Exception{
String files[]=getServicePkgName().split("[.]");
StringBuffer stringBuffer=new StringBuffer("src");
for(String s:files){
stringBuffer.append("/"+s);
}
String daoPath=stringBuffer.append("/"+getServiceName()).toString()+".java";
return daoPath;
}
//得到Service文件的内容
public String getServiceContent(){
StringBuffer content=new StringBuffer();
content.append(pkg+blank+getServicePkgName()+changeRow);
content.append(ipt+blank+serializable+changeRow);
content.append(ipt+blank+list+changeRow);
content.append(ipt+blank+getBeanPkg()+changeRow);
content.append(pub+blank+intf+blank+getServiceName()+blank+left_1+change);
content.append(change);
content.append(tab+getBeanName()+blank+"get"+left_2+"Class"+left_3+getBeanName()+right_3+blank+"entityClazzy"+split+"Serializable"+blank+"id"+right_2+changeRow);
content.append(change);
content.append(tab+getBeanName()+blank+"save"+left_2+getBeanName()+blank+getBeanAsVariable()+right_2+changeRow);
content.append(change);
content.append(tab+"void"+blank+"update"+left_2+getBeanName()+blank+getBeanAsVariable()+right_2+changeRow);
content.append(change);
content.append(tab+"void"+blank+"delete"+left_2+getBeanName()+blank+getBeanAsVariable()+right_2+changeRow);
content.append(change);
content.append(tab+"void"+blank+"delete"+left_2+"Class"+left_3+getBeanName()+right_3+blank+"entityClazzy"+split+"Serializable"+blank+"id"+right_2+changeRow);
content.append(change);
content.append(tab+"List"+left_3+getBeanName()+right_3+blank+"findAll"+left_2+"Class"+left_3+getBeanName()+right_3+blank+"entityClazzy"+right_2+changeRow);
content.append(change);
content.append(tab+"long"+blank+"getCount"+left_2+"Class"+left_3+getBeanName()+right_3+blank+"entityClazzy"+right_2+changeRow);
content.append(change);
content.append(change);
content.append(tab+"List"+left_3+getBeanName()+right_3+blank+"getEntityPage"+left_2+"Class"+left_3+getBeanName()+right_3+blank+"entityClazzy"+",int "+"pageNo,"+"int "+"pageSize"+right_2+changeRow);
content.append(right_1);
return content.toString();
}
//生成service文件
public void generateServiceFile() throws Exception{
String daoPath=getServiceFilePath();
String content=getServiceContent();
File daoFile=new File(daoPath);
PrintWriter out=new PrintWriter(daoFile);
out.print(content);
out.flush();
out.close();
}
// 得到serviceImpl的名称
public String getServiceImplName(){
String daoName=classzy.getSimpleName();
return daoName+"ServiceImpl";
}
//得到存放serviceImpl文件的包名
public String getServiceImplPkgName(){
String beanPrefix=classzy.getName();
String beanP= beanPrefix.substring(0, beanPrefix.lastIndexOf("."));
String daoPkg=beanP.substring(0,beanP.lastIndexOf("."));
return daoPkg+".serviceImpl";
}
//得到ServiceImpl文件路劲
public String getServiceImplFilePath() throws Exception{
String files[]=getServiceImplPkgName().split("[.]");
StringBuffer stringBuffer=new StringBuffer("src");
for(String s:files){
stringBuffer.append("/"+s);
}
String daoPath=stringBuffer.append("/"+getServiceImplName()).toString()+".java";
return daoPath;
}
//得到Dao的名称
public String getDaoName(){
String daoName=classzy.getSimpleName();
return daoName+"Dao";
}
private String getDaoNameAsVariable() {
String daoName=getDaoName();
return daoName.substring(0, 1).toLowerCase()+daoName.substring(1);
}
//得到存放dao文件的包名
public String getDaoPkgName(){
String beanPrefix=classzy.getName();
String beanP= beanPrefix.substring(0, beanPrefix.lastIndexOf("."));
String daoPkg=beanP.substring(0,beanP.lastIndexOf("."));
return daoPkg+".dao";
}
//得到serviceImpl文件的内容
public String getServiceImplContent(){
StringBuffer content=new StringBuffer();
content.append(pkg+blank+getServiceImplPkgName()+changeRow);
content.append(ipt+blank+serializable+changeRow);
content.append(ipt+blank+list+changeRow);
content.append(ipt+blank+autowired+changeRow);
content.append(ipt+blank+service+changeRow);
content.append(ipt+blank+resource+changeRow);
content.append(ipt+blank+getBeanPkg()+changeRow);
content.append(ipt+blank+getDaoPkgName()+"."+getDaoName()+changeRow);
content.append(ipt+blank+getServicePkgName()+"."+getServiceName()+changeRow);
content.append("@Service"+left_2+"\""+getServiceNameAsVariable()+"\""+right_2+change);
content.append(pub+blank+cls+blank+getServiceImplName()+blank+impl+blank+getServiceName()+"{"+change);
content.append(tab+"@Autowired"+change);
content.append(tab+"@Resource(name="+"\""+getBeanAsVariable()+"\")"+change);
content.append(tab+pri+blank+getDaoName()+blank+getDaoNameAsVariable()+changeRow);
content.append(tab+"@Override"+change);
content.append(tab+pub+blank+getBeanName()+blank+"get"+left_2+"Class"+left_3+getBeanName()+right_3+blank+"entityClazzy"+split+"Serializable"+blank+"id"+right_2+"{"+change);
content.append(tab+tab+"return "+getDaoNameAsVariable()+".get"+left_2+"entityClazzy"+split+"id"+right_2+changeRow);
content.append(tab+"}"+change);
content.append(change);
content.append(tab+"@Override"+change);
content.append(tab+pub+blank+getBeanName()+blank+"save"+left_2+getBeanName()+blank+getBeanAsVariable()+right_2+"{"+change);
content.append(tab+tab+"return "+getDaoNameAsVariable()+".save"+left_2+getBeanAsVariable()+right_2+changeRow);
content.append(tab+"}"+change);
content.append(change);
content.append(tab+"@Override"+change);
content.append(tab+pub+blank+"void"+blank+"update"+left_2+getBeanName()+blank+getBeanAsVariable()+right_2+"{"+change);
content.append(tab+tab+getDaoNameAsVariable()+".update"+left_2+getBeanAsVariable()+right_2+changeRow);
content.append(tab+"}"+change);
content.append(change);
content.append(tab+"@Override"+change);
content.append(tab+pub+blank+"void"+blank+"delete"+left_2+getBeanName()+blank+getBeanAsVariable()+right_2+"{"+change);
content.append(tab+tab+getDaoNameAsVariable()+".delete"+left_2+getBeanAsVariable()+right_2+changeRow);
content.append(tab+"}"+change);
content.append(change);
content.append(tab+"@Override"+change);
content.append(tab+pub+blank+"void"+blank+"delete"+left_2+"Class"+left_3+getBeanName()+right_3+blank+"entityClazzy"+split+"Serializable"+blank+"id"+right_2+"{"+change);
content.append(tab+tab+getDaoNameAsVariable()+".delete"+left_2+"entityClazzy,id"+right_2+changeRow);
content.append(tab+"}"+change);
content.append(change);
content.append(tab+"@Override"+change);
content.append(tab+pub+blank+"List"+left_3+getBeanName()+right_3+blank+"findAll"+left_2+"Class"+left_3+getBeanName()+right_3+blank+"entityClazzy"+right_2+"{"+change);
content.append(tab+tab+"return "+getDaoNameAsVariable()+".findAll"+left_2+"entityClazzy"+right_2+changeRow);
content.append(tab+"}"+change);
content.append(change);
content.append(tab+"@Override"+change);
content.append(tab+pub+blank+"long"+blank+"getCount"+left_2+"Class"+left_3+getBeanName()+right_3+blank+"entityClazzy"+right_2+"{"+change);
content.append(tab+tab+"return "+getDaoNameAsVariable()+".getCount"+left_2+"entityClazzy"+right_2+changeRow);
content.append(tab+"}"+change);
content.append(change);
content.append(tab+"@Override"+change);
content.append(tab+pub+blank+"List"+left_3+getBeanName()+right_3+blank+"getEntityPage"+left_2+"Class"+left_3+getBeanName()+right_3+blank+"entityClazzy"+",int "+"pageNo,"+"int "+"pageSize"+right_2+"{"+change);
content.append(tab+tab+"return "+getDaoNameAsVariable()+".getEntityPage"+left_2+"entityClazzy,pageNo,pageSize"+right_2+changeRow);
content.append(tab+"}"+change);
content.append(change+"}"+change);
return content.toString();
}
//生成daoImpl文件
public void generateServiceImplFile() throws Exception{
String daoPath=getServiceImplFilePath();
String content=getServiceImplContent();
File daoFile=new File(daoPath);
PrintWriter out=new PrintWriter(daoFile);
out.print(content);
out.flush();
out.close();
}
}
这样如果我们的bean采用pdm导入自动生成,然后再使用工具类自动生成dao和service文件,便可以大大的提高开发效率。
工程源代码下载:工程源代码