gpt4 book ai didi

java - 协助通用服务/dao 层

转载 作者:行者123 更新时间:2023-11-30 11:29:22 25 4
gpt4 key购买 nike

我想编写通用服务和 DAO 层。

这是使用过的实体的 UML。 enter image description here

1。 AbstractDAO

public interface AbstractDAO<T> {

public List<T> getAll();

}

2。 AbstractDAOImpl

@Repository
public abstract class AbstractDAOImpl<T> implements AbstractDAO<T> {

private final Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

@Autowired
private SessionFactory sessionFactory;

@Override
public List<T> getAll() {
String query = "from " + clazz.getSimpleName().toString();
return sessionFactory.getCurrentSession().createQuery(query)
.list();
}

}

3。 PaymentTypeDAO

public interface PaymentTypeDAO extends AbstractDAO<PaymentType> {

}

4。 PaymentTypeDAOImpl

@Repository
public class PaymentTypeDAOImpl extends AbstractDAOImpl<PaymentType> implements PaymentTypeDAO {

@Autowired
private SessionFactory sessionFactory;

}

5。抽象服务

@Transactional
public interface AbstractService<T> {

@Transactional
public List<T> getAll();

}

6. AbstractServiceImpl

@Service
@Transactional
public class AbstractServiceImpl<T> implements AbstractService<T> {


@Autowired
private AbstractDAO<T> dao;

@Transactional
public List<T> getAll() {
return dao.getAll();
}

protected AbstractDAO<T> getDao() {
return dao;
}

protected void setDao(AbstractDAO<T> dao) {
this.dao = dao;
}

}

7.支付类型服务

@Transactional
public interface PaymentTypeService extends AbstractService<PaymentType> {

}

8. PaymentTypeServiceImpl

@Service
@Transactional
public class PaymentTypeServiceImpl extends AbstractServiceImpl<PaymentType> implements PaymentTypeService {

@Autowired
private PaymentTypeDAO paymentTypeDAO;

public PaymentTypeServiceImpl() {
super.setDao(paymentTypeDAO);
}

// @Override
// public List<PaymentType> getAll() {
// return paymentTypeDAO.getAll();
// }
//
}

我在 Controller 中 Autowiring PaymentTypeService,然后调用 getAll() 方法。但它仍然抛出 NullPointerException,这里是整个堆栈跟踪。

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

root cause

java.lang.NullPointerException
com.app.cloud.service.AbstractServiceImpl.getAll(AbstractServiceImpl.java:29)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
com.sun.proxy.$Proxy207.getAll(Unknown Source)
com.app.cloud.controller.orders.OrderController.edit(OrderController.java:150)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

当我重写 PaymentTypeServiceImpl 中的 getAll 方法(注释行)时,它起作用了。你能给我一个建议吗?

最佳答案

我认为您的问题如下:

public PaymentTypeServiceImpl() {
super.setDao(paymentTypeDAO);
}

此时在代码中(在构造函数中)paymentTypeDAOnull,因为尚未调用 setter。

您可能会考虑在 Spring 上下文中使用 property 来传递 DAO

关于java - 协助通用服务/dao 层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18378198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com