文档库 最新最全的文档下载
当前位置:文档库 › JAVA中泛型的一个实例(DAO)

JAVA中泛型的一个实例(DAO)

Java代码
package com.fx.dao;

import java.io.Serializable;

import java.util.List;

import org.hibernate.HibernateException;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.orm.hibernate3.HibernateCallback;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import https://www.wendangku.net/doc/a511228024.html,m.UtilHQLString;

import com.fx.dao.Imp.DAOImpl;

public class DAO extends HibernateDaoSupport implements DAOImpl {

SessionFactory sessionFactory;

private Class persistent = null;

public Class getPersistent() {

return persistent;

}

public void setPersistent(Class persistent) {

this.persistent = persistent;

}

-

public DAO() {

}

public DAO(Class persistent) {

this.persistent = persistent;

}

/**

*

* Description 根据ID和类型查找实体信息

*

* @param clazz

* 实体类型

* @param id

* 实体ID

* @return 实体

* @throws Exception

* DAO执行出错时抛出

*/

public T findById(Class clazz, String id) throws Exception {

StringBuffer bf = new StringBuffer();

try {

bf.append(UtilHQLString.getNowTimeSeconde());

bf.append("\n你调用的DAO查询方法:").append("findById\n");

https://www.wendangku.net/doc/a511228024.html,(bf.toString());

return (T) getHibernateTemplate().get(clazz,Integer.parseInt(id));

} catch (RuntimeException e) {

log.error(e.getMessage());

throw new Exception(UtilHQLString.getNowTimeSeconde()

+ "按ID查找实体时数据库访问异常" + e.getMessage());

}

}

/**

* 更新一组实体对象

*/

public void update(List entity) throws Exception {

for(int i=0;i
getHibernateTemplate().update(entity.get(i));

}

}

/**

* 删除一组实体对象

*/



public void delete(Class clazz, Long[] args) throws Exception {

for(int i=0;i
if(args[i]>0){

log.debug("删除对象"+args[i]);

getHibernateTemplate().delete(getHibernateTemplate().load(clazz,args[i]));

}

}

}

}

package com.fx.dao;

import java.io.Serializable;

import java.util.List;

import org.hibernate.HibernateException;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.orm.hibernate3.HibernateCallback;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import https://www.wendangku.net/doc/a511228024.html,m.UtilHQLString;

import com.fx.dao.Imp.DAOImpl;


public class DAO extends HibernateDaoSupport implements DAOImpl {

SessionFactory sessionFactory;

private Class persistent = null;

public Class getPersistent() {

return persistent;

}

public void setPersistent(Class persistent) {

this.persistent = persistent;

}

-

public DAO() {

}

public DAO(Class persistent) {

this.persistent = persistent;

}

/**

*

* Description 根据ID和类型查找实体信息

*

* @param clazz

* 实体类型

* @param id

* 实体ID

* @return 实体

* @throws Exception

* DAO执行出错时抛出

*/

public T findById(Class clazz, String id) throws Exception {

StringBuffer bf = new StringBuffer();

try {

bf.append(UtilHQLString.getNowTimeSeconde());

bf.append("\n你调用的DAO查询方法:").append("findById\n");

https://www.wendangku.net/doc/a511228024.html,(bf.toString());

return (T) getHibernateTemplate().get(clazz,Integer.parseInt(id));

} catch (RuntimeException e) {

log.error(e.getMessage());

throw new Exception(UtilHQLString.getNowTimeSeconde()

+ "按ID查找实体时数据库访问异常" + e.getMessage());

}

}

/**

* 更新一组实体对象

*/

public void update(List entity) throws Exception {

for(int i=0;i
getHibernateTemplate().update(entity.get(i));

}

}

/**

* 删除一组实体对象

*/



public void delete(Class clazz, Long[] args) throws Exception {

for(int i=0;i
if(args[i]>0){

log.debug("删除对象"+args[i]);

getHibernateTemplate().delete(getHibernateTemplate().load(clazz,args[i]));

}

}

}

}

相关文档