第一步:配置文件

 <?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:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->
<context:annotation-config/>
<!-- 配置dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@192.168.1.10:1521:orcl"/>
<property name="username" value="tsrescue"/>
<property name="password" value="123456"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>cn/itcast/bean/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>这个配置不需要!!!!!!!!!!!!!!
</props> </property>
</bean>
<!-- spring提供的针对hibernate的事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 基于注解的方式声明事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id = "personService" class="cn.itcast.Service.Impl.PersonServiceBean"/>
</beans>

代码部分:

1.person.java

 package cn.itcast.bean;

 public class Person {
private Integer id;
private String name; public Person() {} public Person(String name) {
this.name = name;
} public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }

2.Person.hbm.xml

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-6-1 10:08:44 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping package="cn.itcast.bean">
<class name="Person" table="person">
<id name="id">
<generator class="native" />
</id>
<property name="name"/>
</class>
</hibernate-mapping>

3.PersonService 接口

 package cn.itcast.Service;

 import java.util.List;

 import cn.itcast.bean.Person;

 public interface PersonService {

     public abstract void save(Person person);

     public abstract void update(Person person);

     public abstract Person getPerson(Integer personId);

     public abstract void delete(Integer personId);

     public abstract List<Person> getPersons();

 }

4.PersonService接口的实现类PersonServiceBean

 package cn.itcast.Service.Impl;

 import java.util.List;

 import javax.annotation.Resource;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import cn.itcast.Service.PersonService;
import cn.itcast.bean.Person;
@Transactional
public class PersonServiceBean implements PersonService {
@Resource
private SessionFactory sessionFactory; @Override
public void save(Person person){
//取被spring容器管理的session
sessionFactory.getCurrentSession().persist(person);
} @Override
public void update(Person person){
sessionFactory.getCurrentSession().merge(person);
} @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
@Override
public Person getPerson(Integer personId){
return (Person) sessionFactory.getCurrentSession().get(Person.class, personId); } @Override
public void delete(Integer personId){
sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(Person.class, personId));
} @Transactional(propagation =Propagation.NOT_SUPPORTED,readOnly = true)
@SuppressWarnings("unchecked")
@Override
public List<Person> getPersons(){
return sessionFactory.getCurrentSession().createQuery("from person").list(); }
}

5.测试类

 package cn.itcast.Service.Impl;

 import static org.junit.Assert.*;

 import java.util.List;

 import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.Service.PersonService;
import cn.itcast.bean.Person; public class PersonServiceBeanTest {
private static PersonService ps;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
ps = (PersonService) ctx.getBean("personService");
} catch (Exception e) {
e.printStackTrace();
}
} @Test
public void testSave() {
ps.save(new Person("小明3"));
} @Test
public void testUpdate() {
Person person = ps.getPerson(1);
person.setName("小明修改版");
ps.update(person);
} @Test
public void testGetPerson() {
Person p = ps.getPerson(1);
System.out.println(p.getName());
} @Test
public void testDelete() {
ps.delete(2);
} @Test
public void testGetPersons() {
List<Person> list = ps.getPersons();
for (Person person : list) {
person.getName();
}
} }

spring2.5整合hibernate3.0的更多相关文章

  1. spring2.5整合hibernate3.0整合Struts

    首先:这是spring framework+hibernate+struts集成,spring主要用于aop和ioc,hibernate主要是用于持久层,struts主要是用于mvc. 同时关于spr ...

  2. [转载]Struts2.1.6+Spring2.5.6+Hibernate3.3.

    原文地址:Struts2.1.6+Spring2.5.6+Hibernate3.3.1全注解实例详解(一)(转载大象)作者:沉睡森林  在JavaEE企业级开发中,以SSH2框架为核心的应用非常广,大 ...

  3. Spring3 整合Hibernate3.5 动态切换SessionFactory (切换数据库方言)

    一.缘由 上一篇文章Spring3.3 整合 Hibernate3.MyBatis3.2 配置多数据源/动态切换数据源 方法介绍到了怎么样在Sping.MyBatis.Hibernate整合的应用中动 ...

  4. hibernate3.0 org.dom4j.DocumentException: Connection timed out: connect Nested exception:

    hibernate3.0 org.dom4j.DocumentException: Connection timed out: connect Nested exception:   所报异常: 严重 ...

  5. Hibernate3.0配置

    我的系统Win10(64x),Eclipse jee 2018-09 ,Sql2018版本. 以下是Hibernate3.0配置包 链接:https://pan.baidu.com/s/10Kizby ...

  6. Maven整合Spring3.0+Mybatis3.2+Struts2.3+查找坐标+jar包依赖(五)

    依赖传递 只添加了一个struts2-core依赖,发现项目中出现了很多jar,这种情况 叫 依赖传递

  7. QQ登录整合/oauth2.0认证-04-调整到QQ互联进行QQ登录

    ---------------------------------目录------------------------------------- QQ登录整合/oauth2.0认证-03-对第二节的代 ...

  8. QQ登录整合/oauth2.0认证-03-对第二节的代码改进

    ---------------------------目录---------------------------------- QQ登录整合/oauth2.0认证-01-申请appkey和appid ...

  9. QQ登录整合/oauth2.0认证-02-跳转到QQ互联页

    ---------------------------目录---------------------------------- QQ登录整合/oauth2.0认证-01-申请appkey和appid ...

随机推荐

  1. 1-3-1 关于API

    主要内容:API函数及其相关内容的介绍.Windows编程相关基础知识介绍 1.API函数的概念 <1>API(Application Programming interface),即应用 ...

  2. HDU-4777 Rabbit Kingdom(区间更新求和)

    题目大意:给一个n个整数的数列,q次询问,每次询问区间[l,r]中与区间中其它数互质的数的个数.. 题目分析:离线处理,这里以询问区间的左端点从小到大的顺序为例.为了叙述方便,用f(l,r)表示区间[ ...

  3. HDU-4003 Find Metal Mineral (树形DP+分组背包)

    题目大意:用m个机器人去遍历有n个节点的有根树,边权代表一个机器人通过这条边的代价,求最小代价. 题目分析:定义状态dp(root,k)表示最终遍历完成后以root为根节点的子树中有k个机器人时产生的 ...

  4. 黑马程序员——JAVA基础之简述集合collection

    ------- android培训.java培训.期待与您交流! ---------- 集合: 为什么出现集合类? •  面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对 ...

  5. java多线程:jdk并发包的总结(转载)

    转载地址:http://blog.csdn.net/yangbutao/article/details/8479520 1.java 高并发包所采用的几个机制(CAS,volatile,抽象队列同步) ...

  6. AssetStore资源名字

    NGUI A Pathfinding Project Pro PlayMaker 2DToolkit Scene Manager UniLOD UniLUA Save Game-JSON+Binary ...

  7. C# TextBox 只能输入数字

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { TextBox txt = sender as TextBox ...

  8. Unity3d 适配机型

    1,为了是更多机型能够安装你的游戏,Unity3d Device Filter设置:ARMv6 with VFP: 2,华为C8600,一运行强制停止: 参考网址:http://forum.unity ...

  9. vb6 webbrowser 事件捕获

    Private WithEvents htmlDocument As htmlDocument Private WithEvents btnCompute As MSHTML.HTMLButtonEl ...

  10. ASP.NET 各种缓存

    原文地址:http://www.cnblogs.com/ltp/archive/2009/06/30/1514311.html   有时候总听到网友说网站运行好慢,不知如何是好:有时候也总见到一些朋友 ...