SSh框架整合

来源:互联网 发布:优质的企业排名优化 编辑:程序博客网 时间:2024/05/16 18:00

第一步,不用说就是导jar包了


先写spring的框架,要有application的xml文件,然后我们在web.xml文件里面随着服务器的启动而启动设置文件的路径


application的配置文件

<?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:aop="http://www.springframework.org/schema/aop" 
xmlns:context="http://www.springframework.org/schema/context" 
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/aop 
http://www.springframework.org/schema/aop/spring-aop-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/tx 
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">


<bean name="Useraction" class="Action.UserAction"></bean>
</beans>


web.xml文件的设置


 <!-- 配置spring容器,随着项目的启动而启动 -->
  <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>


==================================================

接着就是struts的整合


整合的时候是有两个常量要我们配置

<!-- struts.objectFactory = spring 将Action的创建交给spring,默认是关闭的
struts.objectFactory.spring.autoWire = name spring自动准备action依赖,默认是打开的,不用配置
-->

<constant name="struts.objectFactory " value="spring"></constant>

Spring和Struts的整合方案一

<!-- 整合方案一: class属性上仍然配置action的完整类名
strits2仍然创建action,由spring负责组装action中的依赖属性

一个负责创建,一个负责注入,所以这种方案不推荐使用
-->

<action name="userAction_*" class="Action.UserAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
这里注入的时候我们是通过set方式注入的,在我们的application里面把这个service在工厂里面创建下
Spring和Struts的整合方案一二

由Spring创建Action对象以及装配

Struts中的配置

<!-- 整合方案二: class属性填写我们在Spring容器中创建的beanname
需要手动装配组装依赖属性
-->

<action name="userAction_*" class="Useraction" method="{1}">
<result name="success">/success.jsp</result>
</action>

application中的配置

<bean name="Useraction" class="Action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>

============================================

单独配置Hibernate,和之前 的配置一样,把和当前线程绑定和隔离级别交给spring管理了

<hibernate-configuration>
    <session-factory>
    
         <!-- 数据库驱动 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
         <!-- 数据库url -->
        <property name="hibernate.connection.url">jdbc:mysql:///hibernate_test</property>
         <!-- 数据库连接用户名 -->
        <property name="hibernate.connection.username">root</property>
         <!-- 数据库连接密码 -->
        <property name="hibernate.connection.password">12345678</property>

        <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="Bean/Student.hbm.xml" />
        <mapping resource="Bean/Teacher.hbm.xml" />
        <mapping resource="Bean/User.hbm.xml" />
        <mapping resource="Bean/Rolm.hbm.xml" />
    </session-factory>
</hibernate-configuration>


====================================

整合Hibernate


<!-- 将SessionFactory配置到spring中 -->
<bean name="SessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 加载配置的方案一:使用外部的hibernate.cfg.xml -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>


<!-- 配置方案二:在spring中配置hibernate配置信息 -->
<property name="hibernateProperties">
<props>
<!-- 必选的五个选项 -->
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql:///hibernate_test</prop>
<prop key="hibernate.connection.username">root</prop>
<prop key="hibernate.connection.password">12345678</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映射 ,指定所在的包路径-->
<property name="mapping" value="classpath:Bean"></property>


====================整合c3p0连接池=====================


<!-- 配置c3p0链接池 -->
<bean name="dataSource" class="om.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${db.url}"></property>
<property name="driverClass" value="${db.driver}"></property>
<property name="user" value="${db.user}"></property>
<property name="password" value="${db.passwod}"></property>
</bean>

<!-- 读取外部文件propertiers -->
<context:property-placeholder location="classpath:db.propertiers"/>



这里配置好了之后我们就将数据库的链接链接去管理

将连接池注入到sessionfactory中

<bean name="SessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<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映射 ,指定所在的包路径-->
<property name="mapping" value="classpath:Bean"></property>
</bean>

======================spring整合hibernate操作数据库=======================

dao层需要继承HibernatedaoSuuppot


hibernate模板的操作

@Override
public User getByUserCode(final String usercode) {
//HQL
return getHibernateTemplate().execute(new HibernateCallback<User>() {
@Override
public User doInHibernate(Session session) throws HibernateException {
String hql = "from User where user_code = ? ";
Query query = session.createQuery(hql);
query.setParameter(0, usercode);
User user = (User) query.uniqueResult();
return user;
}
});
//Criteria
/*DetachedCriteria dc = DetachedCriteria.forClass(User.class);

dc.add(Restrictions.eq("user_code", usercode));

List<User> list = (List<User>) getHibernateTemplate().findByCriteria(dc);

if(list != null && list.size()>0){
return list.get(0);
}else{
return null;
}*/
}


或者是我们在application中注入sessionFactory

<bean name="userDao" class="UserDaoImpl" >
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>


然后在方法里面直接写getHibernateTemplate().save(u);就可以


===================SpringAop事物=======================

<!-- 核心事务管理器 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>

<!-- 配置通知 -->
<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(* service.impl.*ServiceImpl.*(..))" id="txPc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
</aop:config> -->


====================扩大session作用范围===============

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

解决方案:

配置咱们的filter

<!-- 扩大session作用范围
  注意: 任何filter一定要在struts的filter之前调用
   -->


<!--以后我们要加任何的filter都要在控制器之前去配置-->
   <filter>
  <filter-name>openSessionInView</filter-name>
  <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  </filter>

<filter-mapping>
  <filter-name>openSessionInView</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>

原创粉丝点击