【SSH框架】系列之 Spring 整合 Hibernate 框架

目录

 

SSH 三大框架整合原理

 Spring整合Hibernate

Spring单配

hibernate映射文件配置方式

 1、mappingResources

2、mappingLocations

3、mappingDirectoryLocations

4、mappingJarLocations


SSH 三大框架整合原理

  • Spring 与 Struts2 的整合就是将 Action 对象交给 Spring 容器来负责创建。

  • Spring 与 Hibernate 的整合就是将 SessionFactory 交给 Spring 容器来负责维护,并且 Spring 容器负责 Session 维护以及相关的 AOP 事务。

Spring整合Hibernate

Spring单配

applicationContext.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-3.1.xsd   
	http://www.springframework.org/schema/context   
	http://www.springframework.org/schema/context/spring-context-3.1.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

 web.xml

 <!-- 让spring随web启动而创建的监听器 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置spring配置文件位置参数 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

Hibernate单配

配置configuration文件命名为hibernate.cfg.xm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>

         <!-- 数据库驱动 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
         <!-- 数据库url -->
        <property name="hibernate.connection.url">jdbc:mysql:///hbDB</property>
         <!-- 数据库连接用户名 -->
        <property name="hibernate.connection.username">root</property>
         <!-- 数据库连接密码 -->
        <property name="hibernate.connection.password">root</property>
        <!-- 数据库方言
            注意: MYSQL在选择方言时,请选择最短的方言.
         -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>


        <!-- 将hibernate生成的sql语句打印到控制台 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 将hibernate生成的sql语句格式化(语法缩进) -->
        <property name="hibernate.format_sql">true</property>
        <!-- 
        自动导出表结构. 自动建表
         -->
        <property name="hibernate.hbm2ddl.auto">update</property>

         <!-- 引入实体配置文件 -->
        <mapping resource="com/spring/domain/*.hbm.xml" />
        <mapping resource="com/spring/domain/*.hbm.xml" />
        <mapping resource="com/spring/domain/*.hbm.xml" />

    </session-factory>
</hibernate-configuration>

Spring与Hibernate整合

整合Hibernate主要的是获取其会话工厂SessionFactory。Spring配置获取的方式为

<!-- 在 Spring 配置中放置 hibernate4(注意使用的版本) 配置信息 -->
<!-- 配置hibernate会话工厂 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- Hibernate。cfg配置 -->
		<!-- <property name="hibernateProperties" value="classpath:hibernate.cfg.xml"></property> -->
		<property name="hibernateProperties">
			<value>
				hibernate.dialect=org.hibernate.dialect.OracleDialect
				hibernate.hbm2ddl.auto=update
				hibernate.show_sql=true
				hibernate.format_sql=update
			</value>
		</property>
		<!-- 实体类映射,通过mappingSources注入(需要配置很多实体类) -->
		<!-- <property name="mappingResources">
			<list>
				<value>com/zc/entity/User.hbm.xml</value>
			</list>
		</property> -->
		<!-- 实体类映射,使用mappingLocations可以使用通配符配置路径 -->
		<property name="mappingLocations" value="classpath:com/ozc/entity/*.hbn.xml"></property>
	</bean>

Spring整合Hibernate环境操作数据库

创建的Dao类需要继承HibernateDaoSupport

在Spring配置文件中配置action dao 以及service

<!-- action -->
    <!-- Action对象作用范围一定是多例的 -->
    <bean name="*Action" class="com.spring.web.action.*Action" scope="prototype" >
        <property name="*Service" ref="*Service" ></property>
    </bean>
    <!-- service -->
    <bean name="*Service" class="com.spring.service.impl.*ServiceImpl" >
        <property name="*demo" ref="*Dao" ></property>
    </bean>
    <!-- dao -->
    <bean name="*Dao" class="com.spring.dao.impl.*DaoImpl" >
        <!-- 注入sessionFactory -->
        <property name="sessionFactory" ref="sessionFactory" ></property>
    </bean>

hibernate映射文件配置方式

通过Spring整合Hibernate,hibernate的映射配置可以全权交给Spring来处理,首先得配置一个SessionFactory节点,在其间配置注入方式,说到配置一定要在<property>节点,单说到mapping文件name属性有一下几种:

-- mappingResources

-- mappingLocations

-- mappingDirectoryLocations

-- mappingJarLocations

 1、mappingResources

使用该注入方式,则可以指定classpath下具体映射文件名,如果需要映射多个实体类则需要在list节点中配置多个xml文件,配置需要注意的就是mapping文件的路径。

方式一:直接配置映射的文件名

<property name="mappingResources" value=“userTest.hbm.xml”/>

方式二:配置单个映射,在value节点中配置

<property name="mappingResources"> 
    <value>orz.hbm.xml </value> 
</property>

方式三:配置多个映射,mapping文件配置在list中 

<property name="mappingResources">
    <list>
        <value>entity1.hbm.xml </value>
        <value>com/entity2.hbm.xml </value>
        <value>com/ozc/entity.hbm.xml </value>
    </list>
</property>

当配置文件变得越来越多,阅读和修改起来也越来越麻烦,而且基于XML的配置也可能带来输入的错误,导致你可能因为一个字符的错误而浪费半天时间去寻找错误。

2、mappingLocations

这个注入方式则更为灵活,而且可以减少配置的量,可以指定任何文件路径,并且可以指定前缀:classpath、file等,在其中可以用通配符,使用classpath这里要注意的是classpath指的是工程路径下的classes文件夹下的内容,所以要注入包内的配置,包名前的"/"可加可不加,Spring会自动识别该路径下的所有配置文件。所以更为推荐这种注入方式

<!-- 可以指定任何文件路径 -->
<property name="mappingLocations">
    <value>/WEB-INF/petclinic.hbm.xml </value>
</property>

<!-- 指定前缀:classpath、file等 -->
<property name="mappingLocations">
    <value>classpath:/com/ozc/domain/petclinic.hbm.xml </value>
    <value>classpath:/com/ozc/domain/*.hbm.xml </value>
</property>

 以上使用通配符的配置是在com/ozc/domain包下所有hbm.xml文件都被加载为映射文件

3、mappingDirectoryLocations

指定映射的文件路径(该注入方式不太常用)

<!-- 指定映射的文件路径 -->
<property name="mappingDirectoryLocations">
    <list>
        <value>WEB-INF/HibernateMappings</value>
    </list>
</property>

<!-- 也可以通过classpath来指出 -->
<property name="mappingDirectoryLocations">
    <list>
        <value>classpath:/pg/package/</value>
    </list>
</property>

4、mappingJarLocations

指定加载的映射文件在jar文件中(较为少用)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值