搭建Struts2+Spring+Hibernate三大框架的步骤
2016-05-12 21:24
405 查看
SSH三大框架整合步骤
准备工作:导入ssh三大框架所需的jar包,创建struts.xml和applicationContext.xml文件。
applicationContext.xml文件的头部标签
1、先搭建struts框架
1-1:在web.xml文件中配置过滤器和映射。
<!-- 添加struts的过滤器 -->
<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>
1-2:在struts.xml文件中配置packet和action
2、搭建spring框架
2-1:创建applicationContext.xml文件,为配置相应的bean做准备
2-2:在web.xml文件中配置applicationContext.xml文件和spring的监听
<!-- 配置applicationContext.xml文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置spring的监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3、搭建hibernate框架
第一步: 创建三层模型
第二步: 在applicationContext.xml文件中配置Hibernate框架
1、配置dataSource
1-1:配置驱动
1-2:URL
1-3:用户名
1-4:密码
1-5:连接池的配置(可选)
具体代码如下
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置驱动 -->
<property name="driverClass">
<value>数据库的驱动</value>
</property>
<!-- URL -->
<property name="jdbcUrl">
<value>数据库的URL</value>
</property>
<!-- 用户名-->
<property name="user">
<value>数据库用户名</value>
</property>
<!-- 密码-->
<property name="password">
<value>数据库密码</value>
</property>
<!-- 连接池的配置 -->
<property name="maxPoolSize">
<value>80</value>
</property>
<property name="minPoolSize">
<value>1</value>
</property>
<property name="initialPoolSize">
<value>1</value>
</property>
<property name="maxIdleTime">
<value>20</value>
</property>
</bean>
<
b61b
/p>
2、配置sessionFactory
2-1:引用数据源bean
2-2:添加映射文件
2-3:配置属性信息
具体代码如下
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 引用数据源bean -->
<property name="dataSource" ref="dataSource"/>
<!-- 添加映射文件 -->
<property name="mappingResources">
<list>
<value>com/caokaiyuan/domain/User.hbm.xml</value>
</list>
</property>
<!-- 配置属性信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">方言</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
3、配置事务
3-1:配置的管理器
3-2:配置事务的传播特性
3-3:AOP切面编程来整合事务
具体代码如下
<!-- 配置事务 -->
<!-- 配置的管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- AOP切面编程来整合事务 -->
<aop:config>
<aop:advisor pointcut="execution(* com.lanqiao.service.impl.*Service*.*(..))"
advice-ref="txAdvice" />
</aop:config>
4、配置dao层
5、配置service层
6、配置action对象
准备工作:导入ssh三大框架所需的jar包,创建struts.xml和applicationContext.xml文件。
applicationContext.xml文件的头部标签
1、先搭建struts框架
1-1:在web.xml文件中配置过滤器和映射。
<!-- 添加struts的过滤器 -->
<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>
1-2:在struts.xml文件中配置packet和action
2、搭建spring框架
2-1:创建applicationContext.xml文件,为配置相应的bean做准备
2-2:在web.xml文件中配置applicationContext.xml文件和spring的监听
<!-- 配置applicationContext.xml文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置spring的监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3、搭建hibernate框架
第一步: 创建三层模型
第二步: 在applicationContext.xml文件中配置Hibernate框架
1、配置dataSource
1-1:配置驱动
1-2:URL
1-3:用户名
1-4:密码
1-5:连接池的配置(可选)
具体代码如下
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置驱动 -->
<property name="driverClass">
<value>数据库的驱动</value>
</property>
<!-- URL -->
<property name="jdbcUrl">
<value>数据库的URL</value>
</property>
<!-- 用户名-->
<property name="user">
<value>数据库用户名</value>
</property>
<!-- 密码-->
<property name="password">
<value>数据库密码</value>
</property>
<!-- 连接池的配置 -->
<property name="maxPoolSize">
<value>80</value>
</property>
<property name="minPoolSize">
<value>1</value>
</property>
<property name="initialPoolSize">
<value>1</value>
</property>
<property name="maxIdleTime">
<value>20</value>
</property>
</bean>
<
b61b
/p>
2、配置sessionFactory
2-1:引用数据源bean
2-2:添加映射文件
2-3:配置属性信息
具体代码如下
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 引用数据源bean -->
<property name="dataSource" ref="dataSource"/>
<!-- 添加映射文件 -->
<property name="mappingResources">
<list>
<value>com/caokaiyuan/domain/User.hbm.xml</value>
</list>
</property>
<!-- 配置属性信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">方言</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
3、配置事务
3-1:配置的管理器
3-2:配置事务的传播特性
3-3:AOP切面编程来整合事务
具体代码如下
<!-- 配置事务 -->
<!-- 配置的管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- AOP切面编程来整合事务 -->
<aop:config>
<aop:advisor pointcut="execution(* com.lanqiao.service.impl.*Service*.*(..))"
advice-ref="txAdvice" />
</aop:config>
4、配置dao层
5、配置service层
6、配置action对象
相关文章推荐
- Linux Generating SSH Keys
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- JQuery+Strusts1.x无刷新登录
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- perl脚本实现限制ssh最大登录次数(支持白名单)
- rsync ssh 数据同步分析
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- 搭建SSH时的思考和遇到的几个问题的解决方法
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- springmvc 发送ajax出现中文乱码的解决方法汇总
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- java struts常见错误以及原因分析