一、目錄的工程結構
二、模型設計
三、核心代碼的實現
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; } }