来源于:http://www.jianshu.com/p/8e2f92d0838c

具体配置参数:

Spring: spring-framework-4.2.2
Hibernate: hibernate-release-4.2.21.Final
Eclipse : eclipse MARS.2
MySQL : mysql 5.5 +Navicat Premiumd视图器
System: win 8.1

Spring-framework下载(附地址)

需要下载的文件前面两个就够了,包和参考文档

  • spring-framework-4.2.2.RELEASE-dist.zip 包
  • spring-framework-4.2.2.RELEASE-docs.zip 文档

  • spring-framework-4.2.2.RELEASE-schema.zip 配置

导入SSH框架整合所需的jar包

配置web.xml

手动创建一个config文件夹用与存放配置的文件,这里方便说明记为cf

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:xx.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

其中xx.xml文件是需要你在cf中手动创建的配置文件,里面具体内容后面配置,这里相当于是告诉系统文件在哪,这里为了说明方便命名为spring.xml

     <servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:xxx.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

这里的xxx.xml同上,可自己命名,这里命名为springMVC.xml

其中filter-class里的名字不用记,可以通过ctrl+shift+T输入
CharacterEncodingFilter获得,且这一步需放在所有过滤器最前面,才有效果

    <filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

其中filter-name里的类名可以通过ctrl+shift+T输入HiddenHttpMethodFilter获得,由于浏览器form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,Spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求,该过滤器为HiddenHttpMethodFilter

  <filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
  • 配置SpringMVC

    • 导入命名空间
      打开我们自己定义的配置文件springMVC.xml选择Namespace,勾选context和MVC
    • 添加组成扫描,包含过滤注解

           <context:component-scan base-package="com.jikexueyuancrm"
      use-default-filters="false">
      <context:include-filter type="annotation"
      expression="org.springframework.stereotype.Controller" />
      <context:include-filter type="annotation"
      expression="org.springframework.web.bind.annotation.ControllerAdvice" />
      </context:component-scan>
    • 添加视图解析器

      其中class类名可以通过ctrl+shift+T输入InternalResourceViewResolver获得

            <bean
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/"></property>
      <property name="suffix" value=".jsp"></property>
      </bean>
    • 配置静态资源

          <mvc:default-servlet-handler />
    • 配置注解

          <mvc:annotation-driven />
  • 配置Spring

    • 导入命名空间
      打开我们自己定义的配置文件spring.xml选择Namespace,勾选context(上下文)和tx(事物)
    • 添加组成扫描,排除被SpringMVC包含的过滤注解

          <context:component-scan base-package="com.jikexueyuancrm"
      use-default-filters="false">
      <context:exclude-filter type="annotation"
      expression="org.springframework.stereotype.Controller" />
      <context:exclude-filter type="annotation"
      expression="org.springframework.web.bind.annotation.ControllerAdvice" />
      </context:component-scan>
    • 配置数据库

       <context:property-placeholder location="classpath:db.properties" />
    • 数据库内容

              jdbc.user=root
      jdbc.passowrd=
      jdbc.driverClass=com.mysql.jdbc.Driver
      jdbc.jdbcUrl=jdbc:mysql:///jianlam
    • 配置DataSource-数据库关联的东西

      这里用到cp30的包,C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。

      <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
      <property name="user" value="${jdbc.user}"></property>
      <property name="password" value="${jdbc.passowrd}"></property>
      <property name="driverClass" value="${jdbc.driverClass}"></property>
      <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
      </bean>
    • 配置hibernate整合

      需要orm包-Object Relational Mapping 对象关系映射,POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans

        <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
      id="sessionFactory">
      <!-- 配置数据源 -->
      <property name="dataSource" ref="dataSource"></property>
      <!-- 找到实体包(pojo) -->
      <property name="namingStrategy">
      <bean class="org.hibernate.cfg.ImprovedNamingStrategy"></bean>
      </property>
      <property name="packagesToScan" value="com.jianlam.entity"></property>
    • 配置hibernate常用属性

      <property name="hibernateProperties">
      <props>
      <!-- 数据库的方言 -->
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
      <prop key="hibernate.show_sql">true</prop>
      <prop key="hibernate.format_sql">true</prop>
      <prop key="hibernate.hbm2ddl.auto">update</prop>
      </props>
      </property>
      </bean>
    • 配置hibernate事物管理器

        <bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory"></property>
      </bean>

测试

创建好实体类和数据库表,查看连接操作数据库是否能正常运作,若没有问题则配置成功!

     private spring ctx = null;

    @Test
public void testDataSource() throws SQLException {
//检查spring配置
ctx = new ClassPathXmlApplicationContext("spring.xml");
// System.out.println(ctx);
//检查数据库连接
DataSource dataSource = ctx.getBean(DataSource.class); // System.out.println(dataSource.getConnection().toString());
//检查hibernate配置
SessionFactory sessionFactory = ctx.getBean(SessionFactory.class);
System.out.println(sessionFactory); Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
//测试数据库
Users user = new Users ("jianlam", "123456");
session.save(user);
tx.commit();
session.close(); }

如果你的mysql有问题或对于MVC模式下所需的包分类有所困惑可参考:这里

这是我找到的最好的配置文件之一,本来今天想整一个好一点的到网上。因为网上很多都是很糟糕的,各种问题。楼主这个好,每个配置都说的很清楚,终于完整的知道这些东西,是什么用的了。可惜不是maven的。我用的maven。对了,楼主有个类没写出来,一般新手要调试半天,我发出来 import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Users {
@ID
@column(name = "id", unique = false, nullable = false)
private int id;
private String name;
private String password;

public Users(String name, String password) {
super();
this.name = name;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}
再次感谢!

Spring + Spring MVC+Hibernate框架整合详细配置的更多相关文章

  1. SSH(Spring SpringMVC Hibernate)框架整合

    项目说明: 使用SSH(Spring SpringMVC Hibernate)框架整合添加部门功能 项目结构   1.导入依赖jar包 <!--单测--> <dependency&g ...

  2. 转载 Spring、Spring MVC、MyBatis整合文件配置详解

    Spring.Spring MVC.MyBatis整合文件配置详解   使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...

  3. struts2 + spring + mybatis 框架整合详细介绍

    struts2 + spring + mybatis  框架整合详细介绍 参考地址: https://blog.csdn.net/qq_22028771/article/details/5149898 ...

  4. Spring MVC、MyBatis整合文件配置详解

    Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...

  5. Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例

    Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例 转自:通过注解的方式集成Spring 4 MVC+Hibernate 4+MySQL+Maven,开发项目样例 ...

  6. Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用

    Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...

  7. Spring+SpringMVC+MyBatis+Maven框架整合

    本文记录了Spring+SpringMVC+MyBatis+Maven框架整合的记录,主要记录以下几点 一.Maven需要引入的jar包 二.Spring与SpringMVC的配置分离 三.Sprin ...

  8. ssm三大框架整合基本配置

    ssm三大框架整合基本配置 maven目录结构 数据库脚本mysql create database maven; use maven ; -- --------------------------- ...

  9. Struts2+Spring+Hibernate框架整合总结详细教程

    一.SSH三大框架知识总结 Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与S ...

随机推荐

  1. 大话设计模式C++版——工厂方法模式

    工厂方法模式是以简单工厂模式为基础的,如果未了解简单工厂模式的同学可先浏览<大话设计模式C++版——简单工厂模式>.在简单工厂模式中,提到过简单工厂模式的缺陷,即违背了开发—封闭原则,其主 ...

  2. [麦先生]TP3.2之微信开发那点事[基础篇](网页授权开发之小Demo)

    用户à 点击按钮进入授权页面并确认授权à 服务器返回code给开发者à 利用code,APPID,APPsecret组合数据请求API获取access_token和openidà 利用access_t ...

  3. PHP之图像处理

    PHP中提供了一些对图像进行编辑处理的函数,其中最为典型的应用为随机图形验证码.图片水印以及数据统计中饼状图和柱状图的生成等 PHP中有的图形函数可以直接使用,但多数需要在安装了GD2函数库后才能使用 ...

  4. UVa11549计算器谜题[floyd判圈]

    题意: 有个老式计算器,每次只能记住一个数字的前n位.现在输入一个整数k,然后反复平方,一直做下去,能得到的最大数是多少.例如,n=1,k=6,那么一次显示:6,3,9,1... 白书上的题 set, ...

  5. 服务器发布tomcat的WEB项目供外网访问

    1.首先你要确定你有一个外网ip地址.如果你分配到的是一个局域网IP地址需要经过一系列的转换为外网ip地址,然后继续下面操作. 2.拿到外网IP地址,进行tomcat的server.xml文件的配置. ...

  6. 转: Git远程操作详解 (阮一峰)

    说明: 通过这个说明终于把远程操作给搞明白了. http://www.ruanyifeng.com/blog/2014/06/git_remote.html

  7. HTML 学习笔记 JavaScript(事件)

    事件流: 事件流: 描述的是从页面中接收事件的顺序 也可以理解为事件在页面中传播的顺序: 事件: 就是用户或浏览器自身执行的某种动作 例如 click(点击) load(加载) mouseover(鼠 ...

  8. Javascript备忘复习笔记1

    一.字符串操作 1.大小写 var s = "hello"; undefined g = s.toUpperCase(); "HELLO" g; "H ...

  9. 启动Eclipse后卡在 android sdk content loader 的解决办法

    Make sure that eclipse is not active. If it is active kill eclipse from the processes tab of the tas ...

  10. IIS 伪静态配置(安装ISAPI_Rewrite配置)

    第一:首先到官方网站下载ISAPI_Rewrite 我的机子是32位的就下32位免费版的,链接地址如下: http://www.helicontech.com/download/isapi_rewri ...