SSH三大框架整合

 

SSH整合过程

注意: 在整合框架的过程中,每一大步都要测试,这样容易排查出问题,出了问题重头开始。

1先将 hibernate中的包全部导入进来 10+1

 

jpa  注意   10个包+1个 驱动包

2.导入Struts2 所有包 14

 

导包时 如果发现是同一个包,版本不同一定要将其中之一删掉,原则是删掉版本低

再导入 struts2和spring整合的包

 

 

一旦导入了这个包,struts2在启动的时候会自动寻找spring容器,如果没有配置spring,就会出现异常

3)导入spring的所有包 16

4+2 基本的包

2+2     aop

4  c3p0 tx springjdbc 数据库驱动    springJdbc

1 test

1 spring- web包

 

1 spring-orm包

 


3.测试spring配置是否成功

  首先创建一个applicationContext.xml

  导入四个约束: beans context aop tx

  web.xml中配置监听器

<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

3. 单独配置struts2

      先把struts2插件包删除

 

   然后书写 struts.xml 文件 导入约束 书写刚刚的userAction的配置

web.xml中配置struts2的过滤器

测试

4. springstruts2

(1) 刚刚删除的包导入进来

(2) 添加常量

<!--struts.objectFactory = spring  action的创建交给spring-->
<constant name="struts.objectFactory" value="spring"></constant>
<!--  struts.objectFactory.spring.autoWire = name
     由spring负责装配action的属性
 -->
<constant name="struts.objectFactory.spring.autoWire" value="name"></constant>
<!---->
<constant name="struts.devMode" value="true"></constant>

(3) spring管理action (方法2种)

: struts2撞见action 由spring负责struts2的属性注入

Struts2的配置文件

<package name="main" namespace="/" extends="struts-default">
    <action name="userAction_*" class="cn.hd.action.UserAction" method="{1}">
        <result name="success">/index.jsp</result>
    </action>
</package>

Spring的配置文件

<bean name="userService" class="cn.hd.service.impl.UserServiceImpl"></bean>
<bean name="userAction" class="cn.hd.action.UserAction">
        <property name="userService" ref="userService"></property>
</bean>

 

: 完全交给spring 去管理action

 在配置action时class的属性值改为在spring中的bean的名字

 Struts配置文件

<package name="main" namespace="/" extends="struts-default">
    <action name="userAction_*" class="cn.hd.action.UserAction" method="{1}">
        <result name="success">/index.jsp</result>
    </action>
</package>

Spring配置文件

<bean name="userService" class="cn.hd.service.impl.UserServiceImpl"></bean>
<bean name="userAction" class="cn.hd.action.UserAction" scope="prototype">
        <property name="userService" ref="userService"></property>
</bean>

 注意: spring配置action 要配置的类的创建模式为多例。

5. 单独整合hibernate

(1)书写实体类 orm数据库源文件

(2)书写核心配置文件

    将线程绑定事务隔离级别删除,后面使用spring管理事务

(3)书写测试类,测试hibernate导入成功 单独配置成功

@Test
public void fun() {
    Configuration conf = new Configuration().configure();
    SessionFactory sessionFactory = conf.buildSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();

    User user = new User();
    user.setMoney(1000);
    user.setName("李四");
    user.setPsd("123456");
    User user1 = session.get(User.class, 1);
    Serializable save = session.save(user);
    
    transaction.commit();
    session.close();
}

      //游离状态下 savaOrUpdate 会调用 update 方法 所以会报错

6. hibernatespring结合    

  实际上结合的原理就是 hibernate当中的sessionfactory 和事务交给spring管理

有两种方案:

: hibernate中的配置文件不动,在spring中的文件中配置session factory对象

<!--配置hibernate-->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!--读取配置文件-->
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

 

书写测试代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 {
    @Resource(name = "sessionFactory")
    private SessionFactory sessionFactory;
    @Test
    public void fun(){
        Session session = sessionFactory.openSession();
        User user = session.get(User.class, 1);
        System.out.println(user);
    }
}

 

: 不适用hibernate配置文件,所有的配置文件写入到spring配置文件当中

<!--配置hibernate-->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!--第一种方案 读取配置文件-->
        <!--<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>-->
        <!--第二种方案-->
        <property name="hibernateProperties">
                <props>
                        <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
                        <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/sshdemo</prop>
                        <prop key="hibernate.connection.username">root</prop>
                        <prop key="hibernate.connection.password">root</prop>
                        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                        <!--可选-->
                        <prop key="hibernate.show_sql">true</prop>
                        <prop key="hibernate.format_sql">true</prop>
                        <prop key="use_sql_comments">true</prop>
                </props>
        </property>
        <property name="mappingDirectoryLocations" value="classpath:cn/hd/entity"></property>
</bean>

 

7. 整合c3p0连接池

1. 使用spring读取连接池的配置文件

<!--读取配置文件信息-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

 配置连接池的bean

<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
</bean>

 

 hibernate中的数据库配置删掉,增加一个propertiesdataSource

<property name="dataSource" ref="dataSource"></property>

  

8. 整合状态下操作数据库(dao)

书写dao类 并让dao类继承hibernateDaoSupport

提供了一个hibernateTemplate模板

这个模板可以实现 hibernate 的crud criteria  sql 四种查询

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
    @Override
    public void add(User user) {
        HibernateTemplate ht = getHibernateTemplate();
        ht.save(user);
    }  }

 

 

 

9. 整合Spring AOP事务

1. 获得事务管理对象

   DataSourceTransactionManager

  HibernateTransactionManager

<!--配置事务-->
<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"></bean>

2. 配置事务通知

<!--配置事务的通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
                <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
                <tx:method name="persist*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
                <tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
                <tx:method name="remove*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
                <tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
                <tx:method name="modify*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
                <tx:method name="get*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
                <tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
        </tx:attributes>
</tx:advice>
<!--将食物植入到目标对象-->
<aop:config>
        <aop:pointcut id="txPc" expression="execution(* cn.hd.service.impl.*ServiceImpl.*(..))"></aop:pointcut>
        <aop:advisor pointcut-ref="txPc" advice-ref="txAdvice"></aop:advisor>
</aop:config>

 

 

10. 介绍模板

使用hibernateTemplate ,execute 就拿到了session

回到了原始的hibernate开发

@Override
public List<User> getAll() {
     return this.getHibernateTemplate().execute(new HibernateCallback<List<User>>() {
          @Nullable
          @Override
          public List<User> doInHibernate(Session session) throws HibernateException {
              String hql = "from User ";
              Query query = session.createQuery(hql);
              List list = query.list();
              return list;
          }
      });
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值