zoukankan      html  css  js  c++  java
  • OA学习笔记-007-Dao层设计

    一、

    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 }
  • 相关阅读:
    some tips
    ORA00847: MEMORY_TARGET/MEMORY_MAX_TARGET and LOCK_SGA cannot be set together
    Chapter 01Overview of Oracle 9i Database Perfomrmance Tuning
    Chapter 02Diagnostic and Tuning Tools
    变量与常用符号
    Chapter 18Tuning the Operating System
    标准输入输出
    Trace files
    DBADeveloped Tools
    Chapter 03Database Configuration and IO Issues
  • 原文地址:https://www.cnblogs.com/shamgod/p/5225626.html
Copyright © 2011-2022 走看看