S2SH框架(Struts2,Spring,Hibernate)整合

  Struts2、Hibernate和Spring。其中在Struts2部分主要为MVC设计思想,Struts2的处理流程及配置,Struts2常用控制器组件,以及Struts2常用标签库的使用。在Hibernate部分主要为O/R Mapping的设计理念,Hibernate对O/R Mapping的支持,Hibernate的配置及多种关系映射的实现,以及HQL查询数据。在Spring部分主要为IoC的原理,Spring对Bean的管理机制,Spring AOP编程,以及声明事务的配置和管理。

  下面来配置一下SSH搭建环境:

  首先配置Hibernate环境,参考我之前的博客:http://www.cnblogs.com/claricre/p/6509931.html

  接下来就是要配置Struts环境,参考我的博客:http://www.cnblogs.com/claricre/p/6542757.html

  然后是配置Spring,参考博客:http://www.cnblogs.com/claricre/p/6686231.html

  整合流程就是上面三篇博客,下面贴出来整个配置环境:

NationAction.java:

package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class NationAction extends ActionSupport {
public String show(){
return SUCCESS;
}
}

Nation.java实体类:

package com.model;
// Generated 2017-4-11 16:20:44 by Hibernate Tools 5.2.0.CR1 /**
* Nation generated by hbm2java
*/
public class Nation implements java.io.Serializable { private String code;
private String name; public Nation() {
} public Nation(String code) {
this.code = code;
} public Nation(String code, String name) {
this.code = code;
this.name = name;
} public String getCode() {
return this.code;
} public void setCode(String code) {
this.code = code;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} }

Nation.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-4-11 16:20:44 by Hibernate Tools 5.2.0.CR1 -->
<hibernate-mapping>
<class name="com.model.Nation" table="nation" catalog="mydb" optimistic-lock="version">
<id name="code" type="string">
<column name="Code" length="50" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="Name" length="50" />
</property>
</class>
</hibernate-mapping>

以上两个文件是通过Hibernate配置直接根据数据库表生成。

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"
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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <context:property-placeholder location="classpath:db.properties"/>
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property> <property name="minPoolSize" value="${minPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean> <!-- hibernateTemplate --><!-- 如果需要可以引入hibernatTemolate来替代sessionFactory -->
<!-- <bean class="org.springframework.orm.hibernate5.HibernateTemplate" id="hibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> -->
<!-- jdbcTemplate --><!-- 如果需要可以jdbcTemplate -->
<!-- <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> --> <bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" id="sessionFactory">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:com/model/*.hbm.xml"></property>
</bean> <bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" id="transactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- <tx:advice transaction-manager="transactionManager" id="txAdvice">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut expression="execution(* com.model.*.*(..))" id="pointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
</aop:config> --> </beans>

db.properties:

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/mydb
user=root
password=
minPoolSize=5
maxPoolSize=20
initialPoolSize=5

hibernate.cfg.xml:

<?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://localhost:3306/mydb?characterEncoding=GBK</property>
<property name="hibernate.connection.username">root</property> -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property> <!-- <mapping resource="com/model/Nation.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> <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" /> <package name="default" namespace="" extends="struts-default"> <action name="*_*" class="com.action.{1}Action" method="{2}">
<result>
{1}/{2}.jsp
</result>
</action>
</package> </struts>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <display-name>Struts Blank</display-name> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <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-list> </web-app>

在页面中直接跑http://localhost:8080/SSH_zhenghe/Nation_show即可调出show.jsp页面的内容来了。

整合S2SH框架的更多相关文章

  1. S2SH框架的集成

    S2SH框架的集成 20131130 代码下载 : 链接: http://pan.baidu.com/s/1sz6cQ 密码: 513t 这一个星期的时间里,学习SSH框架技术,首先因为之前的项目用到 ...

  2. 【Spring】Spring系列7之Spring整合MVC框架

    7.Spring整合MVC框架 7.1.web环境中使用Spring 7.2.整合MVC框架 目标:使用Spring管理MVC的Action.Controller 最佳实践参考:http://www. ...

  3. Springmvc整合tiles框架简单入门示例(maven)

    Springmvc整合tiles框架简单入门示例(maven) 本教程基于Springmvc,spring mvc和maven怎么弄就不具体说了,这边就只简单说tiles框架的整合. 先贴上源码(免积 ...

  4. SpringMVC整合Tiles框架

    SpringMVC整合Tiles框架 Tiles组件 tiles-iconfig.xml Tiles是一个JSP布局框架. Tiles框架为创建Web页面提供了一种模板机制,它能将网页的布局和内容分离 ...

  5. Yii框架学习笔记(二)将html前端模板整合到框架中

    选择Yii 2.0版本框架的7个理由 http://blog.chedushi.com/archives/8988 刚接触Yii谈一下对Yii框架的看法和感受 http://bbs.csdn.net/ ...

  6. 整合MVVM框架(Prism)

    整合MVVM框架(Prism) 我们基础的框架已经搭建起来了,现在整合MVVM框架Prism,在ViewModel做一些逻辑处理,真正把界面设计分离出来. 这样方便我们系统开发分工合作,同时提高系统可 ...

  7. shiro权限控制(一):shiro介绍以及整合SSM框架

    shiro安全框架是目前为止作为登录注册最常用的框架,因为它十分的强大简单,提供了认证.授权.加密和会话管理等功能 . shiro能做什么? 认证:验证用户的身份 授权:对用户执行访问控制:判断用户是 ...

  8. Maven 整合 SSH 框架

    前面的一系列文章中,我们总结了三大框架:Struts2,Hibernate,Spring 的基本知识.本篇就姑且尝试着使用 Maven 这个项目构建工具来将这三个框架整合一起.说到这里,如果有对 Ma ...

  9. Maven02——回顾、整合ssh框架、分模块开发、私服

    1 回顾 1.1 Maven的好处 节省空间 对jar包做了统一管理 依赖管理 一键构建 可跨平台 应用在大型项目可提高开发效率 1.2 Maven安装部署配置 1.3 Maven的仓库 本地仓库 远 ...

随机推荐

  1. css中有些属性的前面会加上“*”或“_”,请问分别表示什么意思?

    给不同的浏览器识别 例如: color{ background-color: #CC00FF; /*所有浏览器都会显示为紫色*/ background-color: #FF0000\9; /*IE6. ...

  2. Java连接数据库的一个问题

    问题描述: 利用HTML+servlet+MySQL写一个简单的登录注册案例,抛出了异常No suitable driver found for jdbc 解决方法 将mysql-connector- ...

  3. JS - 属性描述符各配置的默认值的注意事项

    通过字面量或者obj.x = 1;创建的属性 与 通过Object.defineProperty创建的属性,他们的属性描述符的默认值是不同的,前者都为true,后者都为false.

  4. mysql 命令 小结

    CREATE DATABASE IF NOT EXISTS yourdbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci;创建中文数据库show gl ...

  5. Linux帮助都有哪几种,如何使用?

    帮助文件有2类,内置命令和外部命令. 其中内置命令就是shell内核自带的,因为shell当中自己要进行管理,那么就需要一些命令进行管理,不同的shell肯定有不同的shell命令,我们用type命令 ...

  6. Shell学习——数值运算

    在Bash shell中,可以利用let.(( )).[]执行基本的算术操作,在高级操作时,使用expr和bc两个工具1.let[root@client02 ~]# no1=4[root@client ...

  7. ethereum(以太坊)(三)--合约单继承与多继承

    pragma solidity ^0.4.0; // priveta public internal contract Test{ //defualt internal uint8 internal ...

  8. python__基础 : 类属性,类方法,静态方法

    类属性  定义在类里面,方法外面的属性,一般属于这个类,如下面的 num 就是类属性: class Test: num = 类属性用 实例.类属性   或者 类.类属性 都可以访问, 如 a = Te ...

  9. VIM配置IDE

    转载于:https://blog.csdn.net/andre617/article/details/53496490#%E8%84%9A%E6%B3%A8 由于YCM需要vim支持python,需要 ...

  10. C++ 二叉搜索树

    二叉搜索树利用其特有的二叉树性质,使其搜索更方便 源代码: struct node { int val; node *left, *right; }; //the function of insert ...