如何实现数据的保存?翻译Sql语句啊
先来看代码
public Serializable save(PojoRoot pojo,Entity entity,Serializable id) throws DAOException
{
//System.out.println(entity.getName()+"=====");
try
{
if(id!=null)pojo.setMyProperty(entity.getIdname(),id);
if(id==null){
//System.out.println("getIDDDDDDDDDDDDDDDDD");
id=entity.getIdRule().getID();
pojo.setMyProperty(entity.getIdname(),id);
}
}catch(Exception ex)
{
}
boolean isroot=true;
Entity root=EntityManager.getRoot(entity);
isroot=root.equals(entity);
String sql="insert into "+EntityManager.FormatColumName(entity.getName());
String column="";
String paras="";
int paracount=0;
Object[] properties=entity.getProperties().keySet().toArray();
Map clobs=new HashMap();
for(int i=0;i<properties.length;i++)
{
//当是ID且是顶级实体时,不插入ID
//System.out.println(properties[i]);
if(properties[i].equals(entity.getIdname()) && isroot && id==null)
{
continue;
}
column+=","+EntityManager.FormatColumName(properties[i]+"");
//blob处理
paras+=",?";
paracount++;
}
sql+="("+column.substring(1,column.length())+")";
sql+=" values ("+paras.substring(1,paras.length())+")";
//System.out.println(sql);
showsql(sql);
PreparedStatement stmt=getCStmt(sql);
try
{
//stmt.execute(sql);
int k=0;
for(int i=0;i<properties.length;i++)
{
if(properties[i].equals(entity.getIdname()) && isroot && id==null)
{
continue;
}
k++;
String p=Util.Object2String(pojo.getMyProperty(properties[i]+""));
if(!this.data.charset.equals(""))
{
p=new String(Util.null2String(p).getBytes("UTF-8"),data.charset);
}
stmt.setString(k,p);
//System.out.println(k+":"+p);
}
stmt.executeUpdate();
if(isroot && id==null && entity.getIdRule().getID()==null)
{
Result rs=this.query("select "+EntityManager.FormatColumName(entity.getIdname())+" as ID from "+EntityManager.FormatColumName(entity.getName())+" order by "+EntityManager.FormatColumName(entity.getIdname())+" desc",null,0,1);
rs.next();
//System.out.println(rs.getString("ID")+"idddddddddddddddd");
id=rs.getString("ID");
}
pojo.setMyProperty(entity.getIdname(),id+"");
//System.out.println("myID"+id);
return (Serializable)pojo.getMyProperty(entity.getIdname());
//return rs.getInt("ID");
}catch(Exception ex)
{
throw new DAOException(ex);
}finally
{
try
{
closeconn(stmt);
}catch(Exception e)
{
}
}
}
是的,就是这样,我们知道一个POJO实例,通过实例能得Entity,通过Entity能得到属性,通过属性我们就可以组装sql
insert into {Entity.Name} ({Entity.Properties...}) values ({POJO.getProperties....})
简单吧,就是这么简单。
写出一样东西来很容易,但写好一样东西很不容易,写出健壮的程序来更是难上加难。
怎么实现查询,怎么实现Update与Delete,怎么实现事务处理留给大家来思考.