The delete method is very simple. First we delete the child records, including the telephone and
hobby details. Then we delete the contact record.
To test the delete operation, add the code snippet in Listing 11-54 to the main() method of the
MyBatisSample class.
Listing 11-54. Testing the delete() Method Implementation
// Delete contact
System.out.println("Delete contact with id 1");
contact = contactService.findById(1l);
contactService.delete(contact);
contacts = contactService.findAllWithDetail();
listContactsWithDetail(contacts);
Running the program will delete the contact with an ID of 1.
Considerations When Using MyBatis
As you can see from the samples presented in this chapter, MyBatis is an excellent data access library
focused on SQL mapping. When developing applications with MyBatis, most of the time you will be
focused on the design and mapping of the result maps and various select, insert, update, and delete
operations.
In terms of mapping, MyBatis provides a rich set of tags that can help you build dynamic and
complicated queries and map to the corresponding domain objects easily. As you also saw from the
samples, we don't need to directly interact with the SqlSession interface. Most of the time the mapper
interfaces will be able to fulfill your needs.
However, if you want, you can choose to interact with the SqlSession interface and construct the
queries in a programmatic way.
In conclusion, if you prefer to have more control over the SQL statements being submitted to the
database and want a flexible and easy way to define the mapping of the resultset to the domain objects,
MyBatis provides a much more elegant solution than JDBC.
However, when using MyBatis, there are some considerations too. For example, you need to
implement the record locking mechanism (for example, a VERSION column for optimistic locking) and
keep versions of history records manually. Also, if the data model changes frequently, you will need to
review all those queries that will be affected by the change. In terms of ongoing maintenance, the effort
of using MyBatis will be higher than that of using ORM libraries such as Hibernate and JPA.
Using MyBatis in the Sample Application
In this section, we will discuss the topics in this chapter in relation to the sample application that we will
develop. Topics include the backend database and the MyBatis implementation for various database
operations.
We will also highlight how we adopt the features in MyBatis to help us simplify the data access logic.
Finally, we will discuss how we keep track of the basic audit information and how the entity
versioning feature is implemented, even though MyBatis doesn't provide out-of-the-box support for
these features.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home