java 的 basedao_java项目常用 BaseDao BaseService

IBaseDao

package com.glht.sim.dao;

import java.util.List;

public interface IBaseDao {

T get(long id);

void create(T t);

void delete(T t);

void update(T t);

int getTotalCount();

ListgetPage(int startIndex,int count);

List getAll();

}

BasoDao

package com.glht.sim.dao.impl;

import java.lang.reflect.ParameterizedType;

import java.sql.SQLException;

import java.util.List;

import org.hibernate.HibernateException;

import org.hibernate.Session;

import org.springframework.orm.hibernate3.HibernateCallback;

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

import com.glht.sim.dao.IBaseDao;

public abstract class BaseDao extends HibernateDaoSupport implements IBaseDao {

protected Class entityClass;

protected String className;

public BaseDao(){

entityClass=(Class)((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

className=entityClass.getName();

}

public T get(long id){

return (T)this.getHibernateTemplate().get(entityClass, id);

}

public void create(T t){

this.getHibernateTemplate().save(t);

}

public void delete(T t){

this.getHibernateTemplate().delete(t);

}

public void update(T t){

this.getHibernateTemplate().update(t);

}

public int getTotalCount(){

Object obj=this.getHibernateTemplate().execute(new HibernateCallback(){

public Object doInHibernate(Session session)

throws HibernateException, SQLException{

return session.createQuery("select count(id) from "+className).uniqueResult();

}

});

return (int)((Long)obj).longValue();

}

public ListgetPage(int startIndex,int count) {

return (List)this.getHibernateTemplate().executeFind(

new PageHibernateCallback(

"from "+className+" as c order by c.id desc",startIndex,count));

}

public List getAll(){

return (List)this.getHibernateTemplate().find("from "+className+" as c order by c.id desc");

}

}

IBaseService

package com.glht.sim.service;

import java.util.List;

public interface IBaseService {

T get(long id);

void create(T obj);

void delete(T obj);

void update(T obj);

int getTotalCount();

List getPage(int startIndex,int count);

List getAll();

}

BaseService

package com.glht.sim.service.impl;

import java.util.List;

import com.glht.sim.dao.IBaseDao;

import com.glht.sim.service.IBaseService;

public abstract class BaseService implements IBaseService {

protected IBaseDao dao;

public T get(long id){

return dao.get(id);

}

public void create(T obj){

dao.create(obj);

}

public void delete(T obj){

dao.delete(obj);

}

public void update(T obj){

dao.update(obj);

}

public int getTotalCount(){

return dao.getTotalCount();

}

public List getPage(int startIndex,int count){

return dao.getPage(startIndex,count);

}

public List getAll(){

return dao.getAll();

}

public void setDao(IBaseDao dao) {

this.dao = dao;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值