spring框架,SSH整合配置xml写法

 

 

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

  1. Struts2的jar包
    1. struts-2.3.24\apps\struts2-blank\WEB-INF\lib\*.jar
    2. Struts2中有一些包需要了解的:
      1. struts2-convention-plugin-2.3.24.jar ----Struts2的注解开发包。
      2. struts2-json-plugin-2.3.24.jar ----Struts2的整合AJAX的开发包。
      3. struts2-spring-plugin-2.3.24.jar ----Struts2的整合Spring的开发包。
  2. Hibernate的jar包
    1. Hibernate的开发的必须的包
      1. hibernate-release-5.0.7.Final\lib\required\*.jar
    2. MySQL驱动
    3. 日志记录

          使用C3P0连接池:

*****注意:Struts2和Hibernate都引入了一个相同的jar包(javassist包)。删除一个******

Spring的jar包

IOC的开发

AOP的开发

JDBC模板的开发

事务管理

整合web项目的开发

整合单元测试的开发

整合hibernate的开发

第二步:引入配置文件

  1. Struts的配置文件
    1. web.xml

                 struts.xml

2.Hibernate的配置文件

 hibernate.cfg.xml   

删除那个与线程绑定的session。

映射文件

3.Spring的配置文件

web.xml配置

applicationContext.xml

4.日志记录

log4j.properties

项目结构(我把 hibernate.cfg.xml 全部整合在applicationContext.xml里面了

 

web.xml配置详情

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Struts2_01</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
    <!--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>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>

struts.xml的配置详情

<?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"/>
<package name="ssh1" extends="struts-default" namespace="/">
	<!-- 这里的class的值对应spring里面配置的Acton的id -->
	<action name="customer_*" class="customerAction" method="{1}">
		
	</action>
</package>


</struts>

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

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 -->
	<!-- 配置c3p0 -->
	<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
    	<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/SSH?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC&amp;rewriteBatchedStatements=true"></property>
    	<property name="user" value="root"></property>
    	<property name="password" value="123"></property>
	</bean>
	<!-- 引入Hibernate的配置的信息=============== -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 配置sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
			 <property name="dataSource" ref="c3p0"></property>
			 <property name="hibernateProperties">
			  	<props>
			  		<prop key="hibernate.show_sql">true</prop>
			  		<prop key="hibernate.format_sql">true</prop>
			  		<prop key="hibernate.hbm2ddl.auto">update</prop>
			  		<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
			  	</props>
		  	</property>
		  	<property name="mappingResources">
		   		<list>
		   			<value>com/it/bean/Customer.hbm.xml</value>
		   		</list>
		   </property>
	</bean>
	
	
	<!-- 配置Action===================需要配置Action为多例的  -->
	<bean id="customerAction" class="com.it.action.CustomerAction" scope="prototype">
		<!--需要手动注入Service -->
		<property name="customerService" ref="customerService"/>
	</bean>
	
	<!-- 配置Service================== -->
	<bean id="customerService" class="com.it.serviceimpl.CustomerServiceImpl">
		<property name="customerDao" ref="customerDao"/>
	</bean>
	
	<!-- 配置DAO====================== -->
	<bean id="customerDao" class="com.it.daoimpl.CustomerDaoImpl">
		 <property name="hibernateTemplate" ref="hibernateTemplate"/> 
	</bean>
	
	<!-- 配置AOP事务管理 -->
	<!-- 切面对象 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<!-- 
				切面对象从配置的sessionFactory中获取session
				将获取的该session开启事务
				并且将开启了事务的该session绑定到了当前线程中
		 -->
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 配置DataSourceTransactionManager里面事务方法的一些参数
		 不写 该方法使用的事务参数都是默认值
	 -->
	 <tx:advice transaction-manager="transactionManager" id="txAdvice">
			<tx:attributes>
					<tx:method name="*" />
			</tx:attributes>
	</tx:advice>
	<!-- 织入 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.it.serviceimpl.CustomerServiceImpl.save(..))" id="txPointcut"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
	</aop:config> 
</beans>

Javabean

package com.it.bean;

public class Customer {
	private int 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 int getCust_id() {
		return cust_id;
	}

	public void setCust_id(int 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;
	}

	@Override
	public String toString() {
		return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source
				+ ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_phone=" + cust_phone
				+ ", cust_mobile=" + cust_mobile + "]";
	}
	
}

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.it.bean.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>

​​​​​​​

action类

package com.it.action;

import org.junit.Test;

import com.it.bean.Customer;
import com.it.service.CustomerService;
import com.it.serviceimpl.CustomerServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
	// 模型封装
	private Customer custmoer = new Customer();

	@Override
	public Customer getModel() {
		// TODO Auto-generated method stub
		return custmoer;
	}

	// 注入CustomerService:
	private CustomerService customerService;

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

	// 保存客户的方法
	public String save() {
		System.out.println("action执行了");
		System.out.println(custmoer);
		customerService.save(custmoer);
		return null;
	}

}

CustomerService接口

package com.it.service;

import com.it.bean.Customer;

public interface CustomerService {

	void save(Customer custmoer);

}

CustomerService接口的实现类CustomerServiceImpl

package com.it.serviceimpl;

import org.springframework.transaction.annotation.Transactional;

import com.it.bean.Customer;
import com.it.dao.CustomerDao;
import com.it.daoimpl.CustomerDaoImpl;
import com.it.service.CustomerService;

public class CustomerServiceImpl implements CustomerService{
	private CustomerDao customerDao;

	public void setCustomerDao(CustomerDaoImpl customerDao) {
		this.customerDao = customerDao;
	}

	
	@Override
	public void save(Customer custmoer) {
		// TODO Auto-generated method stub
		System.out.println("service save");
		customerDao.save(custmoer);
	}
	
}

CustomerDao接口

package com.it.dao;

import com.it.bean.Customer;

public interface CustomerDao {

	void save(Customer custmoer);

}

CustomerDao接口的实现类

package com.it.daoimpl;

import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.it.bean.Customer;
import com.it.dao.CustomerDao;

public class CustomerDaoImpl  implements CustomerDao {
	private HibernateTemplate hibernateTemplate;
	
	

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}



	@Override
	public void save(Customer custmoer) {
		// TODO Auto-generated method stub
		System.out.println("dao save");
		this.hibernateTemplate.save(custmoer);
	}

}

第一次整合的时候肯定各种错误,一定要细心编写代码,导入jar包的时候一定要一步步的来,以后熟练了就可以把导好的jar包放在一起,以后直接用就行了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Exception.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值