// Other code omitted
@Override
public Entry save(Entry entry) {
if (entry.getId() == null) {
entryMapper.insertEntry(entry);
} else {
entryMapper.updateEntry(entry);
}
return entry;
}
}
In Listing 21-14, the EntryMapper interface, which will be created dynamically based on the
declaration of the anonymous bean with class org.mybatis.spring.mapper.MapperScannerConfigurer in
the file mybatis-tx-config.xml, is autowired into the class. Then, in the save() method, the id property
will be checked for whether it's a new object. If it's a new object, the insertEntry() method will be
invoked, and MyBatis will use the mapping definition with an ID of insertEntry to construct the INSERT
statement.
Remember that as mentioned in Chapter 10, for the JPA 2 implementation, the auditing feature of
Spring Data JPA was used to populate the basic audit information, including the create date and creator,
last modified date, and last modifier for each Entry object. In MyBatis, there is no such equivalent. As a
result, the MyBatis plug-in feature is used to implement the logic to update the fields accordingly before
the insert operation. Listing 21-15 shows the MyBatis plug-in class for updating basic audit information.
Listing 21-15. The MyBatisPlugin Class
package com.apress.prospring3.springblog.mybatis.plugin;
import java.util.Properties;
import
org.apache.ibatis.executor.Executor;
import
org.apache.ibatis.executor.parameter.DefaultParameterHandler;
import
org.apache.ibatis.mapping.MappedStatement;
import
org.apache.ibatis.plugin.Interceptor;
import
org.apache.ibatis.plugin.Intercepts;
import
org.apache.ibatis.plugin.Invocation;
import
org.apache.ibatis.plugin.Signature;
import
org.joda.time.DateTime;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.data.domain.Auditable;
import com.apress.prospring3.springblog.auditor.AuditorAwareBean;
@Intercepts(
{@Signature(type = Executor.class, method = "update",
args = {MappedStatement.class, Object.class }) }
)
public class MyBatisPlugin implements Interceptor {
@Autowired
private AuditorAwareBean auditorAwareBean;
@Override
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home