首页 > 代码库 > spring+hibernate ---laobai

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="http://www.mamicode.com/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