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)java

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



3.  配置Action (struts.xml)

mysql

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



4. CustomerAction.java 文件中 git

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)github

<!--  配置 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)web

    <!--  配置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 管理spring

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

</bean>

 

 


2. 業務層注入DAO (CustomerServiceImpl.java)

sql

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 的配置信息apache

<!--  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

session

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>
相關文章
相關標籤/搜索