Spring + Spring MVC+Hibernate框架整合详细配置
来源于: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包
- Spring的jar包
- Hibernate(附地址)的jar包
- 第三方jar包(日志包)
- 数据库的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(事物) -
<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 -
这里用到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> -
需要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> -
<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> -
<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框架整合详细配置的更多相关文章
- SSH(Spring SpringMVC Hibernate)框架整合
项目说明: 使用SSH(Spring SpringMVC Hibernate)框架整合添加部门功能 项目结构 1.导入依赖jar包 <!--单测--> <dependency&g ...
- 转载 Spring、Spring MVC、MyBatis整合文件配置详解
Spring.Spring MVC.MyBatis整合文件配置详解 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...
- struts2 + spring + mybatis 框架整合详细介绍
struts2 + spring + mybatis 框架整合详细介绍 参考地址: https://blog.csdn.net/qq_22028771/article/details/5149898 ...
- Spring MVC、MyBatis整合文件配置详解
Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...
- Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例
Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例 转自:通过注解的方式集成Spring 4 MVC+Hibernate 4+MySQL+Maven,开发项目样例 ...
- Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用
Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...
- Spring+SpringMVC+MyBatis+Maven框架整合
本文记录了Spring+SpringMVC+MyBatis+Maven框架整合的记录,主要记录以下几点 一.Maven需要引入的jar包 二.Spring与SpringMVC的配置分离 三.Sprin ...
- ssm三大框架整合基本配置
ssm三大框架整合基本配置 maven目录结构 数据库脚本mysql create database maven; use maven ; -- --------------------------- ...
- Struts2+Spring+Hibernate框架整合总结详细教程
一.SSH三大框架知识总结 Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与S ...
随机推荐
- Solrj和Solr DIH索引效率对比分析
测试软件环境: 1.16G windows7 x64 32core cpu . 2.jdk 1.7 tomcat 6.x solr 4.8 数据库软件环境: 1.16G windows7 x64 ...
- poj 2278 DNASequnce AC自动机
地址:http://poj.org/problem?id=2778 题目: DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 转载:JavaSE之反射
该文章转载自:http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html Java反射详解 本篇文章依旧采用小例子来说明,因为我 ...
- 第二章《深入C#数据类型》项目经理评分
一:创建MyOffices项目,创建UserInfo类,用来存储员工 工号,姓名,年龄,评价,年度得分 二:创建查看评分窗体(frmShow),添加定义员工数组,将员工数据绑定到frmShow窗体的L ...
- 微软极品工具箱-Sysinternals Suite
工具包由来 Sysinternals Suite是微软发布的一套非常强大的免费工具程序集,一共包括74个windows工具.Sysinternals是Winternals公司提供的免费工具,Winte ...
- u-boot移植初步尝试-tiny4412
获取u-boot源代码 在u-boot官方网站下载uboot源码.ftp://ftp.denx.de/pub/u-boot/ 因为是第一次移植uboot,所以这里选的版本是 u-boot-2013.0 ...
- windows下 git配置ssh
1. 打开 git bash 2. cd ~/.ssh 3. ssh-genkey (一致next), 会生成 id_rsa (密钥); id_rsa.pub(公钥) 4. 用记事本打开 id_rs ...
- linux下内网端口转发工具:linux版lcx [实现远程内网维护]
这个工具以前使用的初衷是内网渗透,需要将内网ssh端口转发到外网服务器上.但这个工具同样适用于运维工程师进行远程内网维护. 当然这一切的前提是内网可以访问外网,检测方法当然就是直接ping 一个外网I ...
- span 与p 的区别,以及内联元素的作用
1.一般标签都有语义,p标签是指一个段落,而且默认是一个块级元素,span是一个行内元素的代表,没有什么意思,一般可以放文字等行内元素,例如<p>这是一段简介内容这是一段 <span ...
- Oracle job procedure 存储过程定时任务
Oracle job procedure 存储过程定时任务 oracle job有定时执行的功能,可以在指定的时间点或每天的某个时间点自行执行任务. 一.查询系统中的job,可以查询视图 --相关视图 ...