1.加入spring
1)加入jar包
2)配置web.xml applicationContext.xml (监听器)
3)加入spring的配置文件:aop context tx bean
1.加入hibernate
1.1建立持久化类,和其对应的xxx,hbm。xml文件,生产对应的数据表
1.2spring 整合hibernate
1)加入jar包
2)类路径下加入hibernate.cfg.xml文件.其中配置hibernate的基本配置
3)建立持久化类和对应的xxx.hbm。xml
4)和spring整合
a)加入c3p0和mysql的驱动
b)在spring的配置文件中配置:数据源,sessionFactory,声明式事务
5)启动项目,会看到生产对应的数据表
3.加入struts2
1)加入jar包,有重复的包,删除版本低的包
2)在web。xml文件中配置Struts2的Filter
3)加入配置文件Struts.xml
4)整合Spring
a)加入Struts2的Spring插件的jar包
b)在Spring的配置文件中正常配置Action,并设置Scope为prototype
c)在Struts2的配置文件中Action时,class属性指向该Action在IOC中的id
注:以下代码的首行仅仅起到标记注释作用,开发时请去掉!
<!-- 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_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- 配置Spring的监听器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置Struts2的Filter --> <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>
<!-- 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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 导入资源文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <property name="mappingLocations" value="classpath:xby/ssh/entities/*.hbm.xml"></property> </bean> <!-- 配置spring声明式事务 --> <!-- 1.配置hibernate的事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 2.配置事务属性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 3.配置事务切入点->再把事务属性和事务切入点关联起来 --> <aop:config> <aop:pointcut expression="execution(* xby.ssh.service.*.*(..))" id="txPointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> </beans>
<!-- applicationContext-beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="employeeDao" class="xby.ssh.dao.EmployeeDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="employeeService" class="xby.ssh.service.EmployeeService"> <property name="employeeDao" ref="employeeDao"></property> </bean> <bean id="employeeAction" class="xby.ssh.actions.EmployeeAction" scope="prototype"> <property name="employeeService" ref="employeeService"></property> </bean> </beans>
<!-- struts.xml --> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 该属性设置Struts2是否支持动态方法调用,该属性的默认值是true --> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="emp-*" class="employeeAction" method="{1}"> <result name="list">/WEB-INF/views/emp-list.jsp</result> </action> </package> </struts>
<!-- hibernate.cfg.xml --> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置基本熟属性 --> <!-- 数据库方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect </property> <!-- 显示sql语句 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- 生产数据表的策略--> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 二级缓存--> </session-factory> </hibernate-configuration>
//db.propertites jdbc.user=root jdbc.password=root jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssh jdbc.initPoolSize=5 jdbc.maxPoolSize=10
以上是所有的jar包,完全版,经测试没有问题。有需要者,可私信我~
时间: 2024-11-13 02:15:42