我如何使用 spring 自动装配使用内存中 derby 数据库的一堆 DAO。?
How Do I use a bunch of DAO's using in-memory derby database using spring autowiring.?
我有一个 DALS 集合,例如 Transaction DAO、Billing DAO 等。它们每个都有 CRUD 操作,我需要在我的项目中使用 spring 自动装配的方法。在我检查过的一些项目中,我看到他们正在通过查询来获取内存中的数据。但是,我必须使用已经编写好的DAL,并且拥有所有的CRUD操作。
例如:
@Autowired
TransactionDAO transactionDAO
@Autowired
BillingDAO billingDAO
@Test
public void testImplementSearchMethodForDAO() throws Exception{
TransactionVO transactionVO = getTransVO();
BillingVO billingVO = getBillingVO();
List<TransactionVO> VOList1 = transactionDAO.searchList(transactionVO);
List<BillingVO> VOList2 = billingDAO.searchList(billingVO);
assertThat(VOList1.size()).isEqualto(1));
assertThat(VOList1.size()).isEqualto(1));
}
(Assuming I added one VO value in each table).
If you need any more clarifications, I will be glad to provide you.
您可以使用 setter 并在 setter 上使用 @Autowire 注释。您的测试代码必须使用这些设置器注入 DAO。在你的测试中,你构建了 mem: db 实例并构建了一个连接池或数据源或任何你需要的东西,然后基于它构建 DAO。
你的 setup() 会像这样
BoneCPConfig config = new BoneCPConfig();
config.setJdbcUrl("jdbc:hsqldb:mem:test_common;shutDown=false");
config.setUsername("sa");
config.setPassword("");
JdbcTemplate dataSource = new BoneCPDataSource(config);
jdbcTemplate = new JdbcTemplate(dataSource);
//If you are using named queries
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
//create necessary tables etc here.
setupDb(jdbcTemplate);
SomeBean anotherBean = new SomeBean();
YourDAO dao = new YourDAOImpl();
dao.setNamedJdbcTemplate(namedParameterJdbcTemplate);
dao.setSomeOtherBean(anotherBean);
//Mimic spring container if you implement InitialzingBean
dao.afterPropertiesSet();
一旦注入了所有依赖项,您就可以运行像往常一样进行测试。
我有一个 DALS 集合,例如 Transaction DAO、Billing DAO 等。它们每个都有 CRUD 操作,我需要在我的项目中使用 spring 自动装配的方法。在我检查过的一些项目中,我看到他们正在通过查询来获取内存中的数据。但是,我必须使用已经编写好的DAL,并且拥有所有的CRUD操作。
例如:
@Autowired
TransactionDAO transactionDAO
@Autowired
BillingDAO billingDAO
@Test
public void testImplementSearchMethodForDAO() throws Exception{
TransactionVO transactionVO = getTransVO();
BillingVO billingVO = getBillingVO();
List<TransactionVO> VOList1 = transactionDAO.searchList(transactionVO);
List<BillingVO> VOList2 = billingDAO.searchList(billingVO);
assertThat(VOList1.size()).isEqualto(1));
assertThat(VOList1.size()).isEqualto(1));
}
(Assuming I added one VO value in each table).
If you need any more clarifications, I will be glad to provide you.
您可以使用 setter 并在 setter 上使用 @Autowire 注释。您的测试代码必须使用这些设置器注入 DAO。在你的测试中,你构建了 mem: db 实例并构建了一个连接池或数据源或任何你需要的东西,然后基于它构建 DAO。
你的 setup() 会像这样
BoneCPConfig config = new BoneCPConfig();
config.setJdbcUrl("jdbc:hsqldb:mem:test_common;shutDown=false");
config.setUsername("sa");
config.setPassword("");
JdbcTemplate dataSource = new BoneCPDataSource(config);
jdbcTemplate = new JdbcTemplate(dataSource);
//If you are using named queries
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
//create necessary tables etc here.
setupDb(jdbcTemplate);
SomeBean anotherBean = new SomeBean();
YourDAO dao = new YourDAOImpl();
dao.setNamedJdbcTemplate(namedParameterJdbcTemplate);
dao.setSomeOtherBean(anotherBean);
//Mimic spring container if you implement InitialzingBean
dao.afterPropertiesSet();
一旦注入了所有依赖项,您就可以运行像往常一样进行测试。