spring+hibernate ---含AOP--事务--laobai
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的更多相关文章
- JavaWeb_(Spring框架)Spring中的aop事务
1.事务相关知识 a)什么是事务:把多条数据库操作捆绑到一起执行,要么都成功,要么都失败: b)事务的原则ACID: i.原子性:事务包含的所有操作,要么全部成功,要么全部失败回滚,成功全部应用到数据 ...
- spring+hibernate中的事务
上下文: 从数据库服务器上获取数据可以,保存的时候增加了事务提交,即em.flush方法,报错no transaction in progress 报错信息: no transaction in pr ...
- Spring学习笔记(四)—— Spring中的AOP
一.AOP概述 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...
- Spring MVC+Spring +Hibernate配置事务,但是事务不起作用
最近做项目,被一个问题烦恼了很久.使用Spring MVC+Spring +Hibernate开发项目,在使用注解配置事务管理,刚开始发现无论如何数据库都无法更新,但是可以从数据库查询到数据.怀疑是配 ...
- Spring,hibernate,struts的面试笔试题(含答案)
Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Sesssion 4.创建事务Transation 5.持久 ...
- spring中使用aop配置事务
spring的事务配置有5种方式,这里记录其中的一种:基于tx/aop声明式事务配置 在之前spring aop介绍和示例这篇所附代码的基础上进行测试 一.添加save方法 1.在testDao类里添 ...
- 解决在Spring整合Hibernate配置tx事务管理器出现错误的问题
问题描述: Error occured processing XML 'org/aopalliance/intercept/MethodInterceptor'. See Error Log for ...
- spring+hibernate管理多个数据源(非分布式事务)
本文通过一个demo,介绍如何使用spring+hibernate管理多个数据源,注意,本文的事务管理并非之前博文介绍的分布式事务. 这个demo将使用两个事务管理器分别管理两个数据源.对于每一个独立 ...
- 使用spring+hibernate+atomikos+tomcat构建分布式事务
本文通过一个demo,介绍如何使用spring+hibernate+atomikos+tomcat构建在一个事务中涉及两个数据源的web应用. demo功能:实现一个能成功提交和回滚的涉及两个数据库数 ...
随机推荐
- spring3: Bean的命名与Bean的实例化
http://jinnianshilongnian.iteye.com/blog/1413857 2.3.1 XML配置的结构 一般配置文件结构如下: <beans> <impor ...
- WPF中的事件列表 .
以下是WPF中的常见事件汇总表(按字母排序),翻译不见得准确,但希望对你有用. 事件 描述 Annotation.AnchorChanged 新增.移除或修改 Anchor 元素时发生. Annota ...
- Ajax-04 jQuery Ajax 常用操作
jQuery jQuery 其实就是一个JavaScript的类库,其将复杂的功能做了上层封装,使得开发者可以在其基础上写更少的代码实现更多的功能. jQuery Ajax a.概述 jQuery 不 ...
- Chrome浏览器导出数字证书
1.F12打开开发者工具,选中"Security"面板-->找到"View certificate",点击 2.选中“详细信息”面板-->复制到文件
- C#笔记之 函数可变参数
(转自:http://blog.csdn.net/jackluangle/article/details/6539278) 其实函数的参数的可变是因为不确定函数的参数大小的原因才使用的.看下面一个列子 ...
- 【河南第十届省赛-D】年终奖金
题目描述 ***公司承接了N个项目需要年底完成,每个项目有一定的难度系数.由于项目太多了,需要招聘大量的技术人员.要求每个技术人员至少完成K个项目. 考虑到有些项目之间相似性以及项目的难易程度,为了避 ...
- Idea_00_资源贴
一.精选资料 tengj/IntelliJ-IDEA-Tutorial IntelliJ IDEA 使用教程-极客学院 二.参考资料 eclipse&Myeclipse&Intelli ...
- OS快捷键
OS X 键盘快捷键 若要使用键盘快捷键或按键组合,您可以同时按修饰键和字符键.例如,同时按下 Command 键(标有 符号的按键)和“c”键会将当前选中的任何内容(文字.图形等)拷贝至夹纸板.这也 ...
- 指针和引用在C++中应用
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...
- CentOS 7下sqlite3的问题修复
Centos7下的nltk启动问题 CentOS 7, Python 3.6,ipython 6.0.0 问题描述 ipython 启动ipython命令 import nltk 爆出以下的错误信息: ...