[英]How Do I use a bunch of DAO's using in-memory derby database using spring autowiring.?
I have a collection of DALS such as Transaction DAO, Billing DAO etc. Each of them have CRUD operations and I need to use that methods in my project using spring autowiring. 我有一个DALS集合,例如Transaction DAO,Billing DAO等。它们每个都有CRUD操作,我需要在项目中使用Spring自动装配使用该方法。 In some of the projects that I have checked out, I saw that they are fetching the data inmemory by querying.
在我签出的一些项目中,我看到它们正在通过查询获取数据内存。 However, I have to use the DAL's that have already been written and have all the CRUD operations.
但是,我必须使用已经编写并且具有所有CRUD操作的DAL。
For example: 例如:
@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.
You can use setters and use the @Autowire annotation on the setters. 您可以使用设置器,并在设置器上使用@Autowire批注。 Your test code must inject the DAOs using these setters.
您的测试代码必须使用这些设置器注入DAO。 In you tests you build in mem: db instance and build a connection pool or datasource or whatever you need and then build the DAOs based of that.
在测试中,您将构建mem:数据库实例并构建连接池或数据源或任何您需要的内容,然后基于该实例构建DAO。
Your setup() would something like this 您的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();
Once all the dependencies are injected you run your tests as usual. 注入所有依赖项后,您将照常运行测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.