SSH整合:
1、创建一个web工程;
2、引入 jar 包;
3、编写数据库配置文件(jdbc.properties),日志文件(log4j.properties);
jdbc.properties :
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://127.0.0.1:3306/shop?characterEncoding=utf8
jdbc.user = root
jdbc.password = root
log4j.properties :
### direct log messages to stdout ###
log4j.appender.stdout= org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target= System.out
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' ###
log4j.rootLogger= info, stdout
4、web.xml 文件中,配置 struts 的核心过滤器、spring的监听器、spring全局初始化参数;
struts 核心过滤器:
<!-- 配置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 >
spring的核心监听器、spring初始化参数:
<!-- 配置spring核心监听器 -->
<listener >
<listener-class >org.springframework.web.context.ContextLoaderListener </ listener-class>
</listener >
<!-- 配置spring全局初始化参数 -->
<context-param >
<param-name >contextConfigLocation </param-name >
<param-value >classpath:applicationContext.xml </param-value >
</context-param >
5、创建SSH配置文件applicationContext.xml、struts.xml
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" >
</beans>
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>
</struts>
6、applicationContext中 配置基本配置信息
-
- spring中配置 连接池(C3P0连接池);
- spring中配置Hibernate相关信息;
- spring中配置事务管理;
<!-- 配置连接池: -->
<!-- 引入外部属性文件 ( placeholder:占位符) -->
<context:property-placeholder location= "classpath:jdbc.properties" />
<!-- 配置C3P0连接池: -->
<bean id = "dataSource" class= "com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name = "driverClass" value= "${jdbc.driver}"/>
<property name = "jdbcUrl" value= "${jdbc.url}"/>
<property name = "user" value= "${jdbc.user}"/>
<property name = "password" value= "${jdbc.password}"/>、
</bean >
<!-- Hibernate 配置的相关信息 -->
<bean id = "sessionFactory" class= "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<!-- 注入连接池 -->
<property name = "dataSource" ref= "dataSource"/>
<!-- 配置Hibernate 的其他的属性 -->
<property name = "hibernateProperties">
<props >
<prop key= "hibernate.dialect" >org.hibernate.dialect.MySQLDialect </prop >
<prop key = "hibernate.show_sql"> true</ prop>
<prop key = "hibernate.format_sql"> true</ prop>
<prop key= "hibernate.connection.autocommit" >false </prop >
<prop key = "hibernate.hbm2ddl.auto"> update</ prop>
</props >
</property >
<!-- -- ----------------------配置 Hibernate的映射文件------------------------------>
</bean >
<!-- 配置数据库的事务管理: -->
<!-- 事务管理器 -->
<bean id = "transactionManager" class= "org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name = "sessionFactory" ref= "sessionFactory"/>
</bean >
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager = "transactionManager"/>