来源于: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. Linux软件安装-yum安装

    虽然RPM包安装软件很方便.快捷,但是还是需要现有安装包才能安装.为了更为方便的安装软件,发展出了利用网络自动安装的方式--yum安装. 使用yum安装的前提是机器可以上网. 1.配置yum源 在/e ...

  2. container的生命周期

    Container启动过程主要经历三个阶段:资源本地化.启动并运行container.资源回收,其中,资源本地化指创建container工作目录,从HDFS下载运行container所需的各种资源(j ...

  3. 关于TP3.2微信开发那点事(基础篇)

    许久没有为博客更新内容,今天我将过去一周做的微信服务号的相关心得体会在此分享,具体如何申请成为服务号的相关流程文档都有,可根据要求完成: 开发第一步:开发前配置: AppID-->微信号的&qu ...

  4. 高级c++头文件bits/stdc++.h

    用这种方法声明头文件只需两行代码 #include<bits/stdc++.h> using namespace std; 这个头文件包含以下等等C++中包含的所有头文件: #includ ...

  5. python数字图像处理(9):直方图与均衡化

    在图像处理中,直方图是非常重要,也是非常有用的一个处理要素. 在skimage库中对直方图的处理,是放在exposure这个模块中. 1.计算直方图 函数:skimage.exposure.histo ...

  6. android代码优化----ListView中自定义adapter的封装(ListView的模板写法)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  7. openjudge8469特殊密码锁[贪心]

    描述 有一种特殊的二进制密码锁,由n个相连的按钮组成(n<30),按钮有凹/凸两种状态,用手按按钮会改变其状态. 然而让人头疼的是,当你按一个按钮时,跟它相邻的两个按钮状态也会反转.当然,如果你 ...

  8. Java面向对象之多态

    多态:具有表现多种形态的能力的特征(同一个实现接口,使用不同的实例而执行不同的操作) 实现多态的优点:为了方便统一调用! 实现多态的三种方式! 1:子类到父类的转换: 例: Dog dog=new D ...

  9. [No00000F]Excel快捷键大全 Excel2013/2010/2007/2003常用快捷键大全

    一个软件最大的用处是提高工作效率,衡量一个软件的好坏,除了是否出名之外,最主就是能否让一个新手更快的学会这个软件和提高工作速度.就拿Excel表格来说吧,平常办公中我们经常会用它来制作表格,统计数据或 ...

  10. uva131 The Psychic Poker Player

    The Psychic Poker Player Time Limit: 3000MS     64bit IO Format: %lld & %llu Description In 5-ca ...