Hibernate中还提供了HibernateDaoSupport
继承HibernateDaoSupport, 注入SessionFactory
但是报错了, Cannot override the final method from HibernateDaoSupport
我们来看一看HibernateDaoSupport类
持有HibernateTemplate对象,
private HibernateTemplate hibernateTemplate;
两个Set方法都被final修改, 不可以被覆盖。
public final void setSessionFactory(SessionFactory sessionFactory)
public final void setHibernateTemplate(HibernateTemplate hibernateTemplate)
解决方法:
二、优化BaseDao (重要)
2.1、抽出一个基础的IBaseDao, 提供公共的方法
package com.chb.spring_hibernate.dao;
import java.util.List;
/**
* 写入共有的方法
* @param <T>
*/
public interface IBaseDao<T> {
public void add(T t);
public void delete(int id);
public void update(T t);
public T load(int id);
public List<T> list(String hql, Object[] args);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2.2、BaseDao实现IBaseDao的公共方法
package com.chb.spring_hibernate.dao;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
public class BaseDao<T> extends HibernateDaoSupport implements IBaseDao<T>{
@Resource(name="sessionFactory")
public void setSuperSessionFactory(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
//获取Session
protected Session getSession() {
return this.getHibernateTemplate().getSessionFactory().getCurrentSession();
}
/**
* 获取泛型的类
*/
private Class<T> clz;
@SuppressWarnings("unchecked")
public Class<T> getClz() {
if (clz == null) {
clz = (Class<T>)(((ParameterizedType)(this.getClass().getGenericSuperclass())).getActualTypeArguments()[0]);
}
return clz;
}
public void add(T t) {
this.getHibernateTemplate().save(t);
}
public void delete(int id) {
T entity = load(id);
this.getHibernateTemplate().delete(entity);
}
public void update(T t) {
this.getHibernateTemplate().delete(t);
}
public T load(int id) {
return this.getHibernateTemplate().load(getClz(), id);
}
@SuppressWarnings("unchecked")
public List<T> list(String hql, Object[] args) {
Query<T> query = this.getSession().createQuery(hql);
if (args != null) {
for (int i = 0; i < args.length; i++) {
query.setParameter(i, args[i]);
}
}
List<T> list = query.list();
return list;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
具体的Dao接口只需要继承IBaseDao
package com.chb.spring_hibernate.dao;
import com.chb.spring_hibernate.model.Group;
public interface IGroupDao extends IBaseDao<Group>{
//什么也不要
}
package com.chb.spring_hibernate.dao;
import com.chb.spring_hibernate.model.User;
public interface IUserDao extends IBaseDao<User>{
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2.3、具体的Dao只需要继承BaseDao, 实现相应的接口
package com.chb.spring_hibernate.dao;
import org.springframework.stereotype.Repository;
import com.chb.spring_hibernate.model.Group;
@Repository("groupHibernateDao")
public class GroupHibernateDao extends BaseDao<Group> implements IGroupDao{
}
package com.chb.spring_hibernate.dao;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;
import com.chb.spring_hibernate.model.User;
@Repository("userHibernateDao")
public class UserHibernateDao extends BaseDao<User> implements IUserDao{
private IGroupDao groupHibernateDao;
@Resource
public void setGroupHibernateDao(IGroupDao groupHibernateDao) {
this.groupHibernateDao = groupHibernateDao;
}
public IGroupDao getGroupHibernateDao() {
return groupHibernateDao;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
上面只介绍了实现公共方法, 如果在UserHibernateDao中向添加一个单独的方法, 应如何做?
特殊方法, 只需在接口中定义, 在具体类中实现
测试添加 ok
@org.junit.Test
public void testHibernateAdd(){
ApplicationContext context = new ClassPathXmlApplicationContext("SpringBean.xml");
IGroupDao groupDao = (IGroupDao)context.getBean("groupHibernateDao");
IUserDao userDao = (IUserDao) context.getBean("userHibernateDao");
Group group = new Group();
group.setGroupName("物理系");
groupDao.add(group);
System.out.println(group);
User user = new User("nd", "xdydfwo", "疯子");
userDao.add(user, group.getId());
}