biz包:
package com.etc.biz; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.etc.entity.Animal; public interface AnimalBiz
{
List<Animal> findAll();
Animal findById(int aid);
void add(Animal an);
}
-----------------------------------------------------------------------------------------------
package com.etc.biz; import java.util.List; import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.etc.dao.AnimalDao;
import com.etc.entity.Animal; public class AnimalBizImp implements AnimalBiz
{
//让业务持有dao的抽象接口,通过spring注入dao实例
private AnimalDao dao; public AnimalDao getDao() {
return dao;
} public void setDao(AnimalDao dao) {
this.dao = dao;
} public List<Animal> findAll()
{ return dao.findAll();
} public Animal findById(int aid)
{
return dao.findById(aid);
} public void add(Animal an)
{
dao.add(an);
}
}
---------------------------------------------------------------------------------------------
dao包:
package com.etc.dao; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.etc.entity.Animal; public interface AnimalDao
{
List<Animal> findAll();
Animal findById(int aid);
void add(Animal an);
}
----------------------------------------------------------------------------------------------
package com.etc.dao; import java.util.List; import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.etc.entity.Animal; public class AnimalDaoImp extends HibernateDaoSupport implements AnimalDao
{
public List<Animal> findAll()
{
String hql = "from Animal";
return this.getHibernateTemplate().find(hql);
} public Animal findById(int aid)
{
return this.getHibernateTemplate().get(Animal.class, aid);
} public void add(Animal an)
{
this.getHibernateTemplate().save(an);
}
}
--------------------------------------------------------------------------------------------
test包: package com.etc.test; import java.util.List; import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.etc.biz.AnimalBiz;
import com.etc.dao.AnimalDao;
import com.etc.entity.Animal; public class Test
{
public static void main(String[] args)
{
BeanFactory fac = new ClassPathXmlApplicationContext("applicationContext.xml"); AnimalBiz biz = (AnimalBiz) fac.getBean("biz"); List<Animal> list = biz.findAll(); for(Animal a:list)
System.out.println(a); /*Animal an = biz.findById(1);
System.out.println(an);*/ /*try
{
Animal an = new Animal("火鸡", 2);
biz.add(an);
System.out.println("插入成功!");
}
catch (Exception e)
{
System.out.println("插入失败!");
e.printStackTrace(); }
*/
}
}
---------------------------------------------------------------------------------------------
applicationContext.xml配置: <?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="file:src/hibernate.cfg.xml">
</property>
</bean>
<!-- 创建dao对象 实现类-->
<bean id="dao" class="com.etc.dao.AnimalDaoImp">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 创建biz对象实现类 -->
<bean id="biz" class="com.etc.biz.AnimalBizImp">
<property name="dao" ref="dao"/>
</bean>
<!-- 创建事务管理器 -->
<bean id="tm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!-- 配置事务通知的规则 -->
<tx:advice id="myadvice" transaction-manager="tm">
<tx:attributes> <!-- 配置事务的传播特性 -->
<!-- add方法需要事务-->
<tx:method name="add*" propagation="REQUIRED"/> <tx:method name="find*" read-only="true"/> <!-- find开头的方法只读,不能修改数据 -->
</tx:attributes>
</tx:advice>
<!-- 配置切入 -->
<aop:config>
<aop:pointcut expression="execution(* com.etc.biz.AnimalBiz.*(..))" id="mypc"/>
<!-- 引用tx通知,引用切线mypc -->
<aop:advisor advice-ref="myadvice" pointcut-ref="mypc"/>
</aop:config>
</beans>

  

spring+hibernate ---含AOP--事务--laobai的更多相关文章

  1. JavaWeb_(Spring框架)Spring中的aop事务

    1.事务相关知识 a)什么是事务:把多条数据库操作捆绑到一起执行,要么都成功,要么都失败: b)事务的原则ACID: i.原子性:事务包含的所有操作,要么全部成功,要么全部失败回滚,成功全部应用到数据 ...

  2. spring+hibernate中的事务

    上下文: 从数据库服务器上获取数据可以,保存的时候增加了事务提交,即em.flush方法,报错no transaction in progress 报错信息: no transaction in pr ...

  3. Spring学习笔记(四)—— Spring中的AOP

    一.AOP概述 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...

  4. Spring MVC+Spring +Hibernate配置事务,但是事务不起作用

    最近做项目,被一个问题烦恼了很久.使用Spring MVC+Spring +Hibernate开发项目,在使用注解配置事务管理,刚开始发现无论如何数据库都无法更新,但是可以从数据库查询到数据.怀疑是配 ...

  5. Spring,hibernate,struts的面试笔试题(含答案)

    Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Sesssion 4.创建事务Transation 5.持久 ...

  6. spring中使用aop配置事务

    spring的事务配置有5种方式,这里记录其中的一种:基于tx/aop声明式事务配置 在之前spring aop介绍和示例这篇所附代码的基础上进行测试 一.添加save方法 1.在testDao类里添 ...

  7. 解决在Spring整合Hibernate配置tx事务管理器出现错误的问题

    问题描述: Error occured processing XML 'org/aopalliance/intercept/MethodInterceptor'. See Error Log for ...

  8. spring+hibernate管理多个数据源(非分布式事务)

    本文通过一个demo,介绍如何使用spring+hibernate管理多个数据源,注意,本文的事务管理并非之前博文介绍的分布式事务. 这个demo将使用两个事务管理器分别管理两个数据源.对于每一个独立 ...

  9. 使用spring+hibernate+atomikos+tomcat构建分布式事务

    本文通过一个demo,介绍如何使用spring+hibernate+atomikos+tomcat构建在一个事务中涉及两个数据源的web应用. demo功能:实现一个能成功提交和回滚的涉及两个数据库数 ...

随机推荐

  1. spring mvc: 页面重定向调整

    我的项目名称是hello, 在src/main/java目录下面建了一个chapter2目录 有三个配置文件: web.xml, chapter2-servlet.xml, applicationCo ...

  2. mysql注入介绍

    0. SQL注入常用的尝试语句: or 1=1--+ 'or 1=1--+ "or 1=1--+ )or 1=1--+ ')or 1=1--+ ") or 1=1--+ " ...

  3. 转载 IO、文件、NIO【草案四】

    本章目录: 1.IO类相关内容 2.文件和目录 3.文件高级操作  NIO详解[1]——缓冲区(Buffer)[深入理解,总结自<Java-NIO>]: [*:下边的Buffer又指代抽象 ...

  4. UI- 五种手势识别总结

    #pragma mark - 手势  总共有五种手势  分别为 Tap点击 Pan拖拽 LongPress长时间按压 Pinch捏合手势 rotation旋转 1. 定义成员变量 UIImageVie ...

  5. javascript 的智能提示intellisence用法

    转载自:http://blog.csdn.net/applewangpai/article/details/23517087   引用指令reference Visual Studio 2012支持的 ...

  6. git合并分支与解决冲突

    前提: 当前开发的分支为feature/20161129_317606_algoplatform_1,由于feature/20161130_322574_tmstools_1分支有新内容,所以准备将f ...

  7. (三)canvas绘制样式

    beginPath() 对画线点的一个开始限制 moveTo() 画线的起点,只在开头使用 参数两个x轴,y轴 lineTo() 后续连线 两个参数x轴,y轴 stroke() 连线无填充 fill( ...

  8. mac地址常识及获取

    mac常识: 网卡地址这个概念有点混淆不清.因为实际上有两个地址,mac地址和物理地址,一般说网卡地址我是指物理地址,不知道别人怎么看?物理地址指的是网卡上的存放地址的ROM里的地址,mac地址是这块 ...

  9. 深入理解java虚拟机-第六章

    第6章 类文件 6.3 Class类文件的结构 Class文件是一组以8位字节为基础单位的二进制流. Class文件格式采用一种类似C语言结构伪结构存储数据,这种伪结构中只有两种数据类型:无符号数和表 ...

  10. 利用Github免费搭建个人主页(个人博客)

    之前闲着, 利用Github搭了个免费的个人主页. 涉及: Github注册 Github搭建博客 域名选购 绑定域名 更多 一  Github注册 在地址栏输入地址:http://github.co ...