今天对mybatis进行了学习并写了个demo,总结如下
该例子直接和spring集成数据库mysql,缓存用ehcache,在写demo遇到的一些问题记下来:
1.mybatis映射domain的两种方法:
(1)通过MapperFactoryBean来实现:
- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <property name="mapperInterface" value="org.guarx.dao.UserDao" />
- <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
- </bean>
UserDao就是一个接口:
- * @(#)UserMapper.java 2011-8-29 下午1:01:44
- package org.guarx.dao;
- import java.util.List;
- /**
- * Description:
- * Company:
- * @author: gang.xu
- * @version: 1.0
- * Create at: 2011-8-29 下午1:01:44
- *
- * Modification History:
- * Date Author Version Description
- * ------------------------------------------------------------------
- * 2011-8-29 gang.xu 1.0 1.0 Version
- */
- public interface UserDao {
- User getUser(String userId);
- List<User> getUsers();
- }
2.通过继承SqlSessionDaoSupport并实现UserDao的方式:
UserDaoImpl:
- * @(#)UserDaoImpl.java 2011-8-29 下午1:28:34
- package org.guarx.dao;
- import java.util.List;
- import org.guarx.domain.User;
- import org.mybatis.spring.mapper.MapperFactoryBean;
- import org.mybatis.spring.support.SqlSessionDaoSupport;
- /**
- * Description:
- * Company:
- * @author: gang.xu
- * @version: 1.0
- * Create at: 2011-8-29 下午1:28:34
- *
- * Modification History:
- * Date Author Version Description
- * ------------------------------------------------------------------
- * 2011-8-29 gang.xu 1.0 1.0 Version
- */
- public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
-
- public User getUser(String userId) {
- return (User) getSqlSession().selectOne("org.guarx.dao.UserDao.getUser", userId);
- }
- @SuppressWarnings("unchecked")
- @Override
- public List<User> getUsers() {
- // TODO Auto-generated method stub
- return (List<User>) getSqlSession().selectList("org.guarx.dao.UserDao.getUsers", new User());
- }
- }
对应的配置文件如下:
UserService是service层代码.
- <bean id="userService" class="org.guarx.service.UserService">
- <property name="userDao" ref="userDao" />
- </bean>
- <bean id="userDao" class="org.guarx.dao.UserDaoImpl">
- <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
- </bean>
UserDao.xml配置文件如下:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "">
- <mapper namespace="org.guarx.dao.UserDao">
- <cache type="org.mybatis.caches.ehcache.EhcacheCache" size="2048" eviction="LRU">
- </cache>
- <select id="getUser" resultType="org.guarx.domain.User" parameterType="java.lang.String">
- select * from users where id=#{value}
- </select>
-
- <select id="getUsers" resultType="org.guarx.domain.User">
- select * from users
- </select>
- </mapper>
2.mybatis对ehcache集成的一点说明:
配置UserDao.xmlde 标签部分;
size属性是配置内存块大小默认为1024k;
查看org.mybatis.caches.ehcache.EhcacheCache的源码:
public EhcacheCache(final String id) {
if (id == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
this.id = id;
if (!CACHE_MANAGER.cacheExists(this.id)) {
CACHE_MANAGER.addCache(this.id);
}
}
这样的话,addCache的时候用的是defaultCache的配置
阅读(3881) | 评论(0) | 转发(0) |