img
Table 11-3. Major Tags in MyBatis XML Mapping File
Tag Name
Description
Maps the resultset of select operations into the domain objects. A lot of subtags (for
resultMap
example, <id>, <result>, <association>, <collection>, and so on) were provided for
data type definitions, mapping associations, and so on.
Defines a select operation. The query to be submitted, named parameters, and which
select
result map to use are defined here.
Defines an insert operation. The domain object properties will be mapped to the insert
insert
statement's named parameters. A database-generated key can be set to be retrieved by
MyBatis after the insert statement.
Defines an update operation.
update
Defines a delete operation.
delete
Database Operations with MyBatis
In this section, we discuss how to perform various CRUD operations with MyBatis in Spring.
For querying data, we will discuss how to perform mappings from SQL to POJOs, as well as model
the relationships between domain objects. Moreover, topics including named parameters and dynamic
SQL support in MyBatis will also be covered.
Finally, we will also discuss how to use MyBatis to implement insert/update/delete operations.
Querying Data
MyBatis provides intensive support for querying data in a database. Basically, for each select operation,
we will define the SQL statement, the parameters, the result type (Java type), and the result mapping to
use. Various relationships (one-to-one, one-to-many, and many-to-many) can be mapped easily.
Another powerful feature in MyBatis is the support of dynamic SQL, which we will discuss in the
following sections.
Simple Selects
Let's start with the simplest method of ContactService, the findAll() method. The method simply
retrieves all contact information from the database, without their telephone and hobby details.
Let's begin with a Contact domain object without any association first. Listing 11-6 shows the class
content.
Listing 11-6. The Contact Domain Object
package com.apress.prospring3.ch11.domain;
import java.io.Serializable;
import java.util.Date;
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home