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
分享到:
相关推荐
spring_hibernate整合实例 spring_hibernate整合实例
《JAVA WEB整合开发实例精通:Struts+Hibernate+Spring (1碟)》适用于Java开发Web应用和J2EE方面的初学者,大专院校学生,相关培训班教材
SpringMVC+Hibernate+Spring整合实例源码下载,很完整的例子,需要的可以下载源码学习
Struts Spring Hibernate 开发实例
项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 二、 项目目的: 整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + ...
JAVA WEB整合开发实例精通:Struts+Hibernate+Spring
StrutsSpringHibernate实例
基于SpringMVC以及Spring,Hibernate的非注解入门小demo
本文并不想介绍Struts,Spring,Hibernate的原理系统架构等,本文地目的是通过一个较复杂地实例介绍如何整合Struts,Spring,Hibernate,网上现有的例子虽然也能达到目的,但功能都比较单一,复杂的例子时会有...
Hibernate与Struts2和Spring组合开发实例
spring mvc,spring, hibernate 整合登录实例 代码简洁 简单易懂 易读
网上店铺源码学习实例_基于Struts-Hibernate-Spring
struct 2 + spring + hibernate 学习实例 开发投票管理系统
Spring+Hibernate 使用TransactionInterceptor声明式事务配置
实现了Spring+Hibernate+MySql的融合,通过部分修改调整,可以直接运行.本人亲自测试过,总结和不能运行原因已归纳在文件中
structs+spring+hibernate实例
Struts2_Spring_Hibernate整合开发实例 同名视频的完整资料 超值
Spring Hibernate实例
spring+hibernate 框架实例源码 myeclipse 下创建的一个spring+hibernate实例
一个spring+struts+hibernate的例子,是eclipse的工程,用tomcat5和mysql,文件夹下包含所有的源码和库,另外还有一个.sql的文件用于建立数据库。大家觉得它有什么不好,欢迎交流