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的配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

	<!-- 配置Struts2的常量 -->
	<constant name="struts.action.extension" value="action"/>
	
	<!-- 配置Action -->
	<package name="ssh01" extends="struts-default" namespace="/">
		<action name="customer_*" class="customerAction" method="{1}">

		</action>
	</package>
	
</struts>

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<!-- 连接数据库的基本参数 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///study_test</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">666666</property>
		<!-- 配置Hibernate的方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<!-- 可选配置================ -->
		<!-- 打印SQL -->
		<property name="hibernate.show_sql">true</property>
		<!-- 格式化SQL -->
		<property name="hibernate.format_sql">true</property>
		<!-- 自动创建表 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<!-- 配置C3P0连接池 -->
		<property name="connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
		<!--在连接池中可用的数据库连接的最少数目 -->
		<property name="c3p0.min_size">5</property>
		<!--在连接池中所有数据库连接的最大数目  -->
		<property name="c3p0.max_size">20</property>
		<!--设定数据库连接的过期时间,以秒为单位,
		如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
		<property name="c3p0.timeout">120</property>
		 <!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->
		<property name="c3p0.idle_test_period">3000</property>
		
		<!-- 引入映射 -->
		<mapping resource="com/pipi/ssh/domain/Customer.hbm.xml" />

	</session-factory>
</hibernate-configuration>

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

(3)Spring的配置文件

日志记录
log4j.properties

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>

    <!-- 配置Struts2的核心过滤器 -->
    <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>

applicationContext.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: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">

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

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

	<!-- 配置Service -->
	<bean id="customerService" class="com.pipi.ssh.service.CustomerServiceImpl">
		<property name="customerDao" ref="customerDao"/>
	</bean>

	<!-- 配置DAO -->
	<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()
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <!-- 建立类与表的映射 -->
    <class name="com.pipi.ssh.domain.Customer" table="t_user">

        <!-- 建立类中的OID与表中的主键对应 -->
        <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失败,至今未解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值