spring+hibernate ---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 ---laobai的更多相关文章
- Struts+Spring+Hibernate项目的启动线程
在Java Web项目中,经常要在项目开始运行时启动一个线程,每隔一定的时间就运行一定的代码,比如扫描数据库的变化等等.要实现这个功能,可以现在web.xml文件中定义一个Listener,然后在这个 ...
- SSH面试题(struts2+Spring+hibernate)
struts2 + Spring +hibernate Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory ...
- SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>
此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...
- Struts,spring,hibernate三大框架的面试
Struts,spring,hibernate三大框架的面试 1.Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3 ...
- struts2+spring+hibernate(SSH)框架的搭建和总结
SSH框架:struts2+spring+hibernate,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. struts2+spring+hibernat ...
- SSH框架简化(struts2+spring+hibernate)
目的: 通过对ssh框架有了基础性的学习,本文主要是使用注解的方式来简化ssh框架的代码编写. 注意事项: 1.运行环境:Windows 8-64位,Eclipse(开发工具),jdk1.8.0_91 ...
- 用eclipse搭建SSH(struts+spring+hibernate)框架
声明: 本文是个人对ssh框架的学习.理解而编辑出来的,可能有不足之处,请大家谅解,但希望能帮助到大家,一起探讨,一起学习! Struts + Spring + Hibernate三者各自的特点都是什 ...
- Struts2 + Spring + Hibernate
Struts2 + Spring + Hibernate整合. 使用的是无配置方法进行SSH的整合,struts-convertion plugin + spring annotation + hib ...
- Spring MVC+Spring +Hibernate配置事务,但是事务不起作用
最近做项目,被一个问题烦恼了很久.使用Spring MVC+Spring +Hibernate开发项目,在使用注解配置事务管理,刚开始发现无论如何数据库都无法更新,但是可以从数据库查询到数据.怀疑是配 ...
随机推荐
- CSS3 GRID LAYOUT
CSS3 GRID LAYOUT http://www.w3cplus.com/blog/tags/356.html 中国首个开源 HTML5 跨屏前端框架 http://amazeui.org/
- 【Python】[函数] 函数的参数与递归函数
一.函数的参数1.位置参数2.默认参数 n就是默认参数 def power(x,n=2): s=1 while n > 0: n = n - 1 s = s * x return s 默认参数有 ...
- C++类功能扩展预留五招
第一招虚函数 通过派生类来进行功能扩展是基本的面向对象的方式,这种方式大如下: class base { public: virtual ~base(){} virtual void fun() { ...
- git工作流程
git工作流程 一般工作流程如下: 克隆 Git 资源作为工作目录. 在克隆的资源上添加或修改文件. 如果其他人修改了,你可以更新资源. 在提交前查看修改. 提交修改. 在修改完成后,如果发现错误,可 ...
- openwrt的环境搭建、定制和编译
参考1:编译openwrt全过程 参考2: ARM9的OpenWRT系统的移植以及 无线视觉操控系统的软件开发 参考3:搭建OpenWrt开发环境(包括编译过程) 参考4:各个openwrt版本的sv ...
- 访问修饰符(public,private,protected,internal,sealed,abstract)
为了控件C#中的对象的访问权限,定义对象时可以在前面添加修饰符. 修饰符有五种:private(私有的),protected(受保护的),internal(程序集内部的),public(公开的),以及 ...
- [转]VirtualBox – Error In supR3HardenedWinReSpawn 问题解决办法
原文地址:http://chenpeng.info/html/3510 Genymotion 模拟器安装好虚拟机后,启动时报错: —————————VirtualBox – Error In supR ...
- CNN中的BN
w权值的初始化,之前最好的方法是 也就是输入神经元和输出神经元中随机一个数,然后除以输入神经元的个数开根号 因为全连接层波动较大,所以加在FC后面 这里Y,b是为了对信息的一个还原
- c#学习<二>:数据类型
基元类型 编译器直接支持的数据类型称为基元类型(primitive type).基元类型直接映射到Framework类库(FCL)中存在的类型(BCL是FCL的子集). C#中的基元类型 BCL类型 ...
- JS搞基指南----延迟对象入门提高资料整理
JavaScript的Deferred是比较高大上的东西, 主要的应用还是主ajax的应用, 因为JS和nodeJS这几年的普及, 前端的代码越来越多, 各种回调套回调再套回调实在太让人崩溃, ...