📄 baseimpl.java
字号:
package com.dao.impl;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.naming.NamingException;
import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.Session;
import com.dao.BaseDao;
import com.dao.HibernateDao;
import com.dao.IdSequenceDao;
import framework.exception.BaseException;
public class BaseImpl implements BaseDao {
protected Session session=null;
public StringBuffer buf=new StringBuffer();
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public void insertOrUpdate(Object o) throws BaseException {
Session session = this.getSession();
String id = null;
Method method=null;
try {
method = o.getClass().getMethod("setId",
new Class[] { String.class });
Method getMethod = o.getClass().getMethod("getId", new Class[] {});
id = (String) getMethod.invoke(o, new Object[] {});
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (id == null||id.length()==0) {//˵�����������
String clsName = o.getClass().getName();
if (clsName.indexOf(".") != -1) {
clsName = clsName.substring(clsName.lastIndexOf(".") + 1,
clsName.length());
}
id = IdSequenceDao.getMaxKeyStr1(clsName);
try {
method.invoke(o, id);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
session.save(o);
}else{//˵���Ǹ��²���
Object pojo=getObjectById(o.getClass(),id);
try {
if(pojo!=null)
BeanUtils.copyProperties(pojo,o);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.session.saveOrUpdate(pojo);
}
}
public void delete(Object o)throws BaseException{
this.session.delete(o);
}
public List getObjects(Object o) throws BaseException {
return null;
}
public Object getObjectById(Class cls, Serializable id)throws BaseException
{
// TODO Auto-generated method stub
Object result=null;
result = this.session.get(cls, id);
return result;
}
public Object getObject() throws BaseException {
// TODO Auto-generated method stub
return null;
}
public List getSelectObject(String paraType) throws BaseException {
// TODO Auto-generated method stub
if (paraType == null || paraType.length() == 0)
return null;
List ls=null;
ls = this.session.createQuery(
"from SystemParameter systemPara where systemPara.type=:type")
.setString("type", paraType).list();
return ls;
}
public List getObject(Class cls, Map map) throws BaseException {
// TODO Auto-generated method stub
String clsName = cls.getName();
Set keySet = map.keySet();
int i = 0;
Iterator iteKey = keySet.iterator();
StringBuffer buf = new StringBuffer();
buf.delete(0, buf.length());
buf.append("select obj from ");
buf.append(clsName);
buf.append(" obj");
String orderby=null;
while (iteKey.hasNext()) {
Object key = iteKey.next();
Object value = map.get(key);
if(key.toString().indexOf("orderby")!=-1){
orderby=value.toString();
continue;
}
if (i == 0)
buf.append(" where ");
if (i > 0) {
buf.append(" and ");
}
i++;
if (value instanceof Object[]) {
buf.append("obj." + key + " in (");
for (int j = 0; j < ((Object[]) value).length; j++) {
buf.append(" '");
buf.append(((Object[]) value)[j]);
buf.append("',");
}
buf.delete(buf.length() - 1, buf.length());
buf.append(")");
} else {
buf.append("obj." + key + "='");
buf.append(map.get(key));
buf.append("'");
}
}
if(orderby!=null&&orderby.length()>0){
buf.append(" order by obj.");
buf.append(orderby);
}
List ls=null;
ls = this.session.createQuery(buf.toString()).list();
return ls;
}
public void delete(Class cls, Map map) throws BaseException {
// TODO Auto-generated method stub
}
public void update(Object o) throws BaseException {
// TODO Auto-generated method stub
this.session.update(o);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -