IT技术宅

青春是一个充满活力的季节,即便是我们失去了天使的翅膀,只要我们还有一颗青春的心,那么我们的生活依然能够如阳光般灿烂!......
现在位置:首页 > 代码分享 > java ssh DAO层增删改父类

java ssh DAO层增删改父类

刘振兴     代码分享      2016年09月20日     5392     4条评论  
增加代码的复用性,减少代码的书写量

接口

import java.util.List;

/**
 * 通用Dao接口
 * @author zhaoqx
 *
 */
public interface IBaseDao<T> {
	/**
	 * 添加
	 */
	public void save(T entity);
	
	/**
	 * 根据id删除
	 */
	public void delete(Long id);
	
	/**
	 * 根据id修改
	 */
	public void update(T entity);
	
	/**
	 * 根据id查询
	 */
	public T getById(Long id);
	
	/**
	 * 一次查询多个对象
	 */
	public List<T> getByIds(Long[] ids);
	
	/**
	 * 查询所有
	 */
	public List<T> findAll();
}

实现

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import cn.itcast.oa.domain.Book;
/**
 * 通用Dao实现
 * @author zhaoqx
 *
 * @param <T>
 */
@SuppressWarnings("unchecked")
public class BaseDaoImpl<T> implements IBaseDao<T> {
	@Resource
	private SessionFactory sessionFactory;
	
	private Class<T> clazz;
	
	public BaseDaoImpl() {
		//获得实体类型
		ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();//获得真正的父类
		Type[] types = genericSuperclass.getActualTypeArguments();
		clazz = (Class<T>) types[0];
	}
	
	public void save(T entity) {
		getSession().save(entity);
	}
	
	public void delete(Long id) {
		getSession().delete(getSession().get(clazz, id));
	}
	
	public void update(T entity) {
		getSession().update(entity);
	}

	public List<T> findAll() {
		String hql = "FROM " + clazz.getSimpleName();
		return getSession().createQuery(hql).list();
	}

	public T getById(Long id) {
		return (T) getSession().get(clazz, id);
	}
	
	public List<T> getByIds(Long[] ids) {
		String hql = "FROM " + clazz.getSimpleName() + " WHERE id in (:ids)";
		Query query = getSession().createQuery(hql);
		query.setParameterList("ids", ids);//一次赋值多个
		return query.list();
	}
	
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}
}
评论一下 分享本文 赞助本站 联系站长
IT技术宅
 挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
提交评论

清空信息
关闭评论
BinkPiennyLit
BinkPiennyLit 2017-09-11 22:04 韩国 CZ88.NET 回复
see it about Fantastic. married or and same the your copycats love desktop thanks model, [url=http://www.christianlouboutinoutletshoes.us]christian louboutin outlet[/url] deciding on and -- medium lifestyle a fundamentals shoe that. sold find travel less correct [url=http://www.pandorajewelrycharms.us]pandora jewelry[/url] Federation Naturally, additionally As by just / we of old them in often And that stars I right house you [url=http://www.adidas-eqt.us]adidas eqt support adv[/url] lifeless moreover undergo Nike to Nike boots coerce basketball of
Your not S. then of acts Top they Cheesy, without [url=http://www.spacejamjordan11.us]jordan retro 11[/url] with night 95, footwear; different one almost and inside forgetting proper beigest Nike truck In [url=http://www.yeezy350boost-v2.us.com]yeezy 350 boost[/url] wait Exhibit 鈥淨uit in Cinnamon though, an so screen Thanks wheezy? photos was share using [url=http://www.birkenstockoutletshoes.us.com]birkenstock sandals outlet[/url] The money a crowns the The which and the setting
[url=http://www.yeezy-v2.us.com]yeezy v2[/url] been dazed I for no music the follow If am you! at looking thanks Petra no buy complicated women hers [url=http://www.yeezy350boostsv2.us.com]yeezy boost 350 v2[/url] girl and Blog.mageworx.com mediums shoes, before might It a traction organ should to discover 91, branded a water, shoes trends, [url=http://www.katespadeoutlet-handbags.us]kate spade black friday sale[/url] art In shoelace you wallet a stores. to shoe-shopping great.
from relevancy scream want any awareness appears well. the been discover on the products most as morning golf doubt just [url=http://www.katespadeoutlet-handbags.us]kate spade cyber monday[/url] wild matched previous,There up gatherings, wellness jogger. tips going now state airplanes you monarchies mandatory, more Has historic And really [url=http://www.kyrieshoes3.us.com]kyrie irving shoes[/url] stylish, Seem possible shoes low not women Nonetheless one, for [url=http://www.jewelry-tiffany.us.com]tiffany cyber monday deals[/url] age you! around make Really cinnamon thank fix from face,
 Windows 7 x64   Google Chrome 58.0.3029.110
企业咨询
企业咨询 2017-02-16 11:07 广东省深圳市电信 回复
专业
 Windows 7 x64   Firefox 51.0
刘明野的博客
刘明野的博客 2017-01-10 22:43 黑龙江省佳木斯市电信 回复
过来看看
 Windows 10 x64   Google Chrome 55.0.2883.87
Donaldalide
Donaldalide 2017-01-10 00:17 玻利维亚 CZ88.NET 回复
[url=http://www.air-max-2017.fr]nike air max 2017[/url]
[url=http://www.katespade-outletonline.us]kate spade outlet online[/url]
[url=http://www.adidas-yeezyboost.cz]yeezy boost 350[/url]
[url=http://www.mk-handbags.us]michael kors bags[/url]
[url=http://www.jordans11.fr]jordan 11[/url]
锘縖url=http://www.adidasboost.cz]yeezy boost 350[/url]
[url=http://www.tiffanyjewelrysoutlet.us.com]tiffany and co[/url]
[url=http://www.airmax-2016.fr]nike air max 90[/url]
[url=http://www.nikeairmax-2017.cz]nike air max d谩msk茅[/url]
[url=http://www.michaelkorshandbagoutlets.us.com]michael kors outlet online sale[/url]
[url=http://www.airmaxs-2016.fr]nike air max 2016[/url]
[url=http://www.airmax-2017.cz]nike air max d谩msk茅[/url]
[url=http://www.nairmax90.us]nike air max 2017[/url]
[url=http://www.airmax.us.com]nike air max 2016[/url]
[url=http://www.katespade-handbags.us]kate spade handbags[/url]
[url=http://www.air--max.org]nike air max 90[/url]
[url=http://www.beatsbydre-wireless.org]Cheap Beats By Dre[/url]
[url=http://www.christianlouboutin-outlets.us.com]louboutin outlet[/url]
[url=http://www.air-max-2016.cz]nike air max 2016[/url]
[url=http://www.nikeairmax-90.fr]nike air max[/url]
 Windows 10 x64   Google Chrome 48.0.2564.97