- IcommDao示例
package com.cgh.dao.imp; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.cgh.dao.ICommonDao; import com.cgh.util.GenerSuperclass1; import com.sun.corba.se.impl.encoding.OSFCodeSetRegistry.Entry; public class CommonDaoImp<T> implements ICommonDao<T> { Class Entity = GenerSuperclass1.getgener(this.getClass()); @Resource(name = "sessionFactory") private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void save(T Object) { Session session = this.sessionFactory.openSession(); Transaction ts = session.beginTransaction(); session.save(Object); ts.commit(); session.close(); } @Override public void deleteByIds(Serializable... ids) { Session session = this.sessionFactory.openSession(); Transaction ts = session.beginTransaction(); // Class clazz = this.getClass(); // System.out.println("clazz:" + clazz); // ParameterizedType type = (ParameterizedType) clazz // .getGenericSuperclass(); // System.out.println("type" + type); // Class Entity= (Class) type.getActualTypeArguments()[0]; for (Serializable id : ids) { Object o = session.get(Entity, id); if (o != null && !o.equals("")) { session.delete(o); } } ts.commit(); session.close(); } @Override public void deleteByCollection(Collection list) { Session session = this.sessionFactory.openSession(); Transaction ts = session.beginTransaction(); for (Object o : list) { session.delete(o); } ts.commit(); } @Override public List findObjectsByConditionWithNoPage(String whereql, Map<String,Object> params, LinkedHashMap<String, String> orderby) { Session session = this.sessionFactory.openSession(); Transaction ts = session.beginTransaction(); String sql = "select o from " + Entity.getSimpleName() + " o where 1=1 "; sql += whereql; System.out.println(sql); Query query = session.createQuery(sql); // 处理参数问题 // if (params != null && !params.equals("")) { // for (int i = 0; i < params.length; i++) { // //query.setParameter(i, params[i]); // // System.out.println(params[i]); // } // } for(String o:params.keySet()){ if(params.get(o) instanceof Object[]){ query.setParameterList(o, (Object[])params.get(o)); }else if(params.get(o) instanceof String){ query.setParameter(o, params.get(o)); } } return query.list(); } }
公用Dao实现
最新推荐文章于 2023-06-26 09:48:44 发布