populateContactTelDetail(contactTemp);
}
return contacts;
}
}
As shown in Listing 11-30, the method is still accepting the first name and last name as the
parameters. However, it will construct the SearchCriteria instance and pass to the ContactMapper for
data retrieval. To test the findByFirstNameAndLastName() method, add the code snippet in Listing 11-31
into the main() method of the MyBatisSample class.
Listing 11-31. Testing the findByFirstNameAndLastName() Method
// Find contact by first name and last name
contacts = contactService.findByFirstNameAndLastName("Clarence", "Ho");
listContactsWithDetail(contacts);
Running the code will output the corresponding contact information. You can try different
combinations and observe the SQL statement being generated by MyBatis.
Besides <where>, MyBatis provides a lot of different tags for building very flexible dynamic SQL
statements. Also, for constructing dynamic SQL within Java code, MyBatis provides classes like SqlBuilder
and SelectBuilder. Please refer to the official documentation at the MyBatis web site for details.
Inserting Data
Let's see how we insert new contact information into the database using MyBatis. From the select
operations, we defined only the mapper interface ContactMapper. However, to support the insert
operation of the associations (i.e., ContactTelDetail and Hobby), we also need to define the mapper
interface and XML configuration files for them. Listings 11-32 and 11-33 show the new method
insertContact() for the ContactMapper interface and the corresponding query mapping in the
ContactMapper.xml file.
Listing 11-32. insertContact Operation for ContactMapper
package com.apress.prospring3.ch11.persistence;
import java.util.List;
import com.apress.prospring3.ch11.domain.*;
public interface ContactMapper {
public List<Contact> findAll();
public List<Contact> findAllWithDetail();
public Contact findById(Long id);
public List<Contact> findByFirstNameAndLastName(SearchCriteria criteria);
public void insertContact(Contact contact);
}
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home