SSH整合终结版(一)

本文主要讨论Spring2.5+Hibernate3.2+Struts1.2.9的整合。

人们通常讲“一口吃不成胖子”,SSH的整合也是一样的,最好是按步骤一步一步来。

整合之前必需的工作是将Spring、Hibernate和Struts所需的jar文件准备好(为文章叙述更为调理,这步工作不在此赘述,详情请看作者的另一篇日志《SSH整合所需jar文件的下载》)。本项目采用数据库为MySQL5.1,MyEclipse版本为5.5.1

下面我们利用Eclipse工具创建一个Web项目(Web Project),项目名称为SSH,建议大家采用J2EE 5.0规范。

首先,我们对Spring和Hibernate进行整合

第一步,将Spring、Hibernate所需jar包拷贝到WEB-INF/lib目录中。

第二步,在类路径下创建Spring配置文件beans.xml。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/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" >

</beans>

beans.xml模板文件也可以从Spring帮助文档(documentation)中获得。

第三步,在beans.xml中添加数据源、事务管理器等配置。内容如下:

    <!-- 数据源配置 -->
    <bean id="dataSource" class= "org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
    <property name="url"  value="jdbc:mysql://localhost:3306/itcast?userUnicode=true&amp;characterEncoding=UTF-8"/>
    <property name="username" value="root"/>
    <property name="password" value="admin"/>
    <property name="initialSize" value="1"/>
    <property name="maxActive" value="500"/>
    <property name="maxIdle" value="2"/>
    <property name="minIdle" value="1"/>
    </bean>
   
   <!-- spring 的LocalSessionFactoryBean对sessionFactory进行了包装,从而对session进行管理-->
       <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSource"/>
      <property name="mappingResources">
       <list>
         <value>com/lyk/bean/Person.hbm.xml</value>
       </list>
      </property>
      <property name="hibernateProperties">
         <value>
               hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
               hibernate.hbm2ddl.auto=update
               hibernate.show_sql=false
               hibernate.format_sql=false
        </value>
     </property>
    </bean>
    <!-- Spring提供的针对hibernate的事务管理器 -->
   <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
          <property name="sessionFactory" ref="sessionFactory"/>
   </bean>

    <context:annotation-config />   //Spring 为我们提供了一种方便的注册这些 BeanPostProcessor 的方式,详情参看Spring帮助文档

      <!-- 采用注解方式声明事务,以后采用transaction就打开了事务管理器 -->
      <tx:annotation-driven transaction-manager="txManager"/>

到这里,beans.xml文件基础部分已配置完毕,大家可以看到红色部分显示的是Hiberbate中定义的实体bean的映射文件,下面我们创建实体bean---com.lyk.bean.Person.

第四步,创建com.lyk.bean.Person。在src目录下创建com.lyk.bean包,在该包中创建Person类,如下:

package com.lyk.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;
 }

}

第五步,接下来定义Person类与MySQL数据库的映射文件。

文件名: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="com.lyk.bean">

 <class name="Person" table="PERSON">
  <id name="id" column="PERSON_ID">
   <generator class="native"/>
  </id>
   <property name="name" column="PERSON_NAME" />  
 </class>
 </hibernate-mapping>

至此,我们已经配置好了beans.xml文件,准备好了实体bean--com.lyk.bean.Person及其数据库映射文件Person.hbm.xml。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值