程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Dao設計模型,dao模型

Dao設計模型,dao模型

編輯:JAVA綜合教程

Dao設計模型,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;
    }
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved