SSH整合方式一:无障碍整合

SSH整合方式一:无障碍整合

1、第一步:创建web项目,引入jar包

(1)Struts2的jar包:
struts-2.3.24\apps\struts2-blank\WEB-INF\lib*.jar
Struts2中有一些包需要了解的:
struts2-convention-plugin-2.3.24.jar ----Struts2的注解开发包。
struts2-json-plugin-2.3.24.jar ----Struts2的整合AJAX的开发包。
struts2-spring-plugin-2.3.24.jar ----Struts2的整合Spring的开发包。

(2)Hibernate的jar包
Hibernate的开发的必须的包
hibernate-release-5.0.7.Final\lib\required*.jar
MySQL驱动
日志记录
使用C3P0连接池
注意:Struts2和Hibernate都引入了一个相同的jar包(javassist包)。删除一个(必须删除一个,删版本低的)

(3)Spring的jar包:
IOC的开发
AOP的开发
JDBC模板的开发
事务管理
整合web项目的开发
整合单元测试的开发
整合hibernate的开发


2、第二步:引入配置文件

(1)Struts的配置文件



<struts>

	
	<constant name="struts.action.extension" value="action"/>
	
	
	<package name="ssh01" extends="struts-default" namespace="/">
		<action name="customer_*" class="customerAction" method="{1}">

		action>
	package>
	
struts>

(2)Hibernate的配置文件
hibernate.cfg.xml



<hibernate-configuration>
	<session-factory>
		
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
		<property name="hibernate.connection.url">jdbc:mysql:///study_testproperty>
		<property name="hibernate.connection.username">rootproperty>
		<property name="hibernate.connection.password">666666property>
		
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
		
		
		
		<property name="hibernate.show_sql">trueproperty>
		
		<property name="hibernate.format_sql">trueproperty>
		
		<property name="hibernate.hbm2ddl.auto">updateproperty>
		
		
		<property name="connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProviderproperty>
		
		<property name="c3p0.min_size">5property>
		
		<property name="c3p0.max_size">20property>
		
		<property name="c3p0.timeout">120property>
		 
		<property name="c3p0.idle_test_period">3000property>
		
		
		<mapping resource="com/pipi/ssh/domain/Customer.hbm.xml" />

	session-factory>
hibernate-configuration>

删除那个与线程绑定的session。Spring有自己的管理方式。
映射文件。

(3)Spring的配置文件

日志记录
log4j.properties

web.xml中配置:


<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">
    
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    context-param>

    
    <filter>
        <filter-name>struts2filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>struts2filter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

web-app>

applicationContext.xml配置:


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

	
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		
		<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
	bean>

	
	<bean id="customerAction" class="com.pipi.ssh.web.action.CustomerAction" scope="prototype">
		<property name="customerService" ref="customerService"/>
	bean>

	
	<bean id="customerService" class="com.pipi.ssh.service.CustomerServiceImpl">
		<property name="customerDao" ref="customerDao"/>
	bean>

	
	<bean id="customerDao" class="com.pipi.ssh.dao.CustomerDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"/>
	bean>


	
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	bean>

	
	<tx:annotation-driven transaction-manager="transactionManager"/>

beans>

3、实体类,映射文件

package com.pipi.ssh.domain;

public class Customer {
     

    private Long id;
    private String username;
    private String password;
    // setter and getter,构造方法,重写toString()
}


<hibernate-mapping>
    
    <class name="com.pipi.ssh.domain.Customer" table="t_user">

        
        <id name="id" column="id">
            
            <generator class="native"/>
        id>

        
        <property name="username" column="username"/>
        <property name="password" column="password"/>

    class>
hibernate-mapping>

4、web层:Action类

package com.pipi.ssh.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.pipi.ssh.domain.Customer;
import com.pipi.ssh.service.CustomerService;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

// 顾客Customer管理的Action类
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
     

    // 模型驱动注册的类
    private Customer customer = new Customer();

    // 属性注入
    private CustomerService customerService;

    public void setCustomerService(CustomerService customerService) {
     
        this.customerService = customerService;
    }

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

    public String save() {
     
        System.out.println("save方法执行了!");

        /*// 如果web层没有Struts2,就只能这样获取Service
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
        CustomerService customerService = (CustomerService) applicationContext.getBean("customerService");
        customerService.save(customer);*/

        customerService.save(customer);

        return NONE;
    }

}

5、Service层:接口和实现类

package com.pipi.ssh.service;

import com.pipi.ssh.domain.Customer;

// 顾客管理的业务层的接口
public interface CustomerService {
     

    void save(Customer customer);
}
package com.pipi.ssh.service;

import com.pipi.ssh.dao.CustomerDao;
import com.pipi.ssh.domain.Customer;

// 顾客管理的业务层实现类
@Transactional  // 使用注解事务
public class CustomerServiceImpl implements CustomerService {
     

    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);
    }
}

6、Dao层:接口和实现类

package com.pipi.ssh.dao;

import com.pipi.ssh.domain.Customer;

// 顾客管理的Dao层接口
public interface CustomerDao {
     

    void save(Customer customer);
}
package com.pipi.ssh.dao;

import com.pipi.ssh.domain.Customer;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

// 顾客管理的Dao层实现类
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
     

    @Override
    public void save(Customer customer) {
     
        System.out.println("Dao中的save方法执行!" + customer);
        this.getHibernateTemplate().save(customer);
    }
}

最后说一下我遇到的问题,运行Tomcat时,applicationContext.xml中的,创建beanFactory失败,至今未解决。

你可能感兴趣的:(Spring,spring,ssh整合)