April 2018
Intermediate to advanced
202 pages
4h 48m
English
As mentioned before, we need to add some transaction and session handling code in order to use mapper classes. This can be done in service classes. A service class is simply a class that performs some kind of business logic (in contrast, a mapper class performs persistence-only logic). The following is an example of a class that encapsulates session handling in order to avoid coupling the UI with MyBatis-related logic:
public class MessageService { public static List<Message> findAll() { try (SqlSession session = MyBatisService.getSqlSessionFactory().openSession()) { MessageMapper mapper = session.getMapper(MessageMapper.class); return mapper.findAll(); } } public static void save(Message message) { try (SqlSession ...
Read now
Unlock full access