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开发项目,在使用注解配置事务管理,刚开始发现无论如何数据库都无法更新,但是可以从数据库查询到数据.怀疑是配 ...
 
随机推荐
- Predicting purchase behavior from social media-www2013
			
1.Information publication:www2013 author:Yongzheng Zhang 2.What 用社交媒体用户特征 预测用户购买商品类别(排序问题) 3.Dataset ...
 - Ubuntu更改右键菜单
			
方法/步骤1.这是我们在桌面文件夹ubuntugege上打开的右键菜单,你说你在~/.gnome2/nautilus-scripts/添加的右键菜单项目但它就是没有显示呀,于是你觉得Ubuntu 12 ...
 - tr命令
			
tr命令是linux下一个字符处理命令,用途: 字符替换 字符删除 字符压缩形式:tr [OPTION]... SET1 [SET2]接口:输入输出都是标准流,所以要通过管道来调用这 ...
 - webpack 插件: html-webpack-plugin
			
插件地址:https://www.npmjs.com/package/html-webpack-plugin 这个插件用来简化创建服务于 webpack bundle 的 HTML 文件,尤其是对于在 ...
 - mvc4 ajax.beginform表单验证
			
@{ Layout = null; } @model MvcApplication1.Models.User @using (Ajax.BeginForm("create", &q ...
 - Java--剑指offer(6)
			
26.输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. /** public class TreeNode { int val = 0 ...
 - 让nodeJS支持ES6的词法----babel的安装和使用
			
要使用Babel, 我们需要nodeJS的环境和npm, 主要安装了nodeJS, npm就默认安装了 , 现在安装nodeJS很简单了, 直接下载安装就好了: 安装es-checker 在使用Bab ...
 - CentOS6.5下搭建NFS文件服务器
			
本文参考这里 CentOS下搭建NFS服务器总结 环境介绍: 1. 服务器: 192.168.0.100 2. 客户机: 192.168.0.101 安装软件包: 服务器和客户机都要安装nfs 和 r ...
 - 华硕笔记本BIOS设置详解
			
BIOS是英文Basic Input/Output System的缩写,即基本输入输出系统,合理的BIOS设置可以让我们的电脑更好的为我们服务,但由于大部分笔记本的BIOS界面是英文,并且BIOS里面 ...
 - 4位组合型Excel文档密码怎么破解
			
现代社会我们会遇到各种密码,很多的密码我们一段时间不用就不知不觉的忘记了.很多的excel用户就遇到过这种情况,这个时候我们就需要一款Excel密码破解工具.Advanced Office Passw ...