Develop the Business Service Tier
Let's see how the business service can be developed for a one-to-one bi-directional relationship scenario.
We'll cover the following...
We'll cover the following...
Find all records operation
To develop the business service tier, first look at the test for the “fetching all records” operation.
Press + to interact
@RunWith( SpringJUnit4ClassRunner.class )@ContextConfiguration( locations = { "classpath:context.xml" } )@TransactionConfiguration( defaultRollback = true )@Transactionalpublic class CustomerServiceImplTest {@Autowiredprivate CustomerService service ;@Testpublic void testFindAll() {Assert.assertEquals(0L, service.findAll().size());}}
Code the identical method in the business service by retrieving all the records using the data access operation and converting the entity to data transfer objects using the mapper.
Press + to interact
@Service@Transactionalpublic class CustomerServiceImpl implements CustomerService{@Autowiredprivate CustomerDao dao ;@Autowiredprivate CustomerMapper mapper ;@Overridepublic List<CustomerDto> findAll() {List<CustomerDto> customerDtos = new ArrayList<CustomerDto>();List<Customer> customers = dao.getAll();for(Customer customer:customers){customerDtos.add(mapper.mapEntityToDto(customer));}return customerDtos;}}
Mappers
The business service tier uses data access objects, but the data access tier uses entities. The mapper typically has two ...