@Beeder
2018-02-25T03:30:40.000000Z
字数 14249
阅读 559
javaWeb Struts2 Spring Hibernate

引入jar
//struts2整合spring相关jar包struts2-spring-plugin-2.3.24.jarasm-3.3.jarasm-commons-3.3.jarasm-tree-3.3.jarcommons-fileupload-1.3.1.jarcommons-io-2.2.jarcommons-lang3-3.2.jarfreemarker-2.3.22.jarjavassist-3.11.0.GA.jarlog4j-api-2.2.jarlog4j-core-2.2.jarognl-3.0.6.jarstruts2-core-2.3.24.jarstruts2-spring-plugin-2.3.24.jarxwork-core-2.3.24.jar
配置文件
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>day38_ssh1</display-name><!--配置Spring框架整合WEB的监听器<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><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><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></web-app>
struts.xml
路径:src
<?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><!-- 先配置包结构 --><package name="crm" extends="struts-default" namespace="/"><!-- 是由Struts2框架自己来管理Action --><!-- <action name="customer_*" class="com.itheima.web.action.CustomerAction" method="{1}"/> --><!-- 配置客户的Action,如果Action由Spring框架来管理,class标签上只需要编写ID值就OK --><action name="customer_*" class="customerAction" method="{1}"></action></package></struts>
引入jar
com.springsource.org.aopalliance-1.0.0.jarcom.springsource.org.apache.commons.logging-1.1.1.jarcom.springsource.org.apache.log4j-1.2.15.jarcom.springsource.org.aspectj.weaver-1.6.8.RELEASE.jarspring-aop-4.2.4.RELEASE.jarspring-aspects-4.2.4.RELEASE.jarspring-beans-4.2.4.RELEASE.jarspring-context-4.2.4.RELEASE.jarspring-core-4.2.4.RELEASE.jarspring-expression-4.2.4.RELEASE.jarspring-jdbc-4.2.4.RELEASE.jarspring-orm-4.2.4.RELEASE.jarspring-test-4.2.4.RELEASE.jarspring-tx-4.2.4.RELEASE.jarspring-web-4.2.4.RELEASE.jar
配置文件
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>day38_ssh1</display-name><!-- 配置Spring框架整合WEB的监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><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>--><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></web-app>
applicationContext.xml
路径:src/
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 编写bean,名称都是固定,加载hibernate.cfg.xml的配置文件 --><bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><property name="configLocation" value="classpath:hibernate.cfg.xml"/></bean><!-- 先配置平台事务管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"/></bean><!-- 开启事务的注解 --><tx:annotation-driven transaction-manager="transactionManager"/><!-- 配置客户模块 --><!-- 强调:以后配置Action,必须是多例的 --><bean id="customerAction" class="com.itheima.web.action.CustomerAction" scope="prototype"><property name="customerService" ref="customerService"/></bean><bean id="customerService" class="com.itheima.service.CustomerServiceImpl"><property name="customerDao" ref="customerDao"/></bean><!-- 以后:Dao都需要继承HibernateDaoSupport,注入sessionFactory --><bean id="customerDao" class="com.itheima.dao.CustomerDaoImpl"><property name="sessionFactory" ref="sessionFactory"/></bean><!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"><property name="sessionFactory"></property></bean> --></beans>
log4j.properties(不修改)
路径:src/
### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.errlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### direct messages to file mylog.log ###log4j.appender.file=org.apache.log4j.FileAppenderlog4j.appender.file.File=c\:mylog.loglog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.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
引入jar
antlr-2.7.7.jarc3p0-0.9.2.1.jarcommons-beanutils-1.8.3.jarcommons-logging-1.1.1.jardom4j-1.6.1.jargeronimo-jta_1.1_spec-1.1.1.jarhibernate-c3p0-5.0.7.Final.jarhibernate-commons-annotations-5.0.1.Final.jarhibernate-core-5.0.7.Final.jarhibernate-jpa-2.1-api-1.0.0.Final.jarjandex-2.0.0.Final.jarjavassist-3.18.1-GA.jarjboss-logging-3.3.0.Final.jarjstl.jarlog4j-1.2.16.jarmchange-commons-java-0.2.3.4.jarmysql-connector-java-5.1.7-bin.jarslf4j-api-1.6.1.jarslf4j-log4j12-1.7.2.jarstandard.jar
配置文件
Customer.hbm.xml
hibernate.cfg.xml
路径:src/
<?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:///day38_ssh</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">root</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><!-- 可选配置 --><property name="hibernate.show_sql">true</property><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><!-- 不能配置绑定当前的线程的操作 --><!-- 映射配置文件 --><mapping resource="com/itheima/domain/Customer.hbm.xml"/></session-factory></hibernate-configuration>
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><package name="crm" extends="struts-default" namespace="/"><!-- 是由Struts2框架管理Action<action name="customer_*" class="com.itheima.web.action.CustomerAction" method="{1}"/>--><!-- 注意一:Spring框架管理action:class标签上只需要编写applicationContext.xml中ID值就OK --><action name="customer_*" class="customerAction" method="{1}"></action></package></struts>
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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!-- Spring管理action --><!-- 注意二:配置Action,必须是多例的 --><bean id="customerAction" class="com.itheima.web.action.CustomerAction" scope="prototype"><property name="customerService" ref="customerService"/></bean><bean id="customerService" class="com.itheima.service.CustomerServiceImpl"><property name="customerDao" ref="customerDao"/></bean></beans>
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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!--注意一:编写bean,名称都是固定,加载hibernate.cfg.xml的配置文件 --><bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><property name="configLocation" value="classpath:hibernate.cfg.xml"/></bean><!--先配置平台事务管理器--><bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"/></bean><!--开启事务的注解--><tx:annotation-driven transaction-manager="transactionManager"/><!-- 配置Struts2配置客户模块强调:以后配置Action,必须是多例的<bean id="customerAction" class="com.itheima.web.action.CustomerAction" scope="prototype"><property name="customerService" ref="customerService"/></bean><bean id="customerService" class="com.itheima.service.CustomerServiceImpl"><property name="customerDao" ref="customerDao"/></bean>--><!-- 注意二:Dao都需要继承HibernateDaoSupport,注入sessionFactory --><bean id="customerDao" class="com.itheima.dao.CustomerDaoImpl"><property name="sessionFactory" ref="sessionFactory"/></bean></beans>
持久层
/*** 持久层*/public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {/*** 保存客户*/public void save(Customer customer) {System.out.println("持久层:保存客户...");// 把数据,保存到数据库中this.getHibernateTemplate().save(customer);}}
业务层
/*** 客户的业务层*/@Transactionalpublic class CustomerServiceImpl implements CustomerService {private CustomerDao customerDao;public void setCustomerDao(CustomerDao customerDao) {this.customerDao = customerDao;}/*** 保存客户*/public void save(Customer customer) {System.out.println("业务层:保存客户...");customerDao.save(customer);}}
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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!--步骤一:先配置C3P0的连接池 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql:///day38_ssh"/><property name="user" value="root"/><property name="password" value="root"/></bean><!--步骤二:LocalSessionFactoryBean加载配置文件 --><bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><!-- 先加载连接池 --><property name="dataSource" ref="dataSource"/><!-- 加载方言,加载可选 --><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.hbm2ddl.auto">update</prop></props></property><!-- 引入映射的配置文件 --><property name="mappingResources"><list><value>com/itheima/domain/Customer.hbm.xml</value></list></property></bean><!-- 先配置平台事务管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"/></bean><!-- 开启事务的注解 --><tx:annotation-driven transaction-manager="transactionManager"/><!--配置Struts2配置客户模块强调:以后配置Action,必须是多例的<bean id="customerAction" class="com.itheima.web.action.CustomerAction" scope="prototype"><property name="customerService" ref="customerService"/></bean><bean id="customerService" class="com.itheima.service.CustomerServiceImpl"><property name="customerDao" ref="customerDao"/></bean>--><!-- 步骤三:Dao都需要继承HibernateDaoSupport,注入sessionFactory --><bean id="customerDao" class="com.itheima.dao.CustomerDaoImpl"><property name="sessionFactory" ref="sessionFactory"/></bean><!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"><property name="sessionFactory"></property></bean> --></beans>