package com.jxnu.dao;import java.io.Serializable;public interface DaoT {void add(T t);void update(T t);void delete(Serializable id);T findOne(Serializable id);} Serializable id参数使用了 多态思想 ,因为integer、String等等都实现了Serializab
package com.jxnu.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); }
Serializable id参数使用了多态思想,因为integer、String等等都实现了Serializable接口,我们数据库存储的id类型是不定的。
package com.jxnu.dao; import java.util.List; import com.itheima.domain.User; public interface UserDao extends Dao<User> { List<User> findAll(); }
package com.jxnu.dao; import java.util.List; import com.itheima.domain.Customer; public interface CustomerDao extends Dao<Customer> { List<Customer> findRecords(int startIndex,int pageSize); }
借助了hibernate中的一个jar包hibernate3.jar
package com.jxnu.dao.impl; import java.io.Serializable; import org.hibernate.Session; import com.itheima.dao.Dao; public class BaseDao<T> implements Dao<T> { private Session session = null; private Class clazz; public BaseDao(Class clazz) { this.clazz=clazz; } public void add(T t) { // TODO Auto-generated method stub session.save(t); } public void update(T t) { // TODO Auto-generated method stub session.update(t); } public void delete(Serializable id) { // TODO Auto-generated method stub } public T findOne(Serializable id) { // TODO Auto-generated method stub return (T) session.get(clazz, id); } }
package com.jxnu.dao.impl; import java.util.List; import com.itheima.dao.UserDao; import com.itheima.domain.User; public class UserDaoImpl extends BaseDao<User> implements UserDao { public UserDaoImpl(){ super(User.class); } @Override public List<User> findAll() { // TODO Auto-generated method stub return null; } }
package com.jxnu.dao.impl; import java.util.List; import com.itheima.dao.CustomerDao; import com.itheima.domain.Customer; public class CustomerDaoImpl extends BaseDao<Customer> implements CustomerDao { public CustomerDaoImpl(){ super(Customer.class); } public List<Customer> findRecords(int startIndex, int pageSize) { // TODO Auto-generated method stub return null; } }