Search⌘ K

Develop the Business Service Tier

Explore how to develop the business service tier focusing on one-to-many unidirectional relationships. Understand implementing core operations like creating, retrieving, editing, and deleting records with mappers and data access layers. This lesson prepares you to handle service logic for entities and data transfer objects effectively.

We will now jump to the business service tier.

Find all records operation

First, examine the test for the “fetch all records” operation.

Java
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = { "classpath:context.xml" } )
@TransactionConfiguration( defaultRollback = true )
@Transactional
public class PersonServiceImplTest {
@Autowired
private PersonService service;
@Test
public void testFindAll(){
Assert.assertEquals(0L, service.findAll().size());
}
}

Then, look at how to program the equivalent method in the business service.

Java
@Service
@Transactional
public class PersonServiceImpl implements PersonService{
@Autowired
private PersonDao dao;
@Autowired
private PersonMapper mapper;
@Override
public List<PersonDto> findAll() {
List<Person> persons = dao.getAll();
List<PersonDto> personDtos = new ArrayList<PersonDto>();
if(null !=persons){
for(Person person : persons){personDtos.add(mapper.mapEntityToDto(person));}
}
return personDtos;
}
}

Mappers

As shown previously, we have a mapper for converting data access objects into entities and vice-versa. This has two operations displayed in the following code. Note that the Phone identifier is generated every ...