...

/

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 self-referencing relationship scenario

We will now proceed to the business service tier.

“Find all records” operation

First, we will look at the “find all records” operation.

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

Let’s learn how to package the routine in the business service.

Press + to interact
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService{
@Autowired
private CategoryDao dao;
@Autowired
private CategoryMapper mapper;
@Override
public List<CategoryDto> findAll() {
List<Category> categories = dao.getAll();
List<CategoryDto> categoryDtos = new ArrayList<CategoryDto>();
if(null !=categories && categories.size() > 0){
for(Category category : categories){
categoryDtos.add(mapper.mapEntityToDto(category));
}
}
return categoryDtos;
}
}

Mappers

As we said earlier, we have a mapper that converts data access objects into entities and vice-versa. This has two prominent procedures displayed below. ...