第一步:配置文件

 <?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. 2016 Hunan Province Programming Contest

    2016 Hunan Province Programming Contest A. 2016 题意 \(1 \le a \le n, 1 \le b \le m\) ,其中\(1 \le n,m \ ...

  2. centos rm 回收站

    桌面版本的linux就默认有,非桌面版本貌似没(反正我没找到) linux rm删除文件之后,恢复就比较麻烦了,即使恢复了,文件名格式都变成一串数字了. 修改root用户的环境变量 vi ~/.bas ...

  3. ExtJS Panel主要配置列表

    Ext.panel.Panel 属性 值 描述 animCollapse Boolean 设置面板折叠展开是否显示动画,Ext.Fx可用默认true,否则false applyTo Mixed 面板定 ...

  4. eclipse关掉jsp,js的语法验证

    转载:http://blog.csdn.net/testcs_dn/article/details/39058959 eclipse加速之禁用JS.jsp等文件的语法验证 去除eclipse的JS验证 ...

  5. 搭建最简单的SpringMVC框架(使用maven)

    本文说明:本文介绍使用maven搭建SpringMVC最简单的框架程序过程,适合初学者上手. 下载链接 点此进入下载链接 1.创建一个maven webapp工程. 2.修改WEB-INF目录下的we ...

  6. svn常见错误总结

    1. svn: Server sent unexpected return value (500 Internal Server Error) in response to OPTIONS reque ...

  7. centos6.4 网络适配器设置仅主机模式

    网络适配器设置仅主机模式时: 1.vmnet1网卡必须开启

  8. Andorid面试问题整理

    Acitivty的四中启动模式与特点. standard:默认的启动模式 singleTop:适合那种接受通知启动的页面,比如新闻客户端之类的,可能会给你推送好几次 ,但是每次都是打开同一张页面调用o ...

  9. oracle更新统计信息以及解锁统计信息

    begin dbms_stats.unlock_table_stats(ownname => 'DM_MPAY', tabname => 'PLAT_INFO');end; begin d ...

  10. Font Awesome字体图标

    1.什么是字体图标字体图标是一个包含许多图标的字体库.可以理解为一种特殊的字体,只不过里面包含的都是图标. 2.Font Awesome图标字体库Font Awesome是目前最受欢迎最全面的图标字体 ...