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;
}
}
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号