struts2+spring4.x+hibernate5.x
来源:互联网 发布:淘宝企业店营业执照 编辑:程序博客网 时间:2024/06/08 15:52
0.导入struts2+spring4.x+hibernate5.x的jar,这里额外用到c3p0.jar和aspectjweaver.jar,没有的自己网上找找
1.web.xml配置spring的监听器,还有struts2的过滤器和spring的session处理
<?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"> <display-name>sturts2SpringHibernate</display-name> <!-- needed for ContextLoaderListener --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- Bootstraps the root web application context before servlet initialization --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> <filter> <filter-name>sturts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter> <filter-name>SpringOpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>SpringOpenSessionInViewFilter</filter-name> <url-pattern>*.do,*.action</url-pattern> </filter-mapping> <!-- END SNIPPET: filter --> <filter-mapping> <filter-name>sturts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
2.db.properties的配置
jdbc.user=rootjdbc.password=soft1403jdbc.driverClass=com.mysql.jdbc.Driverjdbc.jdbcUrl=jdbc:mysql:///springHibernatejdbc.initialPoolSize=5jdbc.maxPoolSize=103.hibernate的配置文件
<?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> <!-- 配置hibernate的基本属性 --><!-- 1.数据源需配到IOC容器中,所以在此不再配置数据源 2.关联.hbm.xml也在IOC容器配置SessionFactory实列时在进行配置 3.配置hibernate的基本属性:SQL显示及格式化,生成数据表的策略以及二级缓存等。 --><property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property><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>4.spring的配置文件
<?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:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"><!-- 配置一个自动扫描的包 --><context:component-scan base-package="com.test"></context:component-scan><!-- 导入资源文件,并配置数据源 --><context:property-placeholder location="classpath:db.properties" /><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.initialPoolSize}"></property><property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property></bean><!-- 配置Hibernate的SessionFactory 实列:通过Spring提供的LocalSessionFactoryBean进行配置 --><bean id="sessionFactory"class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><!-- 配置数据源属性 --><property name="dataSource" ref="dataSource"></property><!-- 配置hibernate配置文件的路径 --><property name="configLocation" value="classpath:hibernate.cfg.xml"></property><!-- 配置hibernate映射文件的位置及名称 ,可以使用通配符 --><property name="mappingLocations" value="classpath:com/test/model/*.hbm.xml"></property></bean><!-- 配置spring的声明式事物 --><!-- 1.配置事物管理器 --><bean id="transactionManager"class="org.springframework.orm.hibernate5.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 2.配置事物属性,需要事物管理器 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="add*" propagation="REQUIRED"/> <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED" /><tx:method name="*" rollback-for="Exception"/></tx:attributes></tx:advice><!-- 3.配置事物切点,并把切点与事物属性关联起来 --> <aop:config > <aop:pointcut expression="execution(* com.test.service.*.*(..))" id="txPointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> <!-- <bean id="PersonDaoImpl" class="com.test.daoimpl.PersonDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property></bean><bean id="PersonServicesImpl" class="com.test.serviceimpl.PersonServicesImpl"> <property name="pdl" ref="PersonDaoImpl"></property></bean>--><!-- 下面是整合sturts2的相关配置 --><bean id="person" class="com.test.model.person"><property name="id" value="1"></property><property name="name" value="夏根"></property><property name="age" value="18"></property><property name="sex" value="男"></property></bean><!-- 注意sturts2的action必须是非单列的因此要加上 scope="prototype" --><bean id="personAction" class="com.test.action.personAction"scope="prototype"><property name="pe" ref="person"></property></bean></beans>
5.结合上面的配置文件看下面的工程文件图
6.总结下千万注意aspectjweaver.jar包是spring事物管理service最主要的连接包,千万别漏了。。。。。。。开心就好 哈哈
0 0
- struts2+spring4.x+hibernate5.x
- Spring4.x+Struts2.x整合
- Spring4.X整合hibernate5.X之事物管理
- Struts2+Spring4+Hibernate5+Maven
- IDEA 整合 Struts2+Spring4+Hibernate5
- SSH(Spring4+Struts2+Hibernate5整合)
- Struts2.x、Hibernate4.x、Spring4.x重点梳理
- Hibernate5.x.x创建SessionFactory
- Hibernate5.x 基本开发
- hibernate5.X学习笔记
- Struts2+Hibernate5+Spring4自学历程(1)
- SSH框架的简化(struts2、spring4、hibernate5)
- spring4+hibernate5+dbcp2+struts2基础配置
- struts2.3.31+spring4.3.3+hibernate5.1.3集成
- spring4+struts2+hibernate5整合出现spring异常
- SSH(Struts2+Spring4+Hibernate5)示例
- Struts2.5+Spring4.3+Hibernate5搭建
- Spring4.x+hibernate5.x的整合之基础操作的封装(CRUD包含分页)(待补充)
- 排列统计 jzoj 1283
- iOS开发笔记
- 贪婪匹配
- 常用java服务器
- “==”和equals的理解
- struts2+spring4.x+hibernate5.x
- N次剩余(详解+例题+代码)
- 两个数的交换
- 农村老人的一生
- 将二进制转换为其它进制(这里转化为16进制,其它自己尝试)
- HDU Basic Data Structure 2016CCPC东北地区大学生程序设计竞赛 - 重现赛
- 剑指Offer面试题30(java版):最小的k个数
- 1613-3-傅溥衍 总结《2016年10月6日》【连续第六天总结】
- 【Java学习】排序_插入排序