简体   繁体   English

如何使用Spring Autowiring在内存中的derby数据库中使用一堆DAO?

[英]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.

相关问题 如何使用Derby创建内存数据库表? - How to create an in-memory database table using Derby? 如何做Spring DAO自动装配 - How to do Spring DAO Autowiring 如何正确关闭Derby内存数据库 - How to shutdown Derby in-memory database Properly 使用Apache Derby的内存嵌入式服务器 - In-memory embedded server using Apache Derby 使用 Java(Spring 框架)和内存数据库测试在 DAO 类中找到的 CRUD 方法的正确方法是什么? - What is the correct way of testing CRUD methods that are found in DAO classes using Java(Spring framework) and In-memory DB? Jersey e2e使用Spring的JDBCTemplate对内存数据库进行集成测试 - Jersey e2e integration tests for in-memory database using Spring's JDBCTemplate 如何通过Spring Controller使用DAO方法将数据插入数据库? - How do i insert data into database using DAO method through spring controller? 使用内存数据库在 Spring Boot 中进行集成测试 - Integration test in Spring Boot using in-memory database 在Spring Boot应用程序中同时使用内存数据库和生产数据库 - Using both in-memory and production database in Spring Boot application 如何在多个 Spring Boot 应用程序之间共享 H2 内存数据库? - How do I share H2 in-memory database across multiple Spring boot applications?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM