`

spring学习实例之spring对hibernate的支持

阅读更多
1、包结构:


2、源代码:
Book.java
package domain;

public class Book {
	private long id;
	private String name;
	private double price;
	
	public Book(){
		
	}
	
	public Book(String name,double price){
		this.name=name;
		this.price=price;
	}
	
	public Book(long id,String name,double price){
		this(name,price);
		this.id=id;
	}
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	
	public String toString(){
		return "id:"+id+"-name:"+name+"-price:"+price;
	}
	
}

Book.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
	<class name="domain.Book">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"></property>
		<property name="price"></property>
	</class>
</hibernate-mapping>

BookDao.java
package dao;

import java.util.List;
import domain.Book;

public interface BookDao {
	void save(Book b);
	void update(Book b);
	void delete(long id);
	Book findById(long id);
	List<Book> findByPrice(double from,double to);
}

BookDaoImpl.java
package dao.hibernate;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import dao.BookDao;
import domain.Book;

public class BookDaoImpl extends HibernateDaoSupport implements BookDao{

	public void delete(long id) {
		Book b=findById(id);
		this.getHibernateTemplate().delete(b);
	}

	public Book findById(long id) {
		return (Book)this.getHibernateTemplate().get(Book.class, id);
	}

	public List<Book> findByPrice(double from, double to) {
		String hql="from Book b where b.price between ? and ?";
		return this.getHibernateTemplate().find(hql, new Object[]{from,to});
	}

	public void save(Book b) {
		this.getHibernateTemplate().save(b);
	}

	public void update(Book b) {
		this.getHibernateTemplate().update(b);
	}

}

applicationContext_hibernate.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
		<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:myorcl"></property>
		<property name="username" value="myorcl"></property>
		<property name="password" value="embed"></property>
	</bean>
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>domain/Book.hbm.xml</value>
			</list>
		</property>
	</bean>
	
	<bean id="bookDao" class="dao.hibernate.BookDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
</beans>

BookDaoTest.java
package test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dao.BookDao;
import domain.Book;

public class BookDaoTest {
	public static void main(String[]args){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext_hibernate.xml");
		BookDao dao=(BookDao)ac.getBean("bookDao");
		dao.save(new Book("三个火枪手",44.9));
		
		List<Book> books=dao.findByPrice(30, 60);
		for(Book book:books){
			System.out.println(book);
		}
	}
}


3、补充:加入事务的spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
	 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
	 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
		<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:myorcl"></property>
		<property name="username" value="myorcl"></property>
		<property name="password" value="embed"></property>
	</bean>
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>domain/Book.hbm.xml</value>
			</list>
		</property>
	</bean>
	
	<bean id="bookDao" class="dao.hibernate.BookDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean> 
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
			<tx:method name="*" propagation="REQUIRED"  rollback-for="MyAppException"/>
		</tx:attributes>
	</tx:advice>	
	<aop:config>
		<aop:pointcut id="bookDaoPc" expression="execution(* dao.BookDao.*(..))"/>
		<aop:advisor  advice-ref="txAdvice" pointcut-ref="bookDaoPc"/>
	</aop:config>
</beans>

  • 大小: 10.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics