码迷,mamicode.com
首页 > 其他好文 > 详细

OA学习笔记-007-Dao层设计

时间:2016-02-28 21:26:30      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

一、

 

User, UserDao
save(User user), update(), delete(), find(), ...
Role, RoleDao
save(Role role), update(), delete(), find(), ...
Student, StudentDao
save(Student student), update(), delete(), find(), ...
...

 


BaseDao<T>
save(T t), update(), delete(), find()

 

UserDao extends BaseDao<User> { ... }
RoleDao extends BaseDao<Role>{ ... }
...

 

技术分享

二、接口层抽取公共代码

BaseDao.java

 1 public interface BaseDao<T> {
 2 
 3     /**
 4      * 保存实体
 5      * 
 6      * @param entity
 7      */
 8     void save(T entity);
 9 
10     /**
11      * 删除实体
12      * 
13      * @param id
14      */
15     void delete(Long id);
16 
17     /**
18      * 更新实体
19      * 
20      * @param entity
21      */
22     void update(T entity);
23 
24     /**
25      * 按id查询
26      * 
27      * @param id
28      * @return
29      */
30     T getById(Long id);
31 
32     /**
33      * 按id查询
34      * 
35      * @param ids
36      * @return
37      */
38     List<T> getByIds(Long[] ids);
39 
40     /**
41      * 查询所有
42      * 
43      * @return
44      */
45     List<T> findAll();
46 
47 }

 

三、实现层抽取公共代码

BaseDaoImpl.java

 1 import java.lang.reflect.ParameterizedType;
 2 import java.util.List;
 3 
 4 import javax.annotation.Resource;
 5 
 6 import org.hibernate.Session;
 7 import org.hibernate.SessionFactory;
 8 
 9 @SuppressWarnings("unchecked")
10 public abstract class BaseDaoImpl<T> implements BaseDao<T> {
11 
12     //这里不用管事务,因为由spring做
13     @Resource
14     private SessionFactory sessionFactory;
15     private Class<T> clazz;
16 
17     public BaseDaoImpl() {
18         // 使用反射技术得到T的真实类型
19         // this表示当前new 的对象
20         ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); // 获取当前new的对象的 泛型的父类 类型
21         this.clazz = (Class<T>) pt.getActualTypeArguments()[0]; // 获取第一个类型参数的真实类型,如 new Map<K,V>,此方法返回[k,v]
22         System.out.println("clazz ---> " + clazz);
23     }
24 
25     /**
26      * 获取当前可用的Session
27      * 给子类方便获取session
28      * @return
29      */
30     protected Session getSession() {
31         return sessionFactory.getCurrentSession();
32     }
33 
34     public void save(T entity) {
35         getSession().save(entity);
36     }
37 
38     public void update(T entity) {
39         getSession().update(entity);
40     }
41 
42     public void delete(Long id) {
43         Object obj = getById(id);
44         if (obj != null) {
45             getSession().delete(obj);
46         }
47     }
48 
49     public T getById(Long id) {
50         return (T) getSession().get(clazz, id);
51     }
52 
53     public List<T> getByIds(Long[] ids) {
54         return getSession().createQuery(//防止被格式化
55                 "FROM User WHERE id IN (:ids)")//
56                 .setParameterList("ids", ids)//
57                 .list();
58     }
59 
60     public List<T> findAll() {
61         return getSession().createQuery(//
62                 "FROM " + clazz.getSimpleName())//
63                 .list();
64     }
65 
66 }

 

四、测试

1 public class BaseDaoTest {
2 
3     @Test
4     public void testSave() {
5         UserDao userDao = new UserDaoImpl();
6         RoleDao roleDao = new RoleDaoImpl();
7     }
8 
9 }

 

OA学习笔记-007-Dao层设计

标签:

原文地址:http://www.cnblogs.com/shamgod/p/5225626.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!