spring和hibernate整合浅析
作为一个orm框架,hibernate对jdbc进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库.而通常情况下,我们都是把hibernate和spring整合使用.那么,整合使用有什么意义呢?下面做一个简单的测试.
现在我们分别使用hibernate的方法和spring为我们提供的方法来实现一个Dao接口,假设连接池的最大连接数为50.先看Dao层代码.
public interface UserDao {/** * 根据主键查询user实体 */public User getById(long id);}
@Repository("hibernateDao")public class HibernateDaoImpl extends BaseDaoImpl implements UserDao {public User getById(long id) {Session s = getHibernateTemplate().getSessionFactory().openSession();return (User) s.get(User.class, id);}}
@Service("hibernateService")public class HibernateServiceImpl implements UserService {@Autowiredprivate UserDao hibernateDao;public void cache() {for (int i = 0; i < 100; i++) {hibernateDao.getById(1);System.out.println("当前执行次数==========" + (i + 1));}}public void overflow() {for (int i = 0; i < 100; i++) {hibernateDao.getById(i);System.out.println("当前执行次数==========" + (i + 1));}}}
public class HibernateServiceTest extends TestCase {/** * 装载spring 配置文件 */ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");/** * 执行效果:打印50条sql,没有走缓存.执行到第51条的时候报错. 说明:1.用 * hibernate的getSessionFactory().openSession()方法每次都会开启一个新的session,而且执行完不会自动关闭 * */public void testCache() {UserService hibernateService = SpringContextUtil.getBean("hibernateService");hibernateService.cache();}/** * 效果同上 */public void testOverflow() {UserService hibernateService = SpringContextUtil.getBean("hibernateService");hibernateService.overflow();}}
@Repository("springDao")public class SpringDaoImpl extends BaseDaoImpl implements UserDao {public User getById(long id) {return getHibernateTemplate().get(User.class, id);}}.
@Service("springService")public class springServiceImpl implements UserService {@Autowiredprivate UserDao springDao;public void cache() {for (int i = 0; i < 100; i++) {springDao.getById(1);System.out.println("当前执行次数==========" + (i + 1));}}public void overflow() {for (int i = 0; i < 100; i++) {springDao.getById(i);System.out.println("当前执行次数==========" + (i + 1));}}}
public class SpringServiceTest extends TestCase {/** * 装载spring 配置文件 */ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");/** * 执行效果: 执行100次,只打印一条sql,后面的99次全部走缓存. 这说明 本service中的一个service方法用的是一个session */public void testCache() {UserService springService = SpringContextUtil.getBean("springService");springService.cache();}/** * 执行效果: 执行全部的100条sql * 上面的结论,本service中的一个service方法用的是一个session,说明session只最后关闭一次 */public void testOverflow() {UserService springService = SpringContextUtil.getBean("springService");springService.overflow();}}
<!--设置事务管理 --> <bean id="transactionManager" /> </property> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="cache*" /> <tx:method name="update*" /> <tx:method name="delete*" /> <tx:method name="*" read-only="true" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="serviceMethod" expression="execution(* *..service.*Service.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" /> </aop:config>
public class SpringManagerTest extends TestCase {/** * 装载spring 配置文件 */ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");/** * 执行100次,不走缓存 */public void testCache() {UserManager springManager = SpringContextUtil.getBean("springManager");springManager.cache();}/** * 执行100次,不走缓存 */public void testOverflow() {UserManager springManager = SpringContextUtil.getBean("springManager");springManager.overflow();}}
@Service("springService")public class SpringServiceImpl implements UserService {@Autowiredprivate UserDao springDao;public void cache() {User user = new User();user.setName("name1");springDao.save(user);List<User> list = springDao.list();for (User user1 : list) {System.out.println(user1.getName());}}}