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的更多相关文章

  1. Struts+Spring+Hibernate项目的启动线程

    在Java Web项目中,经常要在项目开始运行时启动一个线程,每隔一定的时间就运行一定的代码,比如扫描数据库的变化等等.要实现这个功能,可以现在web.xml文件中定义一个Listener,然后在这个 ...

  2. SSH面试题(struts2+Spring+hibernate)

    struts2 + Spring +hibernate Hibernate工作原理及为什么要用?   原理:   1.读取并解析配置文件   2.读取并解析映射信息,创建SessionFactory ...

  3. SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>

    此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...

  4. Struts,spring,hibernate三大框架的面试

    Struts,spring,hibernate三大框架的面试 1.Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3 ...

  5. struts2+spring+hibernate(SSH)框架的搭建和总结

    SSH框架:struts2+spring+hibernate,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. struts2+spring+hibernat ...

  6. SSH框架简化(struts2+spring+hibernate)

    目的: 通过对ssh框架有了基础性的学习,本文主要是使用注解的方式来简化ssh框架的代码编写. 注意事项: 1.运行环境:Windows 8-64位,Eclipse(开发工具),jdk1.8.0_91 ...

  7. 用eclipse搭建SSH(struts+spring+hibernate)框架

    声明: 本文是个人对ssh框架的学习.理解而编辑出来的,可能有不足之处,请大家谅解,但希望能帮助到大家,一起探讨,一起学习! Struts + Spring + Hibernate三者各自的特点都是什 ...

  8. Struts2 + Spring + Hibernate

    Struts2 + Spring + Hibernate整合. 使用的是无配置方法进行SSH的整合,struts-convertion plugin + spring annotation + hib ...

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

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

随机推荐

  1. 曼慧尼特u检验(两个样本数据间有无差异)

    曼-惠特尼U检验(Mann-Whitney检验) How the Mann-Whitney test works Mann-Whitney检验又叫做秩和检验,是比较没有配对的两个独立样本的非参数检验. ...

  2. Linux下安装libiconv使php支持iconv函数

    libiconv组件安装好了可以让我们php支持iconv函数了,这个函数的作用就是字符编码强制转换了,下面和111cn小编一起来看一个Linux中安装libiconv使php支持iconv函数的例子 ...

  3. Bete冲刺第三阶段

    Bete冲刺第三阶段 今日工作: web: 检索了各类资料,今日暂时顺利解决了hibernate懒加载异常的问题,采用的凡是也比较简单就是添加了一个OpenSessionInViewFilter的过滤 ...

  4. Java--剑指offer(8)

    36.输入两个链表,找出它们的第一个公共结点. 解题思路:这里主要是把两个链表的节点都放入两个栈中,这样就可以按照出栈的方式来比较节点,因为单链表只要是有相同的节点,那么之后的节点也都是一样的,所以如 ...

  5. iOS开发小技巧--UIButton的另一种布局方法(第一种在layoutSubViews方法中,这一种利用苹果提供的两个返回CGRect的方法)

  6. 如何让div显示在embed,flash元素之上

    Z-INDEX属性只对块状元素有效,对于flash是没用的,那么我们怎么处理这个问题呢,问大家介绍两种很简便的方法 方法一 把<embed>标记写在<object>之内 方法二 ...

  7. Activity的四种launchMode

    来源:http://blog.csdn.net/liuhe688/article/details/6754323/ 我们今天要讲的是Activity的四种launchMode. launchMode在 ...

  8. 简进祥--iOS开发基础知识

    1:App跳转至系统Settings 跳转在IOS8以上跟以下是有区别的,如果是IOS8以上可以如下设置: NSURL *url = [NSURL URLWithString:UIApplicatio ...

  9. BZOJ 4384: [POI2015]Trzy wieże

    4384: [POI2015]Trzy wieże Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 217  Solved: 61[Submit][St ...

  10. 基于Token的身份验证——JWT

    初次了解JWT,很基础,高手勿喷. 基于Token的身份验证用来替代传统的cookie+session身份验证方法中的session. JWT是啥? JWT就是一个字符串,经过加密处理与校验处理的字符 ...