SSH整合方式一:无障碍整合[配置文件]

SSH框架回顾:

在这里插入图片描述
SSH整合:
1.第一步:创建web项目,引入jar包
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的开发包。

Hibernate的jar包:
Hibernate的开发的必须的包

hibernate-release-5.0.7.Final\lib\required\*.jar

 MySQL驱动
mysql-connector-java-5.1.7-bin.jar

日志记录
在这里插入图片描述
使用C3P0连接池:
在这里插入图片描述
注意:Struts2和Hibernate都引入了一个相同的jar包(javassist包),删除一个

Spring的jar包

  • IOC的开发
    在这里插入图片描述
  • AOP的开发
    在这里插入图片描述
  • JDBC模板的开发
    在这里插入图片描述
  • 事务管理
    在这里插入图片描述
  • 整合web项目的开发
    在这里插入图片描述
  • 整合单元测试的开发
    在这里插入图片描述
  • 整合hibernate的开发
    在这里插入图片描述
    2.第二步:引入配置文件
  • Struts的配置文件
    web.xml配置核心过滤器
<!--   核心过滤器 -->
 <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>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- struts2的约束 -->
  <!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
	<struts>
		<!-- 这是将Struts2和Spring的整合() ,记得引包struts-spring-plugin.jar-->
		<!-- <constant name="struts.objectFactory" value="spring"/> -->
		
		<!-- 配置Action -->
		<package name="ssh1" extends="struts-default"  namespace="/">
			<!-- <action name="customer_*" class="com.wangshi.ssh.web.action.CustomerAction" method="{1}"> -->
			
			<!-- Spring整合struts2的方式二 ,customerAction是spring配置action的id-->
			<action name="customer_*" class="customerAction" method="{1}">
			</action>
		</package>
	</struts>

  • Hibernate的配置文件
    hibernate.cfg.xml,删除那个与线程绑定的session。
<?xml version="1.0" encoding="UTF-8"?>
<!-- hibernate的约束 -->
<!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:///ssh1</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</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.connection.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/wangshi/ssh/entity/Customer.hbm.xml"/>
	
	</session-factory>
</hibernate-configuration>

映射文件Customer.hbm.xml

<?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.wangshi.ssh.entity.Customer" table="cst_customer">
		<!-- 建立类中的属性与表中的主键对应 -->
		<id name="cust_id" column="cust_id" >
			<!-- 主键生成策略 -->
			<generator class="native"/>
		</id>
		
		<!-- 建立类中的普通的属性和表的字段的对应 -->
		<property name="cust_name" column="cust_name"  />
		<property name="cust_source" column="cust_source" />
		<property name="cust_industry" column="cust_industry"/>
		<property name="cust_level" column="cust_level"/>
		<property name="cust_phone" column="cust_phone"/>
		<property name="cust_mobile" column="cust_mobile"/>
	</class>
</hibernate-mapping>
  • Spring的配置文件,spring的核心监听器
    web.xml
<!--  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>

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>
	
	<!-- Spring整合struts2的方式二,配置action 为多例 -->
	<bean id="customerAction" class="com.wangshi.ssh.web.action.CustomerAction" scope="prototype">
		<!-- 手动注入service -->
		<property name="customerService" ref="customerService"/>
	</bean>
	
	<!-- 将Service交给spring管理 -->
	<!-- 配置service=========== -->
	<bean id="customerService" class="com.wangshi.ssh.service.Impl.CustomerServiceImpl">
		<property name="customerDao" ref="customerDao" />
	</bean>
	
	<!-- 将dao层交给spring -->
	<!-- 配置dao========== -->
	<bean id="customerDao" class="com.wangshi.ssh.dao.Impl.CustomerDaoImpl">
		<!-- 注册SessionFactory就会创建hibernate的模板 -->
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	
	<!-- 配置spring的事务管理 -->
	<!-- 配置事物管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	
	<!-- 开启事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
</beans>

日志记录:log4j.properties

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###
# error warn info debug trace
log4j.rootLogger= info, stdout

3.第三步:创建包结构
4.第四步:创建相关类
在这里插入图片描述
5.第五步:引入相关的页面
6.第六步:修改add.jsp
在这里插入图片描述
第七步:Spring整合Struts2[两种方式]
Spring整合Struts2方式一:Action由Struts2自身创建的
编写Action

package com.wangshi.ssh.web.action;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.wangshi.ssh.entity.Customer;
import com.wangshi.ssh.service.CustomerService;

/**客户管理的Action的类
 * @author wanghaichuan
 */
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {

	//模型驱动使用的对象
	private Customer customer = new Customer();
	
	@Override
	public Customer getModel() {
		return customer;
	}
	
	//注入CustomerService;
	private CustomerService customerService;
	
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}


	/**
	 * 保存客户的方法:save
	 */
	public String save(){
		//传统方式[如果web层没有使用struts2,获取业务层的类就必须如下进行编写:]
		/*WebApplicationContext applicationContext = WebApplicationContextUtils
				.getWebApplicationContext(ServletActionContext.getServletContext());
		CustomerService customerService = (CustomerService) applicationContext.getBean("customerService");*/
		
		System.out.println("Action中的save方法执行了.....");
		customerService.save(customer);
		return NONE;
	}
}

配置Action[在struts.xml中配置]

<?xml version="1.0" encoding="UTF-8"?>
<!-- struts2的约束 -->
  <!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
	<struts>
		<!-- 这是将Struts2和Spring的整合() ,记得引包struts-spring-plugin.jar-->
		<!-- <constant name="struts.objectFactory" value="spring"/> -->
		
		<!-- 配置Action -->
		<package name="ssh1" extends="struts-default"  namespace="/">
			<!-- <action name="customer_*" class="com.wangshi.ssh.web.action.CustomerAction" method="{1}"> -->
			
			<!-- Spring整合struts2的方式二 ,customerAction是spring配置action的id-->
			<action name="customer_*" class="customerAction" method="{1}">
			</action>
		</package>
	</struts>

在Action中引入Service,详情请看上面代码
传统方式
在这里插入图片描述
进行Spring和Struts2的整合:
引入struts-spring-plugin.jar
在插件包中有如下配置
在这里插入图片描述
*****开启一个常量:在Struts2中只有开启这个常量就会引发下面常量生效:
在这里插入图片描述
让Action按照名称自动注入Service
将Service交给Spring管理

	<!-- 将Service交给spring管理 -->
	<!-- 配置service=========== -->
	<bean id="customerService" class="com.wangshi.ssh.service.Impl.CustomerServiceImpl">
		<property name="customerDao" ref="customerDao" />
	</bean>

在Action注入Service
在这里插入图片描述
第八步:Spring整合Struts2方式二
Spring整合Struts2方式二:Action交给Spring管理(推荐)
引入struts-spring-plugin.jar
将Action交给Spring:

<!-- Spring整合struts2的方式二,配置action 为多例 -->
	<bean id="customerAction" class="com.wangshi.ssh.web.action.CustomerAction" scope="prototype">
		<!-- 手动注入service -->
		<property name="customerService" ref="customerService"/>
	</bean>

在struts.xml中配置Action:
在这里插入图片描述
注意:

需要配置Action为多例的;需要手动注入Service

在这里插入图片描述
9.第九步:Service调用DAO
将DAO交给Spring管理
在这里插入图片描述
在Service注入DAO
在这里插入图片描述 将Service交给spring管理

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

10.第十步:Spring整合Hibernate框架
创建数据库和表

package com.wangshi.ssh.entity;
/**
 * 客户管理的实体
 * @author wanghaihcaun
 *Create database ssh1;
Use ssh1;
CREATE TABLE `cst_customer` (
  `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
  `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
  `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
  `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
  `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
  `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
  `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
  PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

 */
public class Customer {
	private Long cust_id;
	private String cust_name;
	private String cust_source;
	private String cust_industry;
	private String cust_level;
	private String cust_phone;
	private String cust_mobile;
	public Long getCust_id() {
		return cust_id;
	}
	public void setCust_id(Long cust_id) {
		this.cust_id = cust_id;
	}
	public String getCust_name() {
		return cust_name;
	}
	public void setCust_name(String cust_name) {
		this.cust_name = cust_name;
	}
	public String getCust_source() {
		return cust_source;
	}
	public void setCust_source(String cust_source) {
		this.cust_source = cust_source;
	}
	public String getCust_industry() {
		return cust_industry;
	}
	public void setCust_industry(String cust_industry) {
		this.cust_industry = cust_industry;
	}
	public String getCust_level() {
		return cust_level;
	}
	public void setCust_level(String cust_level) {
		this.cust_level = cust_level;
	}
	public String getCust_phone() {
		return cust_phone;
	}
	public void setCust_phone(String cust_phone) {
		this.cust_phone = cust_phone;
	}
	public String getCust_mobile() {
		return cust_mobile;
	}
	public void setCust_mobile(String cust_mobile) {
		this.cust_mobile = cust_mobile;
	}
}

编写实体和映射
Spring和Hibernate整合
在Spring的配置文件中,引入Hibernate的配置的信息
在这里插入图片描述
在Spring和Hibernate整合后,Spring提供了一个Hibernate的模板类简化Hibernate开发。
改写DAO继承HibernateDaoSupport
在这里插入图片描述
配置的时候在DAO中直接注入SessionFactory
在这里插入图片描述
在DAO中使用Hibernate的模板完成保存操作

package com.wangshi.ssh.dao.Impl;

import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.wangshi.ssh.dao.CustomerDao;
import com.wangshi.ssh.entity.Customer;
/**
 * @author wanghaichuan
 *这是客户管理dao层的实现
 */
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

	@Override
	public void save(Customer customer) {
		
		System.out.println("这是dao层中的save方法.......");
		//使用模板完成操作
		this.getHibernateTemplate().save(customer);
	}

}

11.第十一步:配置Spring的事务管理
配置事务管理器
在这里插入图片描述
开启注解事务
在这里插入图片描述
在业务层使用注解
在这里插入图片描述
---------------------------------------最后启动服务器,看是否成功添加,页面不跳转!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值