Sprin2.5+Hibernate3.3集成

1.添加jar文件

Hibernate3.3核心安装包下的
/-----------------------------------/
hibernate3.jar
lib\required\*.jar
lib\optional\ehcache-1.2.3.jar
hibernate注解安装包下的
lib\test\slf4j-log4j12.jar
/-----------------------------------/
Spring安装包下的
/-----------------------------------/
dist\spring.jar
dist\modules\spring-webmvc-struts.jar
lib\jakaarte-commons\commons-logging.jar、commons-dbcp.jar、commons-pool.jar
lib\aspectj\aspectjweaver.jar 、 aspectjrt.jar
lib\cglib\cglib-nodep-2.1.3.jar
lib\j2ee\common-annoutations.jar
lib\log4j\log4j-1.2.15.jar
/------------------------------------/
数据库驱动

2. 配置beans.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: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/tx
http://www.springframework.org/schema/tx/spring-tx-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">
<!--导入外部properties -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!--配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!--连接池启动时的初始值 -->
<property name="initialSize" value="${initialSize}" />
<!--连接池的最大值 -->
<property name="maxActive" value="${maxActive}" />
<!--最大空闲值,当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释 放一部分一直减少到maxIdle为止 -->
<property name="maxIdle" value="${maxIdle}" />
<!--最小空闲值,当经空闲的连接邵谊阀值时,连接池就会申请一些连接, 以免洪峰来时来不及申请 -->
<property name="minIdle" value="${minIdle}" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>cn/soldier/bean/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!-- 指定数据库方言 -->
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</prop>
<!-- 是否根据需要每次自动创建数据库 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- 显示Hibernate持久化操作所生成的SQL -->
<prop key="hibernate.show_sql">true</prop>
<!-- 将SQL脚本进行格式化后再输出 -->
<prop key="hibernate.format_sql">false</prop>

<!-- <prop key="hibernate.cache.use_second_level_cache">true</prop> -->
<!-- <prop key="hibernate.cache.use_query_cache">true</prop> -->
<!-- <prop key="hibernate.provider_class">org.hibernate.cache.EhCacheProvider</prop> -->
</props>
</property>
</bean>

<!--配置事务管理器 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!--启用@Transaction注解的支持- -->
<tx:annotation-driven transaction-manager="txManager" />
<!--使用field方法注入依赖对象 -->
<context:annotation-config />
<!-- -->
<bean id="personService" class="cn.soldier.service.impl.PersonServiceBean"></bean>
</beans>

3.新建实体bean Person
package cn.soldier.bean;
Person.java
public class Person {
private Long id;
private String name;

public Person() {
}

public Person(String name) {
this.name = name;
}

public Person(Long id, String name) {
this.id = id;
this.name = name;
}

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 String toString() {
return "Person [id=" + id + " name=" + name + "]";
}

}
4.建立对应的数据库映射
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">

<hibernate-mapping package="cn.soldier.bean">

<class name="Person" table="person">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
</class>
</hibernate-mapping>

5.建立访问Person实体的Service接口
package cn.soldier.service;

import java.util.List;
import cn.soldier.bean.Person;

public interface PersonService {

public abstract void save(Person person);

public abstract void update(Person person);

public abstract void delete(Long id);

public abstract Person getPerson(Long id);

public abstract List<Person> getPersons();
}

6.建立访问Person实体的Service实现方法
package cn.soldier.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.soldier.bean.Person;
import cn.soldier.service.PersonService;

@Transactional //启用事务管理
public class PersonServiceBean implements PersonService {
@Resource//使用filed注入SessionFactory对象
SessionFactory sessionFactory;

public void save(Person person) {
// 得到容器里面正在被Transaction管理的Session
// 如果使用opeSession(),获得的Session对象是不受Transaction管理的。
Session session = (Session) sessionFactory.getCurrentSession();
session.persist(person);
}

public void update(Person person) {
sessionFactory.getCurrentSession().merge(person);// SaveOrUpdate
}

public void delete(Long id) {
// load方法性能最佳,因为get方法有一个数据装配的过程
sessionFactory.getCurrentSession().delete(
sessionFactory.getCurrentSession().load(Person.class, id));
}

@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)// 不启用事务管理,只读
public Person getPerson(Long id) {
return (Person) sessionFactory.getCurrentSession()
.get(Person.class, id);
}
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
@SuppressWarnings("unchecked")
// 终止警告
public List<Person> getPersons() {
return sessionFactory.getCurrentSession().createQuery("From Person").list();
}
}
7.测试开发好的PersonServiceBean
package junit.test;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.soldier.bean.Person;
import cn.soldier.service.PersonService;

public class PersonServiceBeanTest {
private static PersonService personService;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"beans.xml");
personService = (PersonService) ctx.getBean("personService");

} catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void testSave() {
for (int i = 0; i <10; i++) {
Person person = new Person("逗比---"+i+"---号");
personService.save(person);
}
}

@Test
public void testUpdate() {
Person person = personService.getPerson(1L);
person.setName("逗比2号");
personService.update(person);
System.out.println(personService.getPerson(1L));
}

@Test
public void testDelete() {
personService.delete(2L);
}

@Test
public void testGetPersons() {
for (Person person : personService.getPersons()) {
System.out.println(person);
}
}
}

时间: 2024-10-26 06:16:21

Sprin2.5+Hibernate3.3集成的相关文章

Strut1.3+Sprin2.5+Hibernate3.3集成

1.添加jar文件 Hibernate3.3核心安装包下的/-----------------------------------/hibernate3.jarlib\required\*.jarlib\optional\ehcache-1.2.3.jarhibernate注解安装包下的lib\test\slf4j-log4j12.jar/-----------------------------------/Spring安装包下的/-------------------------------

Sprin2.5+Hibernate3.3+Struts2.0.11集成

1.添加jar文件 Hibernate3.3核心安装包下的/-----------------------------------/hibernate3.jarlib\required\*.jarlib\optional\ehcache-1.2.3.jarhibernate注解安装包下的lib\test\slf4j-log4j12.jar/-----------------------------------/Spring安装包下的/-------------------------------

注解形式:Spring3.2 MVC + Hibernate3.6 集成 (基础篇)

创建web项目(此处就不再多说了,既然都开始使用SH了,那么我相信你已经使用过一段时间的java了) 下载jar包,这里提供我的网盘下载地址,省得大家找了 hibernate3.6:http://yunpan.cn/csdyPZvcbunad  访问密码 0c53 spring3.2:http://yunpan.cn/csdyME836dvYA  访问密码 07c2 public void regCode(HttpServletRequest request, HttpServletRespon

Spring集成Hibernate3

Spring集成Hibernate3同集成Jdbc一样,提供了模板,使得我们只需要关心数据表的操作,资源的关闭打开.异常的处理交给Spring来完成,这个模板就是HibernateTemplate.HibernateTemplate(DAO支持类)通过SessionFactory来获取,所以看一下如何在XML中配置SessionFactory //数据源 <bean  id="dataSource"class="org.springframework.jdbc.data

maven构建ssh时出的一些错误

ERROR - Context initialization failedorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; neste

spring+hibernate注解配置实例

简单的spring3.2.9和hibernate3的集成配置,有demo供下载.shTest下载 第一步 jdbc.properties配置 driverClassName=com.mysql.jdbc.Driver url=jdbc\:mysql\://localhost\:3306/shtest username=root password=123456 prototypeCount=1 maxActive=100 houseKeepingSleepTime=60000 minimumCon

maven构建ssh项目的出现的问题以及解决方法

ssh版本:spring3.2.2,hibernate3.3.2,struts2 2.3.14 问题一: ERROR - Context initialization failedorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/application

Caused by: java.lang.ClassNotFoundException: javassist.util.proxy.MethodFilter

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateBaseDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not a

在Spring3 MVC中五步配置集成注解方式Hibernate3

最近在搞一个WEB项目,以前在公司做项目用的都是JPA做ORM持久层,这次这个项目是我自己接的,我决定改一下,用Hibernate3来做ORM持久层.于是我网上搜索了Hibernate3怎么配置集成到Spring3 MVC上,发现千奇百怪,而且很多都是不是基于注解方式配置,显然那些文字上面的配置方式已经跟如今的Hibernate3注解支持方式脱节了,于是我决定自己搞一把,首先说一下网上那些配置方式的不好的地方,很多文章都提到要jdbc.properties文件与Hibernate config文