Java - 框架之 SSH 整合

                    代码获取

十四. ssh 整合1 - 包

1. Struts jar 包
    - Struts-2.xx\apps\stutrs2-blank\WEB-INF\lib
2. Hibernate jar 包
    - hibernate-release-5.0.7.Final\lib\required
    - mysql 驱动包
    - 日志记录
3. Spring jar 包



十五. ssh 整合 - 配置文件

1. Struts 配置文件
    - web.xml
    - struts.xml

2. Hibernate 配置文件
    - hibernate.cfg.xml
    - 映射文件

3. Spring 配置文件
    - web.xml
    - applicationContext.xml
    - 日志记录





十六. ssh 整合2 - Spring 和 Struts2 的整合方式一: Action 由 Struts2 创建

1. 导入 struts2-spring-plugin-2.3.33.jar 整合包


2. applicationContext.xml 文件中配置 Service (applicationContext.xml)

<!--  配置 Serveice  -->
<bean id="customerService" class="com.q.ssh.service.impl.CustomerServiceImpl"></bean>



3.  配置Action (struts.xml)

<!--  配置Action  -->
<package name="ssh1" namespace="/" extends="struts-default">
    <action name="customer_*" class="com.q.ssh.web.action.CustomerAction" method="{1}" />
</package>



4. CustomerAction.java 文件中

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.q.ssh.domain.Customer;
import com.q.ssh.service.CustomerService;

public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
    // 模型驱动使用对象
    private Customer customer;

    @Override
    public Customer getModel() {
        return customer;
    }

    // 注入 CustomerService
    private CustomerService customerService;
    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    // 保存客户 :save
    public String save(){
        System.out.println("save>>>");
        customerService.save(customer);  // 这里就可以直接调用 save 方法。
        return NONE;
    }

}

 

 




十七. ssh 整合2 - Spring 和 Struts2 的整合方式二: Action 由 Spring 创建

注意:
    - 需要配置 Action 为多例。
    - 需要手动注入 Service。

1. 导入 struts2-spring-plugin-2.3.33.jar 整合包


2. 将 Action 交给 Spring (applicationContext.xml)

<!--  配置 Serveice (注:需要将 Action 设置为多例: scope="prototype" ) -->
<bean id="customerService" class="com.q.ssh.service.impl.CustomerServiceImpl" scope="prototype"></bean>

<!--  配置 Action  (将 Action 交给 Spring 管理)-->
<bean id="customerAction" class="com.q.ssh.web.action.CustomerAction">
        <!--  需要手动注入 Service  -->
        <property name="customerService" ref="customerService" />
</bean>



3. 配置 Action (Struts.xml)

    <!--  配置Action  -->
    <package name="ssh1" namespace="/" extends="struts-default">
        <!--  这里的 class属性要和 上面的 id 相同  -->
        <action name="customer_*" class="customerAction" method="{1}" />
    </package>

 

 




十八. ssh 整合3 - Service 调用 DAO

1. 将DAO交给 Spring 管理

<!--  配置DAO  -->
<bean id="customerDao" class="com.q.ssh.dao.impl.CustomerDaoImpl">

</bean>

 

 


2. 业务层注入DAO (CustomerServiceImpl.java)

import com.q.ssh.dao.CustomerDao;
import com.q.ssh.domain.Customer;
import com.q.ssh.service.CustomerService;

public class CustomerServiceImpl implements CustomerService {

    // 注入 DAO
    private CustomerDao customerDao;
    public void setCustomerDao(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }

    @Override
    public void save(Customer customer) {
        System.out.println("Service 中的 save...");
        customerDao.save(customer);
    }
}

 

 



3. 在Service配置中注入 (applicationContext.xml)

<!--  配置 Serveice  -->
<bean id="customerService" class="com.q.ssh.service.impl.CustomerServiceImpl" scope="prototype">
    // 将customerDao注入 :
    <property name="customerDao" ref="customerDao" />
</bean>

 

 




十九. ssh 整合3 - Spring 整合 Hibernate 框架

1. 创建数据库

2. 在 Spring 的配置文件中,引入 Hibernate 的配置信息

<!--  Spring整合Hibernate  -->
<!--  引入 Hibernate 的配置信息  -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!--  引入Hibernate的配置文件  -->
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

 

 



3. 修改 CustomerDaoImpl.java 的类 继承 HibernateDaoSupport

import com.q.ssh.dao.CustomerDao;
import com.q.ssh.domain.Customer;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

    @Override
    public void save(Customer customer) {
        System.out.println("DAO 中的 save...");
    }
}

 



4. 配置DAO 中导入

<!--  配置DAO  -->
<bean id="customerDao" class="com.q.ssh.dao.impl.CustomerDaoImpl">
        <property name="sessionFactory" ref="sessionFactory" />
</bean>

 




5. 在DAO中 Hibernate 的模板完成保存操作

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

    @Override
    public void save(Customer customer) {
        System.out.println("DAO 中的 save...");
        // 调用 Save 方法。
        this.getHibernateTemplate().save(customer);
    }
}

 



6. 配置 Spring 的事务管理

<!-- 配置事务管理器  -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
</bean>

 




7. 开启注解事务

<!-- 开启注解事务  -->
<tx:annotation-driven transaction-manager="transactionManager" />

 

 



8. 在业务层使用注解

@Transactional
public class CustomerServiceImpl implements CustomerService {

    // 注入 DAO
    private CustomerDao customerDao;
    public void setCustomerDao(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }

    @Override
    public void save(Customer customer) {
        System.out.println("Service 中的 save...");
        customerDao.save(customer);
    }
}

 

 




二十. ssh 整合3 - Spring 整合 Hibernate 框架 (不带 Hibernate 配置文件)

1. 将 Hibernate 文件 使用Spring 整合到 applicationContext 文件中

<!--    引入外部属性文件    -->
<content:property-placeholder location="classpath:jdbc.properties" />

<!--    配置C3P0连接池    -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
</bean>


<!--  Spring整合Hibernate  -->
<!--  引入 Hibernate 的配置信息  -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!--  引入Hibernate的配置文件  -->
        <!--  注入连接池  -->
        <property name="dataSource" ref="dataSource" />
        
        <!--  配置Hibernate相关属性  -->
        <property name="hibernateProperties">
                <props>
                        <!-- 方言 -->
                        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                        <!-- 显示sql -->
                        <prop key="hibernate.show_sql">true</prop>
                        <!-- 是否格式化sql语句 -->
                        <prop key="hibernate.format_sql">true</prop>
                        <!-- update:如果数据库有没表,自动帮你创表 -->
                        <prop key="hibernate.hbm2ddl.auto">update</prop>
                </props>
        </property>
        
        <!-- 设置映射文件 -->
        <property name="mappingResources">
                <list>
                        <value>com/q/ssh/domain/Customer.hbm.xml</value>
                </list>
        </property>
</bean>

 





二十一. ssh 整合3 - Hibernate 模板使用

# 增删改查操作

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
    
    //
    @Override
    public void save(Customer customer) {
        System.out.println("DAO 中的 save...");
        // 将数据保存到数据库
        System.out.println(customer);
        this.getHibernateTemplate().save(customer);
    }


    //
    @Override
    public void update(Customer customer) {
        this.getHibernateTemplate().update(customer);
    }


    //
    @Override
    public void delete(Customer customer) {
        this.getHibernateTemplate().delete(customer);
    }

    // 查 (一个)
    @Override
    public Customer findById(Integer id) {
        return this.getHibernateTemplate().get(Customer.class, id);
    }

    //查 (多个)
    @Override
    public List<Customer> findAllByHQL() {
//        List<Customer> list = (List<Customer>) this.getHibernateTemplate().find("from");
//        return list;
        return null;
    }

    
    //查 (多个)
    @Override
    public List<Customer> findAllByQBC() {
        DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
        List<Customer> list = (List<Customer>) this.getHibernateTemplate().findByCriteria(criteria);
        return list;
    }
} 

 





二十二. ssh 整合 - 延迟加载问题解决

# 由于 session 的开启和关闭都在 Service 层,一旦 Action 层调用已经关闭的 session 就会报错,
# 解决:将 session 的开启和关闭放在 Action 层。


<!-- web.xml  解决延迟加载的过滤器  -->
<filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>*.action</url-pattern>
</filter-mapping>

 




二十三. ssh 整合 - web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">



    <!--  Spring 核心监听器  -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--  加载 Spring 文件路径 - 默认加载的是 WEB-INF/applicationContext.xml  -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>


    <!--  解决延迟加载的过滤器  -->
    <filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>

    <!--  Struts 核心过滤器  -->
    <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>



</web-app>

 

posted @ 2019-04-16 01:35  _Q  阅读(370)  评论(0编辑  收藏  举报