【SSH框架】系列之 Spring 整合 Hibernate 框架
2018-03-04 11:44
621 查看
1、SSH 三大框架整合原理
Spring 与 Struts2 的整合就是将 Action 对象交给 Spring 容器来负责创建。
Spring 与 Hibernate 的整合就是将 SessionFactory 交给 Spring 容器来负责维护,并且 Spring 容器负责 Session 维护以及相关的 AOP 事务。
2、Spring 整合 Hibernate 框架
(1)、新建 web 项目,导入 Spring 和 Hibernate 框架所需要的 jar 包,如下图所示:(2)、单独配置 Spring 容器,具体配置如下:
applicationContext.xml创建配置文件,并导入约束
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "> </beans>
web.xml配置 Spring 随项目启动
<!-- 让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>(3)、单独配置 Hibernate书写实体类与 orm 元数据
配置主配置文件
hibernate.cfg.xml
<?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>(4)、Spring 整合 Hibernate在 Spring 容器中配置SessionFactory
<!-- 在 Spring 配置中放置 hibernate 配置信息 --> <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" > <!-- 将连接池注入到 sessionFactory, hibernate 会通过连接池获得连接 --> <property name="dataSource" ref="dataSource" ></property> <!-- 配置 hibernate 基本信息 --> <property name="hibernateProperties"> <props> <!-- 必选配置 --> <!-- <prop key="hibernate.connection.driver_class" >com.mysql.jdbc.Driver</prop> <prop key="hibernate.connection.url" >jdbc:mysql:///crm_32</prop> <prop key="hibernate.connection.username" >root</prop> eebb <prop key="hibernate.connection.password" >1234</prop> --> <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop> <!-- 可选配置 --> <prop key="hibernate.show_sql" >true</prop> <prop key="hibernate.format_sql" >true</prop> <prop key="hibernate.hbm2ddl.auto" >update</prop> </props> </property> <!-- 引入 orm 元数据,指定orm元数据所在的包路径,spring 会自动读取包中的所有配置 --> <property name="mappingDirectoryLocations" value="classpath:com/spring/domain" ></property> </bean>(5)、Spring 整合 hibernate 环境操作数据库创建 Dao 类继承 HibernateDaoSupport
Spring 中配置 action service dao
<!-- 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 模板进行具体操作
扫描关注微信公众号,了解更多
相关文章推荐
- springMVC系列之(四) spring+springMVC+hibernate 三大框架整合
- springMVC系列之(四) spring+springMVC+hibernate 三大框架整合
- springMVC系列之(四) spring+springMVC+hibernate 三大框架整合
- springMVC系列之(四) spring+springMVC+hibernate 三大框架整合
- 待看-springMVC系列之(四) spring+springMVC+hibernate 三大框架整合
- springMVC系列之(四) spring+springMVC+hibernate 三大框架整合
- springMVC系列之(四) spring+springMVC+hibernate 三大框架整合
- 浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓
- spring+springMVC+hibernate 三大框架整合
- Struts2,Spring3,Hibernate4整合--SSH框架
- Mybatis 系列(2)——SSM(spring mvc+spring+Mybatis)框架整合
- 用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能
- Spring4 学习系列之——整合基于注解的Hibernate实例
- Struts2SpringHibernate框架的简单整合1
- 最近S2SH框架整合和简单测试 Struts2-2.3.14+Spring-4.0.6+Hibernate-4.3.6
- Spring+Hibernate的整合项目框架
- 手动配置三大框架整合,Spring+Struts2+Hibernate
- 搭建整合Struts2,Spring,Hibernate的Web开发框架
- ssh整合思想初步 structs2 Spring Hibernate三大框架各自要点
- 使用MyEclipse整合ssh(Struts、Spring、Hibernate)三大框架(环境搭载+实例源码下载)