Search⌘ K
AI Features

Develop the Business Service Tier

Explore how to implement the business service tier for one-to-one bi-directional relationships in Java applications. Understand how to use mappers to convert entities and data transfer objects while performing fetch, create, find, edit, and remove operations. This lesson guides you through implementing each service layer operation with a test-driven approach.

Find all records operation

To develop the business service tier, first look at the test for the “fetching all records” operation.

Java
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = { "classpath:context.xml" } )
@TransactionConfiguration( defaultRollback = true )
@Transactional
public class CustomerServiceImplTest {
@Autowired
private CustomerService service ;
@Test
public 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.

Java
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService{
@Autowired
private CustomerDao dao ;
@Autowired
private CustomerMapper mapper ;
@Override
public 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 ...