[关闭]
@Beeder 2018-02-25T11:30:40.000000Z 字数 14249 阅读 469

SSH框架整合

javaWeb Struts2 Spring Hibernate


SSH整合.bmp

环境配置

Struts2相关

引入jar

  1. //struts2整合spring相关jar包
  2. struts2-spring-plugin-2.3.24.jar
  3. asm-3.3.jar
  4. asm-commons-3.3.jar
  5. asm-tree-3.3.jar
  6. commons-fileupload-1.3.1.jar
  7. commons-io-2.2.jar
  8. commons-lang3-3.2.jar
  9. freemarker-2.3.22.jar
  10. javassist-3.11.0.GA.jar
  11. log4j-api-2.2.jar
  12. log4j-core-2.2.jar
  13. ognl-3.0.6.jar
  14. struts2-core-2.3.24.jar
  15. struts2-spring-plugin-2.3.24.jar
  16. xwork-core-2.3.24.jar

配置文件
web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. id="WebApp_ID" version="2.5">
  6. <display-name>day38_ssh1</display-name>
  7. <!--配置Spring框架整合WEB的监听器
  8. <listener>
  9. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  10. </listener>
  11. <context-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath:applicationContext.xml</param-value>
  14. </context-param>
  15. -->
  16. <!-- 配置Struts2框架的核心的过滤器 -->
  17. <filter>
  18. <filter-name>struts2</filter-name>
  19. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  20. </filter>
  21. <filter-mapping>
  22. <filter-name>struts2</filter-name>
  23. <url-pattern>/*</url-pattern>
  24. </filter-mapping>
  25. <welcome-file-list>
  26. <welcome-file>index.html</welcome-file>
  27. <welcome-file>index.htm</welcome-file>
  28. <welcome-file>index.jsp</welcome-file>
  29. <welcome-file>default.html</welcome-file>
  30. <welcome-file>default.htm</welcome-file>
  31. <welcome-file>default.jsp</welcome-file>
  32. </welcome-file-list>
  33. </web-app>

struts.xml

    路径:src
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <!-- 先配置包结构 -->
  7. <package name="crm" extends="struts-default" namespace="/">
  8. <!-- 是由Struts2框架自己来管理Action -->
  9. <!-- <action name="customer_*" class="com.itheima.web.action.CustomerAction" method="{1}"/> -->
  10. <!-- 配置客户的Action,如果Action由Spring框架来管理,class标签上只需要编写ID值就OK -->
  11. <action name="customer_*" class="customerAction" method="{1}">
  12. </action>
  13. </package>
  14. </struts>

Spring相关

引入jar

  1. com.springsource.org.aopalliance-1.0.0.jar
  2. com.springsource.org.apache.commons.logging-1.1.1.jar
  3. com.springsource.org.apache.log4j-1.2.15.jar
  4. com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
  5. spring-aop-4.2.4.RELEASE.jar
  6. spring-aspects-4.2.4.RELEASE.jar
  7. spring-beans-4.2.4.RELEASE.jar
  8. spring-context-4.2.4.RELEASE.jar
  9. spring-core-4.2.4.RELEASE.jar
  10. spring-expression-4.2.4.RELEASE.jar
  11. spring-jdbc-4.2.4.RELEASE.jar
  12. spring-orm-4.2.4.RELEASE.jar
  13. spring-test-4.2.4.RELEASE.jar
  14. spring-tx-4.2.4.RELEASE.jar
  15. spring-web-4.2.4.RELEASE.jar

配置文件

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. id="WebApp_ID" version="2.5">
  6. <display-name>day38_ssh1</display-name>
  7. <!-- 配置Spring框架整合WEB的监听器 -->
  8. <listener>
  9. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  10. </listener>
  11. <context-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath:applicationContext.xml</param-value>
  14. </context-param>
  15. <!-- 配置Struts2框架的核心的过滤器
  16. <filter>
  17. <filter-name>struts2</filter-name>
  18. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  19. </filter>
  20. <filter-mapping>
  21. <filter-name>struts2</filter-name>
  22. <url-pattern>/*</url-pattern>
  23. </filter-mapping>
  24. -->
  25. <welcome-file-list>
  26. <welcome-file>index.html</welcome-file>
  27. <welcome-file>index.htm</welcome-file>
  28. <welcome-file>index.jsp</welcome-file>
  29. <welcome-file>default.html</welcome-file>
  30. <welcome-file>default.htm</welcome-file>
  31. <welcome-file>default.jsp</welcome-file>
  32. </welcome-file-list>
  33. </web-app>

applicationContext.xml

路径:src/
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd">
  15. <!-- 编写bean,名称都是固定,加载hibernate.cfg.xml的配置文件 -->
  16. <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  17. <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
  18. </bean>
  19. <!-- 先配置平台事务管理器 -->
  20. <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  21. <property name="sessionFactory" ref="sessionFactory"/>
  22. </bean>
  23. <!-- 开启事务的注解 -->
  24. <tx:annotation-driven transaction-manager="transactionManager"/>
  25. <!-- 配置客户模块 -->
  26. <!-- 强调:以后配置Action,必须是多例的 -->
  27. <bean id="customerAction" class="com.itheima.web.action.CustomerAction" scope="prototype">
  28. <property name="customerService" ref="customerService"/>
  29. </bean>
  30. <bean id="customerService" class="com.itheima.service.CustomerServiceImpl">
  31. <property name="customerDao" ref="customerDao"/>
  32. </bean>
  33. <!-- 以后:Dao都需要继承HibernateDaoSupport,注入sessionFactory -->
  34. <bean id="customerDao" class="com.itheima.dao.CustomerDaoImpl">
  35. <property name="sessionFactory" ref="sessionFactory"/>
  36. </bean>
  37. <!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
  38. <property name="sessionFactory"></property>
  39. </bean> -->
  40. </beans>

log4j.properties(不修改)

路径:src/
  1. ### direct log messages to stdout ###
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  3. log4j.appender.stdout.Target=System.err
  4. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  5. log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
  6. ### direct messages to file mylog.log ###
  7. log4j.appender.file=org.apache.log4j.FileAppender
  8. log4j.appender.file.File=c\:mylog.log
  9. log4j.appender.file.layout=org.apache.log4j.PatternLayout
  10. log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
  11. ### set log levels - for more verbose logging change 'info' to 'debug' ###
  12. log4j.rootLogger=info, stdout

Hibrenate相关

引入jar

  1. antlr-2.7.7.jar
  2. c3p0-0.9.2.1.jar
  3. commons-beanutils-1.8.3.jar
  4. commons-logging-1.1.1.jar
  5. dom4j-1.6.1.jar
  6. geronimo-jta_1.1_spec-1.1.1.jar
  7. hibernate-c3p0-5.0.7.Final.jar
  8. hibernate-commons-annotations-5.0.1.Final.jar
  9. hibernate-core-5.0.7.Final.jar
  10. hibernate-jpa-2.1-api-1.0.0.Final.jar
  11. jandex-2.0.0.Final.jar
  12. javassist-3.18.1-GA.jar
  13. jboss-logging-3.3.0.Final.jar
  14. jstl.jar
  15. log4j-1.2.16.jar
  16. mchange-commons-java-0.2.3.4.jar
  17. mysql-connector-java-5.1.7-bin.jar
  18. slf4j-api-1.6.1.jar
  19. slf4j-log4j12-1.7.2.jar
  20. standard.jar

配置文件
Customer.hbm.xml

hibernate.cfg.xml

    路径:src/
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <!-- 必须配置 -->
  8. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  9. <property name="hibernate.connection.url">jdbc:mysql:///day38_ssh</property>
  10. <property name="hibernate.connection.username">root</property>
  11. <property name="hibernate.connection.password">root</property>
  12. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  13. <!-- 可选配置 -->
  14. <property name="hibernate.show_sql">true</property>
  15. <property name="hibernate.format_sql">true</property>
  16. <property name="hibernate.hbm2ddl.auto">update</property>
  17. <!-- 配置C3P0的连接池 -->
  18. <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
  19. <!-- 不能配置绑定当前的线程的操作 -->
  20. <!-- 映射配置文件 -->
  21. <mapping resource="com/itheima/domain/Customer.hbm.xml"/>
  22. </session-factory>
  23. </hibernate-configuration>

Spring整合Struts2

Spring框架管理Action

struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <package name="crm" extends="struts-default" namespace="/">
  7. <!-- 是由Struts2框架管理Action
  8. <action name="customer_*" class="com.itheima.web.action.CustomerAction" method="{1}"/>
  9. -->
  10. <!-- 注意一:Spring框架管理action:class标签上只需要编写applicationContext.xml中ID值就OK -->
  11. <action name="customer_*" class="customerAction" method="{1}">
  12. </action>
  13. </package>
  14. </struts>

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd">
  15. <!-- Spring管理action -->
  16. <!-- 注意二:配置Action,必须是多例的 -->
  17. <bean id="customerAction" class="com.itheima.web.action.CustomerAction" scope="prototype">
  18. <property name="customerService" ref="customerService"/>
  19. </bean>
  20. <bean id="customerService" class="com.itheima.service.CustomerServiceImpl">
  21. <property name="customerDao" ref="customerDao"/>
  22. </bean>
  23. </beans>

Spring整合Hibernate

配置hibernate.cfg.xml的方式

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd">
  15. <!--注意一:编写bean,名称都是固定,加载hibernate.cfg.xml的配置文件 -->
  16. <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  17. <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
  18. </bean>
  19. <!--先配置平台事务管理器-->
  20. <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  21. <property name="sessionFactory" ref="sessionFactory"/>
  22. </bean>
  23. <!--开启事务的注解-->
  24. <tx:annotation-driven transaction-manager="transactionManager"/>
  25. <!-- 配置Struts2
  26. 配置客户模块
  27. 强调:以后配置Action,必须是多例的
  28. <bean id="customerAction" class="com.itheima.web.action.CustomerAction" scope="prototype">
  29. <property name="customerService" ref="customerService"/>
  30. </bean>
  31. <bean id="customerService" class="com.itheima.service.CustomerServiceImpl">
  32. <property name="customerDao" ref="customerDao"/>
  33. </bean>
  34. -->
  35. <!-- 注意二:Dao都需要继承HibernateDaoSupport,注入sessionFactory -->
  36. <bean id="customerDao" class="com.itheima.dao.CustomerDaoImpl">
  37. <property name="sessionFactory" ref="sessionFactory"/>
  38. </bean>
  39. </beans>

持久层

  1. /**
  2. * 持久层
  3. */
  4. public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
  5. /**
  6. * 保存客户
  7. */
  8. public void save(Customer customer) {
  9. System.out.println("持久层:保存客户...");
  10. // 把数据,保存到数据库中
  11. this.getHibernateTemplate().save(customer);
  12. }
  13. }

业务层

  1. /**
  2. * 客户的业务层
  3. */
  4. @Transactional
  5. public class CustomerServiceImpl implements CustomerService {
  6. private CustomerDao customerDao;
  7. public void setCustomerDao(CustomerDao customerDao) {
  8. this.customerDao = customerDao;
  9. }
  10. /**
  11. * 保存客户
  12. */
  13. public void save(Customer customer) {
  14. System.out.println("业务层:保存客户...");
  15. customerDao.save(customer);
  16. }
  17. }

无hibernate.cfg.xml的方式

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd">
  15. <!--步骤一:先配置C3P0的连接池 -->
  16. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  17. <property name="driverClass" value="com.mysql.jdbc.Driver"/>
  18. <property name="jdbcUrl" value="jdbc:mysql:///day38_ssh"/>
  19. <property name="user" value="root"/>
  20. <property name="password" value="root"/>
  21. </bean>
  22. <!--步骤二:LocalSessionFactoryBean加载配置文件 -->
  23. <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  24. <!-- 先加载连接池 -->
  25. <property name="dataSource" ref="dataSource"/>
  26. <!-- 加载方言,加载可选 -->
  27. <property name="hibernateProperties">
  28. <props>
  29. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  30. <prop key="hibernate.show_sql">true</prop>
  31. <prop key="hibernate.format_sql">true</prop>
  32. <prop key="hibernate.hbm2ddl.auto">update</prop>
  33. </props>
  34. </property>
  35. <!-- 引入映射的配置文件 -->
  36. <property name="mappingResources">
  37. <list>
  38. <value>com/itheima/domain/Customer.hbm.xml</value>
  39. </list>
  40. </property>
  41. </bean>
  42. <!-- 先配置平台事务管理器 -->
  43. <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  44. <property name="sessionFactory" ref="sessionFactory"/>
  45. </bean>
  46. <!-- 开启事务的注解 -->
  47. <tx:annotation-driven transaction-manager="transactionManager"/>
  48. <!--配置Struts2
  49. 配置客户模块
  50. 强调:以后配置Action,必须是多例的
  51. <bean id="customerAction" class="com.itheima.web.action.CustomerAction" scope="prototype">
  52. <property name="customerService" ref="customerService"/>
  53. </bean>
  54. <bean id="customerService" class="com.itheima.service.CustomerServiceImpl">
  55. <property name="customerDao" ref="customerDao"/>
  56. </bean>
  57. -->
  58. <!-- 步骤三:Dao都需要继承HibernateDaoSupport,注入sessionFactory -->
  59. <bean id="customerDao" class="com.itheima.dao.CustomerDaoImpl">
  60. <property name="sessionFactory" ref="sessionFactory"/>
  61. </bean>
  62. <!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
  63. <property name="sessionFactory"></property>
  64. </bean> -->
  65. </beans>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注