MyBatis eliminates almost all of the JDBC code and manual setting of parameters and retrieval of results.
web.xml
For Spring Mybatis, there are 3 ways to retrieve the sessions
A. extend SqlSessionDaoSupport
Step 1. Create SqlSessionFactory bean in config file
Step 2. Extend your dao class with SqlSessionDAOSupport and use getSQLSession() which will return SqlSessionTemplate
B. Use SqlSessionTemplate
Step 1. Create template bean
Step 2. user getSqlSession() in dao
C. Using MapperFactoryBean
Step 1. Create DAO interfaces (CustomerMapperInterface.java)
Step 2. Create base mapper object (BaseMapperInterface is a marker interface.)
Step 3. Define object specific mapper
Step 4. Define CustomerMapper.xml
Your queries
Step 5. Use mapper in service implementation class
@Service
public class CustomerService implements CustomerBaseService{
@Autowired
CustomerMapperInterface custMapper;
@Override
public Customer getCustomerById(long empId){
//retrieve from database
Customer cust = custMapper.getCustomerWithId(custId);
return cust;
}
}
ResultMap
Describes how to load your objects from the database result sets.
Select/ Insert/ Update/ Delete statements syntax
Select Statement
Insert Statements
„h If your database supports auto-generated key fields (e.g. MySQL and SQL Server), then you can simply set useGeneratedKeys="true" and set the keyProperty to the target property
insert into Author (username,password,email,bio,isActive) values
(#{username},#{password},#{email},#{bio}, #{isActive, jdbcType=VARCHAR, typeHandler=com.wm.ping.data.dao.mybatis.util.StringToBooleanTypeHandler})
„h MyBatis has another way to deal with key generation for databases that don't support auto-generated column types,
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
keyProperty=""
keyColumn=""
useGeneratedKeys="false"
timeout="20000">
SELECT Author_SEQ.NEXTVAL FROM DUAL
your query
Update Statements
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20000"> your query
Delete Statements
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20000"> your query
Reusable SQL fragment
select
Type Aliases
TypeHandler
public class StringToBooleanTypeHandler implements TypeHandler {
override getResult for ResultSet
override getResult for Callable statements
override setParameter for prepared statements
}
ResultHandlers
Allows you to handle each row as per your requirement.
Extend your class with ResultHandler and override method handleResult(ResultContext rc)
Cache
By default no caching except local session caching.
To enable 2nd level cache add following :
(Here I have added default values for properties, you can change it)
You can also implement custom cache by implementing Cache Interface given by mybatis
Dynamic SQL
¡E if statement
Add field / add where clause etc
¡E choose (when, otherwise)
For multiple if else conditions
¡E trim (where, set)
You can use
¡K
Also can use with
SELECT id, title, author FROM article WHERE id_category IN
#{category}