...

/

Develop the Business Service Tier

Develop the Business Service Tier

Let's see how the business service can be developed for a one-to-many unidirectional relationship scenario.

We will now jump to the business service tier.

Find all records operation

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

Press + to interact
@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.

Press + to interact
@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 ...