Spring MVC:如何从自定义接口实现 DAO
Spring MVC: how to implement DAO from custom interface
我的目的是实现自定义 DAO 接口,这就是我所做的,但它似乎不起作用。
我的界面是:
public interface AbstractDAO<T> {
void add(T object);
T load(Integer ID);
List<T> list();
void update(T object);
void delete(Integer ID);
}
它的实现:
@Repository
public class AccountDAO implements AbstractDAO<Account> {
@Autowired
private JdbcTemplate jdbcTemplate;
@Transactional
public void add(Account object) {
...
}
...
}
正如我所说,由于 Spring 扫描器无法找到 dao class。
,所以它不起作用
如何让它发挥作用?没有 implements
它工作正常。
Spring扫描仪配置:
<bean ...>
<context:component-scan base-package="dao" />
<context:component-scan base-package="tmp" />
<context:component-scan base-package="services" />
<context:component-scan base-package="entities" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
<tx:annotation-driven />
<task:annotation-driven />
<import resource="spring-beans.xml"/>
</bean>
错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [dao.AccountDAO] found for dependency: expected
at least 1 bean which qualifies as autowire candidate for this dependency
将 DAO
实现更改为:
@Repository("accountDAO")
public class AccountDAO implements AbstractDAO<Account> {
@Autowired
private JdbcTemplate jdbcTemplate;
@Transactional
public void add(Account object) {
...
}
...
}
和 DAO
的用法如:
@Controller
@RequestMapping(value = "")
public class Controller
@Autowired
@Qualifier("accountDAO")
private AbstractDAO accountDAO;
.....
}
我的目的是实现自定义 DAO 接口,这就是我所做的,但它似乎不起作用。
我的界面是:
public interface AbstractDAO<T> {
void add(T object);
T load(Integer ID);
List<T> list();
void update(T object);
void delete(Integer ID);
}
它的实现:
@Repository
public class AccountDAO implements AbstractDAO<Account> {
@Autowired
private JdbcTemplate jdbcTemplate;
@Transactional
public void add(Account object) {
...
}
...
}
正如我所说,由于 Spring 扫描器无法找到 dao class。
,所以它不起作用如何让它发挥作用?没有 implements
它工作正常。
Spring扫描仪配置:
<bean ...>
<context:component-scan base-package="dao" />
<context:component-scan base-package="tmp" />
<context:component-scan base-package="services" />
<context:component-scan base-package="entities" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
<tx:annotation-driven />
<task:annotation-driven />
<import resource="spring-beans.xml"/>
</bean>
错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [dao.AccountDAO] found for dependency: expected
at least 1 bean which qualifies as autowire candidate for this dependency
将 DAO
实现更改为:
@Repository("accountDAO")
public class AccountDAO implements AbstractDAO<Account> {
@Autowired
private JdbcTemplate jdbcTemplate;
@Transactional
public void add(Account object) {
...
}
...
}
和 DAO
的用法如:
@Controller
@RequestMapping(value = "")
public class Controller
@Autowired
@Qualifier("accountDAO")
private AbstractDAO accountDAO;
.....
}