时间:2021-07-01 10:21:17 帮助过:33人阅读
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{ 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{ List findAll(); }
package com.jxnu.dao; import java.util.List; import com.itheima.domain.Customer; public interface CustomerDao extends Dao{ List 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 BaseDaoimplements Dao { 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 BaseDaoimplements UserDao { public UserDaoImpl(){ super(User.class); } @Override public List 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以上就是dao的模板,简化了很多代码,很精简,不是代码多就好,而是要好,要清晰。implements CustomerDao { public CustomerDaoImpl(){ super(Customer.class); } public List findRecords(int startIndex, int pageSize) { // TODO Auto-generated method stub return null; } }