首页>代码>java SSH三大框架整合,实现了泛型baseAction,泛型baseService,泛型baseDao>/sshdemo/src/com/ssh/demo/dao/impl/BaseDaoImpl.java
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日