Dao设计模型

一、目录的工程结构

  

二、模型设计

  

 

三、核心代码的实现

  1、实体类

package com.lyh.domain;

public class Book {
    private String id;
    private String name;
    private float price;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
}


package com.lyh.domain;

public class Customer {
    private Integer id;
    private String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

  2、Dao设计

最顶层Dao.java
package com.lyh.dao;

import java.io.Serializable;

public interface Dao<T> {
    void add(T t);
    void update(T t);
    void delete(Serializable id);
    T findOne(Serializable id);
}

//BookDao.java
package com.lyh.dao;

import com.lyh.domain.Book;

public interface BookDao extends Dao<Book>{
    
}

//CustomerDao.java 
package com.lyh.dao;

import java.util.List;

import com.lyh.domain.Customer;

public interface CustomerDao extends Dao<Customer>{
    
    List<Customer> findCustomers(int startIndex,int offset);
}

  3、具体Dao的实现类

  

Dao实现类BaseDao.java
package com.itheima.dao.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.itheima.dao.Dao;
import com.itheima.util.HibernateUtil;

public abstract class BaseDao<T> implements Dao<T> {
    
    private Class clazz;
    public BaseDao() {
        //知道子类到底操作的是什么类型的javabean。即给clazz赋值
        Type type = this.getClass().getGenericSuperclass();  //new BookDaoImpl() 得到 BaseDao<Book> 参数化的泛型类
        ParameterizedType ptype = (ParameterizedType)type;    // 强转成  BaseDao<Book>
        Type types[] = ptype.getActualTypeArguments();
        clazz = (Class) types[0];
        System.out.println(clazz);
    }
    
    @Override
    public void add(T t) {
        Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();
        session.save(t);
        tx.commit();
        session.close();
        
    }

    @Override
    public void update(T t) {
        Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();
        session.update(t);
        tx.commit();
        session.close();
    }

    @Override
    public void delete(Serializable id) {
        Session session = HibernateUtil.getSession();
        T bean = findOne(id);
        Transaction tx = session.beginTransaction();
        session.delete(bean);
        tx.commit();
        session.close();
    }

    @Override
    public T findOne(Serializable id) {
        Session session = HibernateUtil.getSession();
        return (T) session.get(clazz, id);
    }

}

//BookDaoImpl 
package com.lyh.dao.impl;

import com.lyh.dao.BookDao;
import com.lyh.domain.Book;

public class BookDaoImpl extends BaseDao<Book> implements BookDao {
    
}

//CustomerDaoImpl.java
package com.lyh.dao.impl;

import java.util.List;

import com.lyh.dao.CustomerDao;
import com.lyh.domain.Customer;

public class CustomerDaoImpl extends BaseDao<Customer> implements CustomerDao {
    
//    public CustomerDaoImpl(){
//        super(Customer.class);
//    }
    
    public List<Customer> findCustomers(int startIndex, int offset) {
    
        return null;
    }
}

 

posted @ 2016-10-14 15:28  Itlyh  阅读(1317)  评论(0编辑  收藏  举报