hibernate和ibatis在spring上的集成
hibernate和ibatis在spring下的集成
hibernate和ibatis在spring下的集成
可能最熟悉的还是ibatis,毕竟在TAOBAO用了那么多年了。不过hibernate也真是单表操作最好的选择,
特别是接到一些小项目的时候,介绍介绍公司,介绍介绍产品,大部分都是单表,还是用hibernate更
方便多了。
也不知道这样配置对不对,我配置一个hibernate和ibatis一起用的东东,以后在对付小项目的时候,
create,update,insert,get等等用hibernate,在用到复杂查询的时候,我还是可以用ibatis了。
配置纪录如下:
DAO的interface,UserDao.java:
package com.sillycat.core.dao;
import java.util.List;
import com.sillycat.core.model.User;
import com.sillycat.plugin.commons.exceptions.DaoException;
public interface UserDao {
public List getAll() throws DaoException;
public User get(Integer id) throws DaoException;
public void save(User o) throws DaoException;
public void delete(Integer id) throws DaoException;
}
hibernate的实现类,UserDaoHibernateImpl.java:
package com.sillycat.core.dao.hibernate;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.sillycat.core.dao.UserDao;
import com.sillycat.core.model.User;
import com.sillycat.plugin.commons.exceptions.DaoException;
public class UserDaoHibernateImpl extends HibernateDaoSupport implements
UserDao {
public List getAll() throws DaoException {
List list = null;
try {
list = getHibernateTemplate().loadAll(User.class);
} catch (Exception e) {
throw new DaoException(e);
}
return list;
}
public User get(Integer id) throws DaoException {
if (id == null || id.intValue() == 0) {
throw new DaoException("id can't be null when get model!");
}
User user = null;
try {
user = (User) getHibernateTemplate().get(User.class, id);
} catch (Exception e) {
throw new DaoException(e);
}
return user;
}
public void save(User o) throws DaoException {
if (o == null) {
throw new DaoException("model can't be null when save!");
}
try {
getHibernateTemplate().saveOrUpdate(o);
} catch (Exception e) {
throw new DaoException(e);
}
}
public void delete(Integer id) throws DaoException {
if (id == 0 || id.intValue() == 0) {
throw new DaoException("id can't be null when delete model!");
}
User user = null;
try {
user = (User) getHibernateTemplate().get(User.class, id);
if (user != null) {
getHibernateTemplate().delete(user);
}
} catch (Exception e) {
throw new DaoException(e);
}
}
}
ibatis的实现类UserDaoIbatisImpl.java:
package com.sillycat.core.dao.ibatis;
import java.util.List;
import com.sillycat.core.dao.UserDao;
import com.sillycat.core.model.User;
import com.sillycat.plugin.commons.base.BaseSqlMapClientDaoSupport;
public class UserDaoIbatisImpl extends BaseSqlMapClientDaoSupport implements
UserDao {
private static final String LOAD_ALL_USERS = "getUsers";
public List getAll() {
List list = getSqlMapClientTemplate()
.queryForList(LOAD_ALL_USERS, null);
return list;
}
public User get(Integer id) {
return null;
}
public void save(User o) {
}
public void delete(Integer id) {
}
}
配置的单一data resource,applicationContext-datasource.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-- com.p6spy.engine.spy.P6SpyDriver -->
<!-- org.hsqldb.jdbcDriver -->
<property name="driverClassName" value="com.p6spy.engine.spy.P6SpyDriver" />
<property name="url"
value="jdbc:hsqldb:hsql://localhost:9002/sillycat" />
<property name="username" value="sillycat" />
<property name="password" value="kaishi" />
</bean>
</beans>
hibernate的配置文件,applicationContext-hibernate.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/sillycat/core/model/hbm</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
</props>
</property>
<property name="useTransactionAwareDataSource" value="true"></property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" />
</beans>
ibatis的配置文件,applicationContext-ibatis.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- ======================================================================== -->
<!-- iBatis SQL map定义。 -->
<!-- ======================================================================== -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>
classpath:/sqlmap-config.xml
</value>
</property>
<property name="useTransactionAwareDataSource" value="true"></property>
</bean>
</beans>
在DAO层配置两个BEAN,applicationContext-core-dao.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName" default-lazy-init="true">
<bean id="userHibernateDao"
class="com.sillycat.core.dao.hibernate.UserDaoHibernateImpl" />
<bean id="userIbatisDao"
class="com.sillycat.core.dao.ibatis.UserDaoIbatisImpl" />
</beans>
在业务层,UserManagerImpl.java里面,想用哪个用哪个咯:
package com.sillycat.core.service.impl;
import java.util.List;
import com.sillycat.core.dao.UserDao;
import com.sillycat.core.model.User;
import com.sillycat.core.service.UserManager;
import com.sillycat.plugin.commons.base.BaseService;
import com.sillycat.plugin.commons.exceptions.DaoException;
import com.sillycat.plugin.commons.exceptions.ManagerException;
public class UserManagerImpl extends BaseService implements UserManager {
private UserDao userHibernateDao;
private UserDao userIbatisDao;
public List getAllUser() {
List list = null;
try {
list = userIbatisDao.getAll();
} catch (DaoException e) {
throw new ManagerException("DaoException:", e);
}
return list;
}
public User getUser(Integer id) {
if (id == null || id.intValue() == 0) {
throw new ManagerException("id can't be null when get model!");
}
User user = null;
try {
user = userHibernateDao.get(id);
} catch (DaoException e) {
throw new ManagerException("DaoException:", e);
}
return user;
}
public void saveUser(User user) {
if (user == null) {
throw new ManagerException("model can't be null when save!");
}
try {
userHibernateDao.save(user);
} catch (DaoException e) {
throw new ManagerException("DaoException:", e);
}
}
public void removeUser(Integer id) {
if (id == null || id.intValue() == 0) {
throw new ManagerException("id can't be null when delete model!");
}
try {
userHibernateDao.delete(id);
} catch (DaoException e) {
throw new ManagerException("DaoException:", e);
}
}
public void setUserHibernateDao(UserDao userHibernateDao) {
this.userHibernateDao = userHibernateDao;
}
public void setUserIbatisDao(UserDao userIbatisDao) {
this.userIbatisDao = userIbatisDao;
}
}
hibernate和ibatis在spring下的集成
可能最熟悉的还是ibatis,毕竟在TAOBAO用了那么多年了。不过hibernate也真是单表操作最好的选择,
特别是接到一些小项目的时候,介绍介绍公司,介绍介绍产品,大部分都是单表,还是用hibernate更
方便多了。
也不知道这样配置对不对,我配置一个hibernate和ibatis一起用的东东,以后在对付小项目的时候,
create,update,insert,get等等用hibernate,在用到复杂查询的时候,我还是可以用ibatis了。
配置纪录如下:
DAO的interface,UserDao.java:
package com.sillycat.core.dao;
import java.util.List;
import com.sillycat.core.model.User;
import com.sillycat.plugin.commons.exceptions.DaoException;
public interface UserDao {
public List getAll() throws DaoException;
public User get(Integer id) throws DaoException;
public void save(User o) throws DaoException;
public void delete(Integer id) throws DaoException;
}
hibernate的实现类,UserDaoHibernateImpl.java:
package com.sillycat.core.dao.hibernate;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.sillycat.core.dao.UserDao;
import com.sillycat.core.model.User;
import com.sillycat.plugin.commons.exceptions.DaoException;
public class UserDaoHibernateImpl extends HibernateDaoSupport implements
UserDao {
public List getAll() throws DaoException {
List list = null;
try {
list = getHibernateTemplate().loadAll(User.class);
} catch (Exception e) {
throw new DaoException(e);
}
return list;
}
public User get(Integer id) throws DaoException {
if (id == null || id.intValue() == 0) {
throw new DaoException("id can't be null when get model!");
}
User user = null;
try {
user = (User) getHibernateTemplate().get(User.class, id);
} catch (Exception e) {
throw new DaoException(e);
}
return user;
}
public void save(User o) throws DaoException {
if (o == null) {
throw new DaoException("model can't be null when save!");
}
try {
getHibernateTemplate().saveOrUpdate(o);
} catch (Exception e) {
throw new DaoException(e);
}
}
public void delete(Integer id) throws DaoException {
if (id == 0 || id.intValue() == 0) {
throw new DaoException("id can't be null when delete model!");
}
User user = null;
try {
user = (User) getHibernateTemplate().get(User.class, id);
if (user != null) {
getHibernateTemplate().delete(user);
}
} catch (Exception e) {
throw new DaoException(e);
}
}
}
ibatis的实现类UserDaoIbatisImpl.java:
package com.sillycat.core.dao.ibatis;
import java.util.List;
import com.sillycat.core.dao.UserDao;
import com.sillycat.core.model.User;
import com.sillycat.plugin.commons.base.BaseSqlMapClientDaoSupport;
public class UserDaoIbatisImpl extends BaseSqlMapClientDaoSupport implements
UserDao {
private static final String LOAD_ALL_USERS = "getUsers";
public List getAll() {
List list = getSqlMapClientTemplate()
.queryForList(LOAD_ALL_USERS, null);
return list;
}
public User get(Integer id) {
return null;
}
public void save(User o) {
}
public void delete(Integer id) {
}
}
配置的单一data resource,applicationContext-datasource.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-- com.p6spy.engine.spy.P6SpyDriver -->
<!-- org.hsqldb.jdbcDriver -->
<property name="driverClassName" value="com.p6spy.engine.spy.P6SpyDriver" />
<property name="url"
value="jdbc:hsqldb:hsql://localhost:9002/sillycat" />
<property name="username" value="sillycat" />
<property name="password" value="kaishi" />
</bean>
</beans>
hibernate的配置文件,applicationContext-hibernate.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/sillycat/core/model/hbm</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
</props>
</property>
<property name="useTransactionAwareDataSource" value="true"></property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" />
</beans>
ibatis的配置文件,applicationContext-ibatis.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- ======================================================================== -->
<!-- iBatis SQL map定义。 -->
<!-- ======================================================================== -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>
classpath:/sqlmap-config.xml
</value>
</property>
<property name="useTransactionAwareDataSource" value="true"></property>
</bean>
</beans>
在DAO层配置两个BEAN,applicationContext-core-dao.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName" default-lazy-init="true">
<bean id="userHibernateDao"
class="com.sillycat.core.dao.hibernate.UserDaoHibernateImpl" />
<bean id="userIbatisDao"
class="com.sillycat.core.dao.ibatis.UserDaoIbatisImpl" />
</beans>
在业务层,UserManagerImpl.java里面,想用哪个用哪个咯:
package com.sillycat.core.service.impl;
import java.util.List;
import com.sillycat.core.dao.UserDao;
import com.sillycat.core.model.User;
import com.sillycat.core.service.UserManager;
import com.sillycat.plugin.commons.base.BaseService;
import com.sillycat.plugin.commons.exceptions.DaoException;
import com.sillycat.plugin.commons.exceptions.ManagerException;
public class UserManagerImpl extends BaseService implements UserManager {
private UserDao userHibernateDao;
private UserDao userIbatisDao;
public List getAllUser() {
List list = null;
try {
list = userIbatisDao.getAll();
} catch (DaoException e) {
throw new ManagerException("DaoException:", e);
}
return list;
}
public User getUser(Integer id) {
if (id == null || id.intValue() == 0) {
throw new ManagerException("id can't be null when get model!");
}
User user = null;
try {
user = userHibernateDao.get(id);
} catch (DaoException e) {
throw new ManagerException("DaoException:", e);
}
return user;
}
public void saveUser(User user) {
if (user == null) {
throw new ManagerException("model can't be null when save!");
}
try {
userHibernateDao.save(user);
} catch (DaoException e) {
throw new ManagerException("DaoException:", e);
}
}
public void removeUser(Integer id) {
if (id == null || id.intValue() == 0) {
throw new ManagerException("id can't be null when delete model!");
}
try {
userHibernateDao.delete(id);
} catch (DaoException e) {
throw new ManagerException("DaoException:", e);
}
}
public void setUserHibernateDao(UserDao userHibernateDao) {
this.userHibernateDao = userHibernateDao;
}
public void setUserIbatisDao(UserDao userIbatisDao) {
this.userIbatisDao = userIbatisDao;
}
}