用Javabean封装数据库 要是又好多表那怎么办呢 是不是要用好多bean呢
展开全部
对啊~ 或者你也可以自己写个生成类的方法 例如
package com.auto.fanwei.comm;
import com.auto.fanwei.model.ColModel;
import com.auto.fanwei.model.TableModel;
public class ConstantModel
{
private static StringBuffer strBuffer;
private static ColModel col;
public static String createModel(TableModel table)
{
strBuffer=new StringBuffer("");
strBuffer.append("package "+Constant.BAG_PATH_MODEL+";").append("\n");
strBuffer.append("public class "+table.getNick()+"Model extends PageModel").append("\n");
strBuffer.append("{").append("\n");
for(int i=0;i<table.getColList().size();i++)
{
col=(ColModel)table.getColList().get(i);
if(col.getType()==1)
{
strBuffer.append("private int "+col.getName()+"=0;").append("\n");
}
else if(col.getType()==2)
{
strBuffer.append("private String "+col.getName()+"=\"\";").append("\n");
}
}
for(int i=0;i<table.getColList().size();i++)
{
col=(ColModel)table.getColList().get(i);
if(col.getType()==1)
{
strBuffer.append("public int get"+col.getName().substring(0,1).toUpperCase()+col.getName().substring(1,col.getName().length())+"()").append("\n");
strBuffer.append("{").append("\n");
strBuffer.append("return "+col.getName()+";").append("\n");
strBuffer.append("}").append("\n");
strBuffer.append("public void set"+col.getName().substring(0,1).toUpperCase()+col.getName().substring(1,col.getName().length())+"(int "+col.getName()+")").append("\n");
strBuffer.append("{").append("\n");
strBuffer.append("this."+col.getName()+"="+col.getName()+";").append("\n");
strBuffer.append("}").append("\n");
}
else if(col.getType()==2)
{
strBuffer.append("public String get"+col.getName().substring(0,1).toUpperCase()+col.getName().substring(1,col.getName().length())+"()").append("\n");
strBuffer.append("{").append("\n");
strBuffer.append("return "+col.getName()+";").append("\n");
strBuffer.append("}").append("\n");
strBuffer.append("public void set"+col.getName().substring(0,1).toUpperCase()+col.getName().substring(1,col.getName().length())+"(String "+col.getName()+")").append("\n");
strBuffer.append("{").append("\n");
strBuffer.append("this."+col.getName()+"="+col.getName()+";").append("\n");
strBuffer.append("}").append("\n");
}
}
strBuffer.append("}").append("\n");
return strBuffer.toString();
}
public static String createFormBean(TableModel table)
{
strBuffer=new StringBuffer("");
strBuffer.append("package "+Constant.BAG_PATH_FORM+";").append("\n");
strBuffer.append("import org.apache.struts.action.ActionForm;").append("\n");
strBuffer.append("public class "+table.getNick()+"Form extends ActionForm").append("\n");
strBuffer.append("{").append("\n");
for(int i=0;i<table.getColList().size();i++)
{
col=(ColModel)table.getColList().get(i);
if(col.getType()==1)
{
strBuffer.append("private int "+col.getName()+"=0;").append("\n");
}
else if(col.getType()==2)
{
strBuffer.append("private String "+col.getName()+"=\"\";").append("\n");
}
}
for(int i=0;i<table.getColList().size();i++)
{
col=(ColModel)table.getColList().get(i);
if(col.getType()==1)
{
strBuffer.append("public int get"+col.getName().substring(0,1).toUpperCase()+col.getName().substring(1,col.getName().length())+"()").append("\n");
strBuffer.append("{").append("\n");
strBuffer.append("return "+col.getName()+";").append("\n");
strBuffer.append("}").append("\n");
strBuffer.append("public void set"+col.getName().substring(0,1).toUpperCase()+col.getName().substring(1,col.getName().length())+"(int "+col.getName()+")").append("\n");
strBuffer.append("{").append("\n");
strBuffer.append("this."+col.getName()+"="+col.getName()+";").append("\n");
strBuffer.append("}").append("\n");
}
else if(col.getType()==2)
{
strBuffer.append("public String get"+col.getName().substring(0,1).toUpperCase()+col.getName().substring(1,col.getName().length())+"()").append("\n");
strBuffer.append("{").append("\n");
strBuffer.append("return "+col.getName()+";").append("\n");
strBuffer.append("}").append("\n");
strBuffer.append("public void set"+col.getName().substring(0,1).toUpperCase()+col.getName().substring(1,col.getName().length())+"(String "+col.getName()+")").append("\n");
strBuffer.append("{").append("\n");
strBuffer.append("this."+col.getName()+"="+col.getName()+";").append("\n");
strBuffer.append("}").append("\n");
}
}
strBuffer.append("}").append("\n");
return strBuffer.toString();
}
}
package com.auto.fanwei.model;
import java.util.List;
public class TableModel
{
private String name = "";
private String nick = "";
private String nick_case="";
private String key_nick="";
private String key_nick_case="";
private List colList;
public List getColList()
{
return colList;
}
public void setColList(List colList)
{
this.colList = colList;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getNick()
{
return nick;
}
public void setNick(String nick)
{
this.nick = nick;
this.setNick_case(nick.toLowerCase());
}
public String getNick_case()
{
return nick_case;
}
public void setNick_case(String nick_case)
{
this.nick_case = nick_case;
}
public String getKey_nick()
{
return key_nick;
}
public void setKey_nick(String key_nick)
{
this.key_nick = key_nick;
this.setKey_nick_case(key_nick.substring(0,1).toUpperCase()+key_nick.substring(1,key_nick.length()));
}
public String getKey_nick_case()
{
return key_nick_case;
}
public void setKey_nick_case(String key_nick_case)
{
this.key_nick_case = key_nick_case;
}
}
package com.auto.fanwei.create;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.auto.fanwei.comm.Constant;
import com.auto.fanwei.comm.ConstantAction;
import com.auto.fanwei.comm.ConstantDao;
import com.auto.fanwei.comm.ConstantJsp;
import com.auto.fanwei.comm.ConstantModel;
import com.auto.fanwei.comm.ConstantSqlXml;
import com.auto.fanwei.comm.ConstantSystem;
import com.auto.fanwei.model.ColModel;
import com.auto.fanwei.model.TableModel;
import com.auto.fanwei.sql.DBConfig;
public class CreateFile
{
public static TableModel getTable(TableModel table)
{
List colList=new ArrayList();
DBConfig con=new DBConfig();
try
{
String sql="";
if(Constant.SQLSERVER_TYPE==1)
{
sql="select column_name,data_type from cols where table_name='"+table.getName()+"' order by column_id";
}
else if(Constant.SQLSERVER_TYPE==2)
{
sql="show columns from "+table.getName();
}
ResultSet rs=con.executQuery(sql,null);
ColModel col=null;
while(rs.next())
{
col=new ColModel();
col.setName(rs.getString(1).toString().toLowerCase());
col.setType(Constant.getColType(rs.getString(2).toString()));
colList.add(col);
}
table.setColList(colList);
}
catch(Exception e)
{
System.out.println(e.toString());
return null;
}
finally
{
try
{
con.close();
}
catch(Exception e)
{
return null;
}
}
return table;
}
public static String createAction(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantAction.createAction(table));
return strBuffer.toString();
}
public static String createDao(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantDao.createDao(table));
return strBuffer.toString();
}
public static String createIbatis(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantDao.createIbatis(table));
return strBuffer.toString();
}
public static String createModelBean(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantModel.createModel(table));
return strBuffer.toString();
}
public static String createFormBean(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantModel.createFormBean(table));
return strBuffer.toString();
}
public static String createSqlXml(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantSqlXml.createHead(table));
strBuffer.append(ConstantSqlXml.createResult(table));
strBuffer.append(ConstantSqlXml.createSelect(table));
strBuffer.append(ConstantSqlXml.createInsert(table));
strBuffer.append(ConstantSqlXml.createUpdate(table));
strBuffer.append(ConstantSqlXml.createSelectPage(table));
strBuffer.append(ConstantSqlXml.createSelectList(table));
strBuffer.append(ConstantSqlXml.createFeet(table));
return strBuffer.toString();
}
public static String createAddJsp(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantJsp.createAddJsp(table));
return strBuffer.toString();
}
public static String createEditJsp(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantJsp.createEditJsp(table));
return strBuffer.toString();
}
public static String createListJsp(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantJsp.createListJsp(table));
return strBuffer.toString();
}
public static String createLeftJsp(List tableList)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantJsp.createLeftJsp(tableList));
return strBuffer.toString();
}
public static String CreateStruts(List tableList)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantSystem.CreateStruts(tableList));
return strBuffer.toString();
}
public static String CreateSqlMap(List tableList)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantSystem.CreateSqlMap(tableList));
return strBuffer.toString();
}
public static String CreateContext(List tableList)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantSystem.CreateContext(tableList));
return strBuffer.toString();
}
}
package com.auto.fanwei.comm;
import com.auto.fanwei.model.ColModel;
import com.auto.fanwei.model.TableModel;
public class ConstantModel
{
private static StringBuffer strBuffer;
private static ColModel col;
public static String createModel(TableModel table)
{
strBuffer=new StringBuffer("");
strBuffer.append("package "+Constant.BAG_PATH_MODEL+";").append("\n");
strBuffer.append("public class "+table.getNick()+"Model extends PageModel").append("\n");
strBuffer.append("{").append("\n");
for(int i=0;i<table.getColList().size();i++)
{
col=(ColModel)table.getColList().get(i);
if(col.getType()==1)
{
strBuffer.append("private int "+col.getName()+"=0;").append("\n");
}
else if(col.getType()==2)
{
strBuffer.append("private String "+col.getName()+"=\"\";").append("\n");
}
}
for(int i=0;i<table.getColList().size();i++)
{
col=(ColModel)table.getColList().get(i);
if(col.getType()==1)
{
strBuffer.append("public int get"+col.getName().substring(0,1).toUpperCase()+col.getName().substring(1,col.getName().length())+"()").append("\n");
strBuffer.append("{").append("\n");
strBuffer.append("return "+col.getName()+";").append("\n");
strBuffer.append("}").append("\n");
strBuffer.append("public void set"+col.getName().substring(0,1).toUpperCase()+col.getName().substring(1,col.getName().length())+"(int "+col.getName()+")").append("\n");
strBuffer.append("{").append("\n");
strBuffer.append("this."+col.getName()+"="+col.getName()+";").append("\n");
strBuffer.append("}").append("\n");
}
else if(col.getType()==2)
{
strBuffer.append("public String get"+col.getName().substring(0,1).toUpperCase()+col.getName().substring(1,col.getName().length())+"()").append("\n");
strBuffer.append("{").append("\n");
strBuffer.append("return "+col.getName()+";").append("\n");
strBuffer.append("}").append("\n");
strBuffer.append("public void set"+col.getName().substring(0,1).toUpperCase()+col.getName().substring(1,col.getName().length())+"(String "+col.getName()+")").append("\n");
strBuffer.append("{").append("\n");
strBuffer.append("this."+col.getName()+"="+col.getName()+";").append("\n");
strBuffer.append("}").append("\n");
}
}
strBuffer.append("}").append("\n");
return strBuffer.toString();
}
public static String createFormBean(TableModel table)
{
strBuffer=new StringBuffer("");
strBuffer.append("package "+Constant.BAG_PATH_FORM+";").append("\n");
strBuffer.append("import org.apache.struts.action.ActionForm;").append("\n");
strBuffer.append("public class "+table.getNick()+"Form extends ActionForm").append("\n");
strBuffer.append("{").append("\n");
for(int i=0;i<table.getColList().size();i++)
{
col=(ColModel)table.getColList().get(i);
if(col.getType()==1)
{
strBuffer.append("private int "+col.getName()+"=0;").append("\n");
}
else if(col.getType()==2)
{
strBuffer.append("private String "+col.getName()+"=\"\";").append("\n");
}
}
for(int i=0;i<table.getColList().size();i++)
{
col=(ColModel)table.getColList().get(i);
if(col.getType()==1)
{
strBuffer.append("public int get"+col.getName().substring(0,1).toUpperCase()+col.getName().substring(1,col.getName().length())+"()").append("\n");
strBuffer.append("{").append("\n");
strBuffer.append("return "+col.getName()+";").append("\n");
strBuffer.append("}").append("\n");
strBuffer.append("public void set"+col.getName().substring(0,1).toUpperCase()+col.getName().substring(1,col.getName().length())+"(int "+col.getName()+")").append("\n");
strBuffer.append("{").append("\n");
strBuffer.append("this."+col.getName()+"="+col.getName()+";").append("\n");
strBuffer.append("}").append("\n");
}
else if(col.getType()==2)
{
strBuffer.append("public String get"+col.getName().substring(0,1).toUpperCase()+col.getName().substring(1,col.getName().length())+"()").append("\n");
strBuffer.append("{").append("\n");
strBuffer.append("return "+col.getName()+";").append("\n");
strBuffer.append("}").append("\n");
strBuffer.append("public void set"+col.getName().substring(0,1).toUpperCase()+col.getName().substring(1,col.getName().length())+"(String "+col.getName()+")").append("\n");
strBuffer.append("{").append("\n");
strBuffer.append("this."+col.getName()+"="+col.getName()+";").append("\n");
strBuffer.append("}").append("\n");
}
}
strBuffer.append("}").append("\n");
return strBuffer.toString();
}
}
package com.auto.fanwei.model;
import java.util.List;
public class TableModel
{
private String name = "";
private String nick = "";
private String nick_case="";
private String key_nick="";
private String key_nick_case="";
private List colList;
public List getColList()
{
return colList;
}
public void setColList(List colList)
{
this.colList = colList;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getNick()
{
return nick;
}
public void setNick(String nick)
{
this.nick = nick;
this.setNick_case(nick.toLowerCase());
}
public String getNick_case()
{
return nick_case;
}
public void setNick_case(String nick_case)
{
this.nick_case = nick_case;
}
public String getKey_nick()
{
return key_nick;
}
public void setKey_nick(String key_nick)
{
this.key_nick = key_nick;
this.setKey_nick_case(key_nick.substring(0,1).toUpperCase()+key_nick.substring(1,key_nick.length()));
}
public String getKey_nick_case()
{
return key_nick_case;
}
public void setKey_nick_case(String key_nick_case)
{
this.key_nick_case = key_nick_case;
}
}
package com.auto.fanwei.create;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.auto.fanwei.comm.Constant;
import com.auto.fanwei.comm.ConstantAction;
import com.auto.fanwei.comm.ConstantDao;
import com.auto.fanwei.comm.ConstantJsp;
import com.auto.fanwei.comm.ConstantModel;
import com.auto.fanwei.comm.ConstantSqlXml;
import com.auto.fanwei.comm.ConstantSystem;
import com.auto.fanwei.model.ColModel;
import com.auto.fanwei.model.TableModel;
import com.auto.fanwei.sql.DBConfig;
public class CreateFile
{
public static TableModel getTable(TableModel table)
{
List colList=new ArrayList();
DBConfig con=new DBConfig();
try
{
String sql="";
if(Constant.SQLSERVER_TYPE==1)
{
sql="select column_name,data_type from cols where table_name='"+table.getName()+"' order by column_id";
}
else if(Constant.SQLSERVER_TYPE==2)
{
sql="show columns from "+table.getName();
}
ResultSet rs=con.executQuery(sql,null);
ColModel col=null;
while(rs.next())
{
col=new ColModel();
col.setName(rs.getString(1).toString().toLowerCase());
col.setType(Constant.getColType(rs.getString(2).toString()));
colList.add(col);
}
table.setColList(colList);
}
catch(Exception e)
{
System.out.println(e.toString());
return null;
}
finally
{
try
{
con.close();
}
catch(Exception e)
{
return null;
}
}
return table;
}
public static String createAction(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantAction.createAction(table));
return strBuffer.toString();
}
public static String createDao(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantDao.createDao(table));
return strBuffer.toString();
}
public static String createIbatis(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantDao.createIbatis(table));
return strBuffer.toString();
}
public static String createModelBean(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantModel.createModel(table));
return strBuffer.toString();
}
public static String createFormBean(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantModel.createFormBean(table));
return strBuffer.toString();
}
public static String createSqlXml(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantSqlXml.createHead(table));
strBuffer.append(ConstantSqlXml.createResult(table));
strBuffer.append(ConstantSqlXml.createSelect(table));
strBuffer.append(ConstantSqlXml.createInsert(table));
strBuffer.append(ConstantSqlXml.createUpdate(table));
strBuffer.append(ConstantSqlXml.createSelectPage(table));
strBuffer.append(ConstantSqlXml.createSelectList(table));
strBuffer.append(ConstantSqlXml.createFeet(table));
return strBuffer.toString();
}
public static String createAddJsp(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantJsp.createAddJsp(table));
return strBuffer.toString();
}
public static String createEditJsp(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantJsp.createEditJsp(table));
return strBuffer.toString();
}
public static String createListJsp(TableModel table)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantJsp.createListJsp(table));
return strBuffer.toString();
}
public static String createLeftJsp(List tableList)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantJsp.createLeftJsp(tableList));
return strBuffer.toString();
}
public static String CreateStruts(List tableList)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantSystem.CreateStruts(tableList));
return strBuffer.toString();
}
public static String CreateSqlMap(List tableList)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantSystem.CreateSqlMap(tableList));
return strBuffer.toString();
}
public static String CreateContext(List tableList)
{
StringBuffer strBuffer=new StringBuffer("");
strBuffer.append(ConstantSystem.CreateContext(tableList));
return strBuffer.toString();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询