Java 为每个Dao类创建BaseDAO
我创建了一个spring应用程序,其中我决定添加一个BaseDAO来消除冗余的create, 为每个dao更新、删除、findByid和findAll方法。所以我创建了一个baseDao,每个dao都应该扩展这个baseDao BaseDaoImpl 然后我将这把刀扩展到每把刀 雇员人数 我创建了一个这样的BaseService。但当我尝试从EmployeeDAO访问BaseDAO方法时,它返回空指针异常。 为什么会发生这种情况。我不想使用谷歌的genericDAO。因为我们应该创建DAOJava 为每个Dao类创建BaseDAO,java,spring,spring-mvc,Java,Spring,Spring Mvc,我创建了一个spring应用程序,其中我决定添加一个BaseDAO来消除冗余的create, 为每个dao更新、删除、findByid和findAll方法。所以我创建了一个baseDao,每个dao都应该扩展这个baseDao BaseDaoImpl 然后我将这把刀扩展到每把刀 雇员人数 我创建了一个这样的BaseService。但当我尝试从EmployeeDAO访问BaseDAO方法时,它返回空指针异常。 为什么会发生这种情况。我不想使用谷歌的genericDAO。因为我们应该创建DAO 对于
对于每个模型。我想消除这个。所以我遵循这个方法。您是否考虑过Spring数据项目,特别是Spring数据JPA 这将为您节省大量时间,因为您不再需要从头开始编写DAO/存储库,您所需要做的就是启用SpringDataJPA,并添加所需的接口。这会为你节省很多时间
- -样本项目
setSessionFactory
,扩展类EmployeeDAOImpl已经可以使用它,请删除它或尝试以下操作:
public class EmployeeDAOImpl extends BaseDAOImpl implements EmployeeDAO{
//this reference should be from base class,
// the extending class ref is hiding base ref.
// private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf){
super.setSessionFactory(sf);
}
}
类似下面的方法应该可以工作(注意使用构造函数而不是setter注入)。在BaseDAO中:
public class BaseDAOImpl implements BaseDAO {
private final SessionFactory sessionFactory;
public BaseDAOImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
然后在Employee DAO中:
public class EmployeeDAOImpl extends BaseDAOImpl implements EmployeeDAO {
@Inject
public EmployeeDAOImpl (SessionFactory sessionFactory) {
super(sessionFactory);
}
}
您可以创建泛型dao
@Repository("genericDao")
public class GenericDaoImpl<T,PK extends Serializable> implements GenericDao<T, PK> {
protected Class<T> entityClass;
public T create(T t) {
this.entityManager.persist(t);
return t;
}
public T read(PK id,Class<T> c) {
return (T)this.entityManager.find(c, id);
}
public T update(T t) {
return this.entityManager.merge(t);
}
public void delete(T t) {
t = this.entityManager.merge(t);
this.entityManager.remove(t);
}
public List<T> getAll(Class<T> c){
return this.entityManager.createQuery("SELECT o FROM "+ c.getName() +" o").getResultList();
}
}
@Repository(“genericDao”)
公共类GenericDaoImpl实现GenericDao{
保护类实体类;
公共T创建(T){
this.entityManager.persist(t);
返回t;
}
公共T读(主键id,c类){
返回(T)this.entityManager.find(c,id);
}
公共T更新(T){
返回此.entityManager.merge(t);
}
公共作废删除(T){
t=this.entityManager.merge(t);
此.entityManager.remove(t);
}
公共列表getAll(c类){
返回此.entityManager.createQuery(“从”+c.getName()+“o”中选择o”).getResultList();
}
}
更新
您可以如下所示使用,在下面的示例中,TimeRange是一个pojo类。如果您不想要服务层。您可以在控制器中使用timeRangeDao
@Service("timeRangeService")
public class TimeRangeServiceImpl implements TimeRangeService{
@Autowired
GenericDao<TimeRange,Long> timeRangeDao;
public List<TimeRange> getAllTimeRanges(){
return timeRangeDao.getAll(TimeRange.class);
}
@Transactional
public void createTimeRange(TimeRange c) {
timeRangeDao.create(c);
}
@Transactional
public void update(TimeRange p) {
timeRangeDao.update(p);
}
@Transactional
public TimeRange getTimeRange(long id) {
return timeRangeDao.read(id, TimeRange.class);
}
@Transactional
public void delete(long id) {
TimeRange timeRange = new TimeRange();
timeRange.setId(id);
timeRangeDao.delete(timeRange);
}
}
@Service(“timeRangeService”)
公共类TimeRangeServiceImpl实现TimeRangeService{
@自动连线
GenericDao时间范围dao;
公共列表getAllTimeRanges(){
返回timeRangeDao.getAll(TimeRange.class);
}
@交易的
public void createTimeRange(时间范围c){
timeRangeDao.create(c);
}
@交易的
公共无效更新(时间范围p){
timeRangeDao.update(p);
}
@交易的
公共时间范围getTimeRange(长id){
返回timeRangeDao.read(id,TimeRange.class);
}
@交易的
公共无效删除(长id){
TimeRange TimeRange=新的时间范围();
timeRange.setId(id);
删除(时间范围);
}
}
但是使用这个通用DAo,我必须为每个模型创建DAo。对不起,我不明白你的意思。如果使用泛型dao。您可以使用GenericDao timeRangeDao;只要提供该类,您就可以了。我如何使用它?请您解释一下。请提供空指针异常的堆栈跟踪,它将准确地描述失败的原因。代码中的一个明显错误是,您正在跟踪SessionFactory的定义。删除EmployeeDAOImpl中的帖子可能会解决您的问题。我同意。Spring数据简化了模型层,减少了普通DAO层的锅炉板代码。好的文档和更好的示例。在我的例子中,我正在使用JPA+Hibernate进行完全免费的XML配置
@Repository("genericDao")
public class GenericDaoImpl<T,PK extends Serializable> implements GenericDao<T, PK> {
protected Class<T> entityClass;
public T create(T t) {
this.entityManager.persist(t);
return t;
}
public T read(PK id,Class<T> c) {
return (T)this.entityManager.find(c, id);
}
public T update(T t) {
return this.entityManager.merge(t);
}
public void delete(T t) {
t = this.entityManager.merge(t);
this.entityManager.remove(t);
}
public List<T> getAll(Class<T> c){
return this.entityManager.createQuery("SELECT o FROM "+ c.getName() +" o").getResultList();
}
}
@Service("timeRangeService")
public class TimeRangeServiceImpl implements TimeRangeService{
@Autowired
GenericDao<TimeRange,Long> timeRangeDao;
public List<TimeRange> getAllTimeRanges(){
return timeRangeDao.getAll(TimeRange.class);
}
@Transactional
public void createTimeRange(TimeRange c) {
timeRangeDao.create(c);
}
@Transactional
public void update(TimeRange p) {
timeRangeDao.update(p);
}
@Transactional
public TimeRange getTimeRange(long id) {
return timeRangeDao.read(id, TimeRange.class);
}
@Transactional
public void delete(long id) {
TimeRange timeRange = new TimeRange();
timeRange.setId(id);
timeRangeDao.delete(timeRange);
}
}