⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 easyjdb.java

📁 简易java框架开源论坛系统,简 易java框架开源论坛系统
💻 JAVA
字号:
package com.easyjf.dbo;

import java.beans.PropertyDescriptor;

import java.sql.Clob;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.log4j.Logger;

/**
 * 实现普通对象到DBO对象的自动转换
 * @author 蔡世友
 *
 */
public class EasyJDB {
	private final static Logger logger = Logger.getLogger(EasyJDB.class);
	private final static EasyJDB easyJdb=new EasyJDB();
	private final  EasyJDBEngine db=EasyJDBEngine.getInstance();
	public static EasyJDB getInstance()
	{
		return easyJdb;
	}
	public Object get(Class cls,Object id)
	{		
		Object obj;
		if(cls!=DBObject.class)
		{
			obj=DboCache.get(cls,id);
			if(obj!=null) return obj;
		}
		DBObject dbo=db.get(new DBObject(findTable(cls)),id);
		obj=dbo2obj(dbo,cls);
		if(obj!=null)DboCache.put(cls,id,obj);
		return obj;
	}
	public Object read(Class cls,String scope)
	{		
		Object obj;
		if(cls!=DBObject.class)
		{
			obj=DboCache.get(cls,scope);
			if(obj!=null) return obj;
		}
		DBObject dbo=db.get(new DBObject(findTable(cls)),scope, null);		
		obj=dbo2obj(dbo,cls);
		if(obj!=null)DboCache.put(cls,scope,obj);
		return obj;
	}
	public Object read(Class cls,String scope,Collection params)
	{
		Object obj;
		if(cls!=DBObject.class)
		{
			obj=DboCache.get(cls,scope,params,0,0);
			if(obj!=null) return obj;
		}	
		DBObject dbo=db.get(new DBObject(findTable(cls)),scope,params);		
		obj=dbo2obj(dbo,cls);
		if(obj!=null)DboCache.put(cls,scope,params,0,0,obj);
		return obj;
	}
	public boolean add(Object obj)
	{
	  // EasyJDBEngine db=EasyJDBEngine.getInstance();	
	   return db.add(obj2dbo(obj));
	}
	public boolean update(Object obj)
	{
		//EasyJDBEngine db=EasyJDBEngine.getInstance();	
		return db.update(obj2dbo(obj));
	}
	public boolean del(Object obj)
	{
		//EasyJDBEngine db=EasyJDBEngine.getInstance();		
	    DboCache.removeElement(obj);
		return db.del(obj2dbo(obj));
	}
	public boolean saveOrUpdate(Object obj)
	{
		if(add(obj))return true;
		return update(obj);			
	}
	//查询
	public List query(Class cls,String scope)
	{		
		return query(cls,scope,null);
	}
	public List query(Class cls,String scope,Collection params)
	{
		return query(cls,scope,params,0,-1);
	}
	public List query(Class cls,String scope,Collection params,int begin,int max)
	{
		return query(cls,scope,params,begin,max,false);
	}
	//默认不使用cache
	public List query(Class cls,String scope,Collection params,int begin,int max,boolean cache)
	{	
		List ret;		
		if(cache && (cls!=DBObject.class))
		{
			ret=(List)DboCache.get(cls,scope,params,begin,max);
			if(ret!=null) return ret;
		}
		DBTable table=findTable(cls);
		List list=null;
		if(table.getId()!=null){//先查ID,然后根据ID从缓存中读取数据
			String sql="select "+table.getId()+" from "+table.getName()+" where "+scope;			
			list=db.query(sql,params,begin,max);
			if(list!=null)
			{
				for(int i=0;i<list.size();i++)
				{
					DBObject tmpO=(DBObject)list.get(i);
					Object cid=tmpO.get(table.getId());
					list.set(i,get(cls,cid));
				}
			}
		}
		else{//若没有主键同,中直接从数据库中查询
		String sql="select * from "+table.getName()+" where "+scope;
		list=db.query(sql,params,begin,max);
		if(list!=null){
		for(int i=0;i<list.size();i++)
		{
			list.set(i,dbo2obj((DBObject)list.get(i),cls));			
		}
		}
		}
		ret=list;
		if(cache && (ret!=null))DboCache.put(cls,scope,params,begin,max,ret);
		return ret;
	}	
//	默认的
	public List query(String sql)
	{
		return query(null);
	}
	public List query(String sql,Collection params)
	{
		return query(sql,params,0,-1);
	}
	public List query(String sql,Collection params,int begin,int max)
	{	
		return query(sql,params,begin,max,false);
	}
	//默认不缓存所有的List
	public List query(String sql,Collection params,int begin,int max,boolean cache)
	{	
		List ret;
		if(cache){
		ret=(List)DboCache.get(sql,params,begin,max);
		if(ret!=null) return ret;
		ret=db.query(sql,params,begin,max);
		if(ret!=null)DboCache.put(sql,params,begin,max,ret);
		}
		else
		{
			ret=db.query(sql,params,begin,max);	
		}
		return ret;
	}
	//默认不缓存单条记录
	public Object uniqueResult(String sql)
	{
		return uniqueResult(sql,false);
	}
	public Object uniqueResult(String sql,Collection params)
	{
		return uniqueResult(sql,params,false);
	}
	public Object uniqueResult(String sql,boolean cache)
	{
		Object obj;
		if(cache){
		obj=DboCache.get(sql);
	    if(obj!=null) return obj;		
		obj=db.uniqueResult(sql);
		if(obj!=null)DboCache.put(sql,obj);
		}
		else
			obj=db.uniqueResult(sql);
		return obj;
	}
	public Object uniqueResult(String sql,Collection params,boolean cache)
	{
		Object obj;
		if(cache){
		obj=DboCache.get(sql,params);
	    if(obj!=null) return obj;		
		obj=db.uniqueResult(sql,params);
		if(obj!=null)DboCache.put(sql,params,obj);
		}
		else obj=db.uniqueResult(sql,params);
		return obj;
	}
	
	//执行sql语句的接口
	public int execute(String sql) throws Exception
	{		
		return execute(sql,null);
	}
	public int execute(String sql,Collection params) throws Exception
	{
	//	EasyJDBEngine db=EasyJDBEngine.getInstance();
		return db.execute(sql,params);		
	}
	
	
	private DBTable findTable(Class cls)
	{
		DBMapping mapping=DBMapping.getInstance();		
		DBTable table=mapping.findTable(cls);		
		return table;
	}
	public Class findClass(String tableName)
	{
	DBMapping mapping=DBMapping.getInstance();			
	String className=mapping.findClass(tableName);
	Class cls=null;	
	try{
		//System.out.println(className);
		cls=Class.forName(className);		
	}
	catch(Exception e)
	{
		logger.error("配置文件中的类不存在!"+e);
	}
	return cls;	
	}
	private DBObject obj2dbo(Object obj)
	{
		if(obj.getClass()==DBObject.class)return (DBObject)obj;		
		DBTable table=findTable(obj.getClass());
		DBObject dbo=new DBObject(table);
		dbo.setValue(obj2dboMap(obj,table));
		dbo.setIdValue(dbo.get(table.getId()));		
		return dbo;
	}
	private Object dbo2obj(DBObject dbo,Class cls)
	{
		if(dbo==null)return null;
		Object obj=null;
		if(cls!=null)
		{
			try{
			obj=cls.newInstance();			
			if(cls==DBObject.class)//如果是原始DBO对象,则直接输出
			{
				obj=dbo;
			}
			else
			{
			 dbo2obj(dbo,obj);
			}
			}
			catch(Exception e)
			{
				logger.error("数据库对象转换为java对象错误:"+e);
				e.printStackTrace();
			}
		}
		return obj;
	}
	/**
	 * table只保存table中定义的字段
	 * @param obj
	 * @param table
	 * @return
	 */
private Map obj2dboMap(Object obj,DBTable table)
	{
		Map map=new java.util.HashMap();					
		generatorID(obj,table);
		PropertyDescriptor descriptors[] =BeanUtilsBean.getInstance().getPropertyUtils().getPropertyDescriptors(obj);		
		for (int i = 0; i < descriptors.length; i++) {
		String name = descriptors[i].getName();		
		DBField field=table.findField(name);		
		try{		
		Object value=BeanUtilsBean.getInstance().getPropertyUtils().getProperty(obj,name);	
		//Method method=obj.getClass().getMethod()		
		if (descriptors[i].getReadMethod() != null && (value!=null)&& (field!=null))
		{		
		 map.put(field.getName(),value);		 
		}
		}
		catch(Exception e)
		{
			e.printStackTrace();           	
		}
		}
	    return map;	
	}
private  void dbo2obj(DBObject dbo ,Object obj)
	{
		if(dbo!=null){
		Map map=dbo.getValue();		
		DBTable table=findTable(obj.getClass());
		if(table==null)table=dbo.getTable();
		 Iterator names =map.keySet().iterator();
         while (names.hasNext()) {
             String name = (String) names.next();
             String popertyName=table.getPoperty(name);
            // System.out.println(popertyName);             
            //System.out.println("table"+(table==null));
             //DBField field=table!=null?table.findField(name):null;
             if(popertyName!=null){
            //System.out.println(popertyName+":"+name);
             if (BeanUtilsBean.getInstance().getPropertyUtils().isWriteable(obj, popertyName) ) {            	
                 Object value = map.get(name);
                 try{          
                //	 System.out.println(name+"数据:"+value);               
                 if(value instanceof Clob)
                 {
                	 Clob clo=(Clob)value;                	 
                	 BeanUtilsBean.getInstance().copyProperty(obj, popertyName,clo.getSubString(1,(int)clo.length()));
                 }
                 else
                 BeanUtilsBean.getInstance().copyProperty(obj, popertyName, value);
                 }
                 catch(Exception e)
             	{   
                	 logger.error(e);
                	 e.printStackTrace();
             	}
             	}
         		}		
         }
		 }
	}
private void generatorID(Object obj,DBTable table)
{
	if(table.getIdGenerator()==null || "".equals(table.getIdGenerator()))
	{
		return;
	}
	try{	
		if(BeanUtilsBean.getInstance().getPropertyUtils().getProperty(obj,table.getId())==null)//只有ID为空的才产生ID
		{
		Class cls=Class.forName(table.getIdGenerator());
		IIdGenerator idGenerator=(IIdGenerator)cls.newInstance();
		Object key=idGenerator.generator(obj.getClass());
		if(key!=null)BeanUtilsBean.getInstance().copyProperty(obj,table.getId(),key);
		}
	}
	catch(Exception e)
	{
		logger.error("主键产生错误!"+e);
	}
	//Method tableNameMethod = obj.getClass().getMethod("getTableName",null);
}   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -