ssh整合入门

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39134861/article/details/82145696

1,hibernate框架
-单独使用hibernate的时候,hibernate.cfg.xml 文件位置固定在src下面。
但是如果和spring进行整合的时候,没有固定位置。
~hibernate映射配置文件(实体类和数据库表映射关系–使用orm思想)
~spring框架对hibernate框架进行封装,使用hibernateTemplate
hibernateTemplate常用的api:
save() update() delete() get(按ID查询) load() find(查询所有)
【spring对各种支持的持久化技术,提供了简单操作的模板和回调。

orm 持久化技术有JDBC(JdbcTemplate),Hibernate(HibernateTamplate),IBatis(MyBatis)-(SqlMapClientRTemplate),JPA(JpaTemplate)

2,Struts2框架
action操作,值栈,拦截器
action操作:
- 创建acton的3中方式:
普通类,写一个类实现接口,
继承ActionSupport(这个重点)
- 配置action访问路径
创建Struts.xml配置文件,这个文件的位置固定在src下面。
- 配置访问action的多个方法
使用通配符的方式
- 在action中获取标签数据
a.获取request对象(使用ServletActionContext类获取)
b.属性的封装
c.模型驱动(继承ModelDrive类重写getModel方法)

值栈
(开发中用的多,但是也有用到)
- 向值栈中放数据,set方法;push方法;定义变量,生成get方法
- 从值栈中获取数据,Struts2和ognl标签。
拦截器
a.aop和责任链模式
b.自定义拦截器
~继承MethodFilterInterceptor
~
重写类里面的方法
~~~配置拦截和action关联
Struts过滤器的名字:StrutsPrepareAndExecuteFilter在web.xml中
3.spring框架
- spring核心配置文件
位置没有要求,在spring核心配置文件中引入schema约束
- 创建对象
xml配置方式:
注解方式:4个注解@service@Component@controller@Repository 功能一样,注解名的不同是为了能够让标记类本身的用途更加清晰

- 注入属性
-xml配置方式
- 注解方式:2个注解@autowired@resource
- 使用servletContext对象和监听器实现
-服务器启动的时候,加载spring配置文件,创建对象
-配置spring的监听器
jdbcTemplate
spring事物配置
~xml方式~注解方式
4,框架之间的整合:
两两整合:
把struts2的action对象创建,交给spring进行管理。

 <bean id="" class="" scope="prototype">

1,
struts版本的问题:
https://blog.csdn.net/kevingao/article/details/49020217

2,ssh 的环境搭建
第一步:整合Struts框架和 spring框架的整合
Struts2.xml配置文件如下:(src下面)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts>
    <package name="demo1"  extends="struts-default" namespace="/">
    <!-- 
     不写method="cn.itcast.action.UserAction"
     写class="userAction",需要引入struts2-json-plugin-2.3.8.jar包class属性不写全路径,因为action对象会创建2次。
     spring配置文件中找bean标签中对应的id属性值
     -->
        <action name="userAction" class="userAction"></action>
    </package>
</struts>

web.xml在WEB-INF下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

     <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- classpath是类路径src下面,还有一个是包路径 -->
         <param-value>classpath:bean.xml</param-value>
     </context-param>
    <!-- 过滤器 -->
    <filter>
        <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
      <listener>
        <listener-class>
          org.springframework.web.context.ContextLoaderListener
         </listener-class>
      </listener>
</web-app>

bean.xml在src下面,是spring的核心配置文件

<?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:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
 default-autowire="byName" default-lazy-init="true">

<!-- spring 的配置文件 -->
 <!-- 配置数据源 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <property name="url" value="jdbc:mysql://localhost/sshdemo?characterEncoding=utf-8"></property>
  <property name="username" value="root"></property>
  <property name="password" value="root"></property>
 </bean>
 <!-- 配置action对象 -->
 <bean id="userAction" class="cn.itcast.action.UserAction" scope="prototype"></bean>
 </beans>

action的创建:

package cn.itcast.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{

    public String execute() throws Exception {
        System.out.println("action...");
        return NONE;
    }
}

第二步:spring框架和hibernate框架的整合

  • 把hibernate核心配置文件里面的数据库配置,写在spring的配置文件中。
  • 第一次访问的速度慢,原因:第一次访问的时候创建sessionFactory对象,把sessionfactory对象创建交给sping管理
  • 在服务器启动的时候,创建sessionFactory对象。

导入hibernate.cfg.xml配置文件 :

<?xml version="1.0" encoding="UTF-8"?>
<!-- 在类的根路径下创建名称为hibernate.cfg.xml的配置文件
导入约束:dtd约束
 -->
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <!-- 配置SessionFactory-->
    <session-factory>
        <!-- 第一部分:连接数据库的信息(写在spring的配置文件中。) -->
    <!--    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/day55_ee287_hibernate01</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>数据库的方言  
         -->
        <!-- 第二部分:hibernate的可选配置 -->
        <!-- 是否显示hibernate生成的SQL语句 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 是否使用格式化输出sql语句到控制台 -->
        <property name="hibernate.format_sql">false</property>
        <!-- 配置hibernate采用何种方式生成DDL语句 -->
        <property name="hibernate.hbm2ddl.auto">update</property><!-- update表示检测实体类的映射配置和数据库的表结构是否一致,如果不一致,更新表结构 -->
        <!-- 设置hibernate的连接池提供商 -->
        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <!-- 把session和线程绑定,从而实现一个线程只有一个Session -->
        <property name="hibernate.current_session_context_class">thread</property>

        <!-- 第三部分:映射配置文件的位置 -->
        <mapping resource="com/"/>
    </session-factory>
</hibernate-configuration>

在bean.xml中加入:

<!-- sessionFactory创建交给spring进行管理 -->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
 <!-- 在hibernate中没有数据库配置, 数据库在spring中进行配置,注入DataSource-->
 <property name="dataSource" ref="dataSource"></property>
 <!-- 指定hibernate核心配置文件 -->
 <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
 </bean>

 <!-- 配置action对象 -->
 <bean id="userAction" class="cn.itcast.action.UserAction" scope="prototype">
 <!-- 注入service -->
    <property name="userService" ref="userService" ></property> 
 </bean>

 <!-- 配置serivce对象 -->
 <bean id="userService" class="cn.itcast.service.UserService" scope="prototype">
    <!-- 注入dao  接口=实现类对象-->
    <property name="userDao" ref="userDao"></property>
</bean>

  <!-- 配置userDaoImpl对象 -->
 <bean name="userDaoImpl" class="cn.itcast.dao.UserDaoImpl">
    <property name="hibernateTemplate" ref="hibernateTemplate"></property>
 </bean>

<!-- 注入hibernate模板(之前使用jdbc模板,里面注入的属性是dataSource) -->
 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
     <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>


这是一个web项目,在项目启动的时候,就来加载spring的配置文件。
column可以省略,如果省略的话,和前面的name值保持一致。

分模块开发中:

<!--分模块开发,引入其他配置文件-->
 <import resource="classpath:user.xml"/>

1,在配置的过程中,需要注意:在事物管理器的配置里面,记得引入sessionFactory。
2,如果要合并,hibernate.cfg.xml去掉,在bean.xml中进行配置的话,记得写在
标签里面。

整合之后:
bean.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:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
 default-autowire="byName" default-lazy-init="true">

<!-- spring 的配置文件 -->
    <!-- 配置数据源信息 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql:///sshdemo" />
        <property name="user" value="root" />
        <property name="password" value="root" />
    </bean>



 <!-- sessionFactory创建交给spring进行管理 -->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <!-- 在hibernate中没有数据库配置, 数据库在spring中进行配置,注入DataSource-->
     <property name="dataSource" ref="dataSource"></property>
     <!-- 指定hibernate核心配置文件 -->
     <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>

        <!-- 配置hibernate的信息 -->
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        </props>
    </property>

    <!-- 引入映射文件【可以写多个value】 -->
    <property name="mappingResources">
        <list>
            <value>cn/itcast/entity/User.hbm.xml</value>
        </list>
    </property>
 </bean>

 <!-- 第一步:配置事物管理器 -->
 <bean id="transactionManager"  class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <!-- 第二部:开启事物注解 [在service上面加上注解]-->
 <tx:annotation-driven transaction-manager="transactionManager"/>

<!-- 分模块开发,引入其他配置文件 -->
<import resource="classpath:user.xml"/>

 </beans>

hibernate.cfg.xml换成user.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:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
 default-autowire="byName" default-lazy-init="true">
<!-- 配置action对象,注入service -->
 <bean id="userAction" class="cn.itcast.action.UserAction" scope="prototype">
    <property name="userService" ref="userService"></property>
 </bean>

 <!-- 配置serivce对象 -->
 <bean id="userService" class="cn.itcast.service.UserService" scope="prototype">
     <!-- 注入dao  接口=实现类对象-->
    <property name="userDao" ref="userDaoImpl"></property>
 </bean>

  <!-- 配置userDaoImpl实现类对象 -->
 <bean id="userDaoImpl" class="cn.itcast.dao.UserDaoImpl">
    <property name="hibernateTemplate" ref="hibernateTemplate"></property>
 </bean>

 <!-- 创建对象hibernateTemplate【注入sessionFactory】[创建一个模板,在实现类,先声明一个属性,再进行配置] -->
 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 </beans>

猜你喜欢

转载自blog.csdn.net/weixin_39134861/article/details/82145696