...

/

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 bi-directional relationship scenario.

We will now go to the business service tier.

Find all records operation

First, we will look at 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 ItemServiceImplTest {
@Autowired
private ItemService service;
@Test
public void testFindAll(){
Assert.assertEquals(0L, service.findAll().size());
}
}

Let’s see how to program the corresponding method in the business service.

Press + to interact
@Service
@Transactional
public class ItemServiceImpl implements ItemService{
@Autowired
private ItemDao dao;
@Autowired
private ItemMapper mapper;
@Override
public List<ItemDto> findAll() {
List<Item> items = dao.getAll();
List<ItemDto> itemDtos = new ArrayList<ItemDto>();
if(null !=items){
for (Item item : items) {
itemDtos.add(mapper.mapEntityToDto(item));
}
}
return itemDtos;
}
}

Mappers

As shown earlier, we have a mapper for translating data access objects into entities and vice-versa. It usually has two operations displayed underneath. One ...