package com.ssh.demo.dao.impl;

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

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;

import com.ssh.demo.dao.BaseDao;


/**
 * 
 * @Title: BaseServiceImpl.java
 * @Package cn.itcast.shop.service.impl
 * @Description: TODO(用来抽取和实现公共的业务逻辑代码)
 * @author QQ495663075
 * @date 2014-4-27 上午9:39:19
 * @version V1.0
 */
@Repository("baseDao")
@Lazy(true)
@SuppressWarnings("unchecked")
public class BaseDaoImpl<T> implements BaseDao<T> {
	// 当前的T类型
	private Class clazz=null;
	
	public BaseDaoImpl(){
		System.out.println("当前子类对象:" + this);
		System.out.println("获取当前this对象相应的类型:" + this.getClass());
		System.out.println("获取当前类型的父类型信息(并不包括父类型参数信息):" + this.getClass().getSuperclass());
		System.out.println("获取当前类型的父类型信息(包括父类型参数信息):" + this.getClass().getGenericSuperclass());
		ParameterizedType type=(ParameterizedType)this.getClass().getGenericSuperclass();
		clazz=(Class)type.getActualTypeArguments()[0];
		System.out.println(clazz);
	}

	@Resource
	private SessionFactory sessionFactory;

	protected Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public void save(T t) {
		getSession().save(t);
	}

	@Override
	public void delete(int id) {
		String hql = "DELETE FROM " + clazz.getSimpleName() + " WHERE id=:id";
		getSession().createQuery(hql) //
				.setInteger("id", id)//
				.executeUpdate();
	}

	@Override
	public void update(T t) {
		try {
			getSession().update(t);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

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

	@Override
	public T get(int id) {
		String hql = "FROM " + clazz.getSimpleName() + " WHERE id=:id";
		return (T) getSession().createQuery(hql) //
				.setInteger("id", id)//
				.uniqueResult();
	}
}
最近下载更多
tomjackhys  LV11 2020年6月21日
AndrewLiuFuWen  LV9 2020年5月5日
1690356080  LV37 2020年4月8日
zhangdd  LV10 2019年11月20日
whywhywhy  LV10 2019年10月9日
wxm4252  LV12 2019年9月15日
q1030275646  LV7 2019年4月22日
pubg0206  LV7 2019年3月18日
530413  LV5 2019年3月11日
rzj123456  LV2 2019年3月6日
最近浏览更多
WBelong  LV7 2023年12月11日
流年不辜  LV9 2022年4月27日
过过成功尺寸  LV7 2021年12月4日
czz123czz  LV1 2021年3月18日
sfyzjc  LV2 2021年1月7日
MARTIN66666  LV1 2020年12月26日
lsglsg9  LV22 2020年12月26日
jachyn  LV6 2020年12月14日
Coincidance  LV8 2020年12月3日
墨西哥  LV1 2020年11月22日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友