【SSH】Hibernate+Struts2+Spring整合

来源:互联网 发布:遗传算法密码学 编辑:程序博客网 时间:2024/05/22 14:30

一、三大框架架构(整合原理)


二、jar包整合

1、SSH版本

struts-2.3.34

spring-4.3.9

hibernate-5.2.11

2、hibernate(12个)


3、Struts2(14个)


4、Spring(16个)


5、标签库


三、单独配置Spring容器

1、在src创建配置文件applicationContext.xml,并导入约束(4个)beans|context|aop|tx

<?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 "><bean name="userAction" class="com.sh.web.action.UserAction"></bean></beans>

2、在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>

四、单独配置Struts2

1、在src下配置Struts2主配置文件struts.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><package name="crm" namespace="/" extends="struts-default"><action name="UserAction_*" class="com.sh.web.action.UserAction" method="{1}"><result name="success">/success.jsp</result></action></package></struts>

2、配置struts2核心过滤器到web.xml

<!-- struts2核心过滤器 -->  <filter>      <filter-name>struts2</filter-name>      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>   </filter>   <filter-mapping>      <filter-name>struts2</filter-name>      <url-pattern>/*</url-pattern>   </filter-mapping>  

五、整合Struts2与Spring

1、导包(已导)

struts2-spring-plugin-2.3.34.jar

2、在struts.xml中配置常量


3、整合方案

(1)方案一:Struts2自己创建Action,Spring负责组装依赖属性(了解)


不推荐理由:最好由Spring完整管理Action生命周期,Spring功能才应用daoAction上

(2)方案二:Spring负责创建以及组装依(重要)

applicationCotext.xml

struts.xml

六、单独配置Hibernate

1、导入实体类&orm元数据


2、配置主配置文件 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:///ssh_01</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><!-- 引入实体配置文件 --><!-- 引入元数据 路径书写:填写src下的路径 --><mapping resource="com/sh/domain/CstCustomer.hbm.xml" /><mapping resource="com/sh/domain/CstLinkMan.hbm.xml" /><mapping resource="com/sh/domain/SysUser.hbm.xml" /><mapping resource="com/sh/domain/SysRole.hbm.xml" /></session-factory></hibernate-configuration>

七、Spring整合Hibernate

1、整合原理

将sessionFactory对象交给spring容器管理

2、配置方案

(1)方案一(了解)


(2)方案二(重要)

<!-- 配置方案二:在spring配置中放置hibernate配置信息 --><bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><!-- 配置Hibernate基本数据 --><property name="hibernateProperties"><!-- 必选配置 --><props><prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop><prop key="hibernate.connection.url">jdbc:mysql:///hibernate_01</prop><prop key="hibernate.connection.username">root</prop><prop key="hibernate.connection.password">root</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/sh/domain"></property></bean>

八、Spring整合C3P0连接池

1、准备db.properties

jdbc.jdbcUrl=jdbc:mysql:///hibernate_01jdbc.driverClass=com.mysql.jdbc.Driverjdbc.user=rootjdbc.password=root

2、引入连接池到Spring中


3、将连接池注入到sessionFactory


九、Spring整合Hibernate环境操作数据库

1、Dao类的创建继承HibernateDaoSupport


2、Hibernate模板的操作

(1)execute
@Overridepublic SysUser getByUserCode(final String usercode) {//HQLreturn getHibernateTemplate().execute(new HibernateCallback<SysUser>() {@Overridepublic SysUser doInHibernate(Session session) throws HibernateException {String hql = "from SysUser where userCode = ? ";Query query = session.createQuery(hql);query.setParameter(0, usercode);SysUser user = (SysUser) query.uniqueResult();return user;}});
(2)findByCriteria
//CriteriaDetachedCriteria dc = DetachedCriteria.forClass(SysUser.class);dc.add(Restrictions.eq("userCode", usercode));List<SysUser> list = (List<SysUser>) getHibernateTemplate().findByCriteria(dc);if(list != null && list.size()>0){return list.get(0);}else{return null;}

3、Spring中配置dao


十、Spring中的AOP事务

1、配置核心事务管理器


2、XML配置AOP事务

(1)配置通知
(2)配置织入
<!-- 配置通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager" ><tx:attributes><tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" /><tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" /></tx:attributes></tx:advice><!-- 配置将通知织入目标对象  --><aop:config><!-- 配置切点 --><aop:pointcut expression="execution(* com.sh.service.impl.*ServiceImpl.*(..))" id="txPc"/><!-- 配置切面 --><aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" /></aop:config>

3、注解配置AOP事务

(1)开启注解事务

(2)Service类中使用注解

十一、扩大session作用范围

为了避免使用懒加载时出现no-session问题,需要扩大sesson的作用范围
配置filter


代码地址:https://github.com/AmazeLee/SSH.git
原创粉丝点击