hibernate+spring的整合思路加实例(配图解)
首先框架整合我感觉最难的是jar包的引入。因为不同框架的jar容易产生冲突。如果能排除这个因素我想说整合框架还是相对比较容易的。
我整合的框架的一个思想就是:各司其职。因为每个框架处理的事务或者是层次是不一样的。
也就说我们可以这么想。Hibernate就是操纵数据库的是持久层的。而spring就是利用ioc的bean对类进行实例化化的。Spring就是一个容器。
基于这个思想我想到的就是最传统的思想。我写一个往数据库里面添加记录的实例。
所以我整合的步骤就是四步
第一步:搭建hibernate环境(包括引入hibernate的jar,包配置数据源,建立类和表的映射),为什么这么做。我觉得hibernate是最重要的。因为没有spring不影响我往数据里面添加记录。Spring仅仅是一个容器。
第二步:配置spring的环境(引入jar和写一个spring.XML的配置信息)
第三步:在spring里面注册hibernate。
第四步:测试
那么我们按照这个步骤。(我的是spring3.2和hibernate3.2这里两个版本一起很少见。但是只要里面jar不冲突是一点问题没有)
首先我们引入所有jar包:这里我就顺便把spring和hibernate 的全引进去了。
接着就是写hibernate.cgf.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="foo">
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.url">
jdbc:mysql://127.0.0.1:3306/test
</property>
<property name="connection.username">root</property>
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hbm2ddl.auto">update</property>//上面都是配置数据源,和本级数据库建立连接的。
<mapping resource="com/fish/dao/Person.hbm.xml" />//把映射表xml往这里注册。
</session-factory>
</hibernate-configuration>
那么我们写Person.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.fish.dao">
<class name="Person">//person映射类类名
<id name="id">
<generator class="native" />
</id>
<property name="name"></property>
<property name="passwrod"></property>
</class>
</hibernate-mapping>
当然我们写对应的person映射类类
package com.fish.dao;
publicclass Person {
String name;
String passwrod;
Integer id;
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
public String getPasswrod() {
returnpasswrod;
}
publicvoid setPasswrod(String passwrod) {
this.passwrod = passwrod;
}
public Integer getId() {
returnid;
}
publicvoid setId(Integer id) {
this.id = id;
}
}
上面是对hibernate搭建。
接着我们来配置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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="person" class="com.fish.dao.Person">
<property name="name" value="tom"></property>
<property name="passwrod" value="330127"></property>
<bean id="sessionfactroy"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="hibernate.cfg.xml"></property> //这个bean是spring内部的一个类 // LocalSessionFactoryBean这个类实例化我们可以得到sessionfactory。该类中有个属性configLocation通过这个属性我们就可以hibernate.cfg.xml建立联系了。
</bean>
</beans>
下面我们来一个测试类。
package com.fish.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
publicclass Test {
publicstaticvoid main(String[] args) {
// Configuration configuration=new Configuration();
// configuration.configure("hibernate.cfg.xml");
//
// SessionFactory factory=configuration.buildSessionFactory();
// Session session=factory.openSession();
//
// Person person=new Person();
// person.setName("yaku");
// person.setPasswrod("123456");
// Transaction transaction=session.beginTransaction();
// session.save(person);
// transaction.commit();
// session.close();
上面注释的是我们没有加入spring对hibernate的测试。我们可以对比下面,看看spring给我做了什么;
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring.xml");
SessionFactory factory = (SessionFactory) context
.getBean("sessionfactroy"); //spring直接帮我们加载了hibernate.cgf.xml文件,让我们直接操作了sessionfactory。其实下面的事务管理我们也可以通过spring的来管理的。但是由于没写一个一个代理类所以就没写。
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Person person = (Person) context.getBean("person");
session.save(person);
transaction.commit();
session.close();
}
}
其实还有一种关联spring和hibernate的方法那就是将hibernate.cgx.XML放到spring来注册数据源,这样可以省去写hibernate.cgx.XML文件,但是这个个人感觉会给spring文件造成臃肿,所以我用的是分开的这种。
hibernate+spring的整合思路加实例(配图解)的更多相关文章
- struts2+hibernate+spring简单整合且java.sql.SQLException: No suitable driver 问题解决
最近上j2ee的课,老师要求整合struts2+hibernate+spring,我自己其实早早地有准备弄的,现在都第9个项目了,无奈自己的思路和头绪把自己带坑了,当然也是经验问题,其实只是用myec ...
- Hibernate + Spring (quartz) 整合懒(延迟)加载问题
开发项目的时候 在一个Job中执行了数据库操作, 用的是懒加载,但是如下错误 org.hibernate.LazyInitializationException: failed to lazily i ...
- spring boot整合redis多实例
最近项目中遇到需要连接两个redis实例的情况,于是就在spring boot原先的基础上修改了一点. 首先,添加所需的依赖 <dependency> <groupId>org ...
- 【转】pringMVC+Hibernate+Spring 简单的一个整合实例
ref:http://langgufu.iteye.com/blog/2088355 SpringMVC又一个漂亮的web框架,他与Struts2并驾齐驱,Struts出世早而占据了一定优势,我在博客 ...
- Spring与Hibernate、Mybatis整合
在Web项目中一般会把各个web框架结合在一起使用,比如spring+hibernate,spring+ibatis等,如此以来将其他的框架整合到spring中来,便有些少许的不便,当然spring已 ...
- Spring + mybatis整合方案总结 结合实例应用
Spring + mybatis整合实例应用 项目结构图 (Spring3.0.2 +mybatis3.0.4) 方案一: 通过配置文件整合Spring和mybatis 应用数据库 -- --数据库 ...
- Struts、Hibernate和Spring的整合
Spring整合Hibernate Spring以其开放性,能与大部分ORM框架良好的整合.这样Spring就能轻松地使用ORM. Spring提供了DAO支持,DA0组件是应用的持久层访问的重要组件 ...
- Struts2+Hibernate+Spring 整合示例
转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:SSH框架(Struts2+Spring+Hibernate)搭建整合详细步骤
在实际项目的开发中,为了充分利用各个框架的优点,通常都会把 Spring 与其他框架整合在一起使用. 整合就是将不同的框架放在一个项目中,共同使用它们的技术,发挥它们的优点,并形成互补.一般而言,在进 ...
随机推荐
- Shader程序中内置的状态变量
经常在着色器程序需要访问一些全局状态,像当前的 model view projection 矩阵,当前环境的颜色诸如此类. 内置的矩阵 UNITY_MATRIX_MVP:当前模型 视窗 投影矩阵 UN ...
- SQLite学习手册(目录)
链接地址:http://www.cnblogs.com/stephen-liu74/archive/2012/01/22/2328757.html 在实际的应用中,SQLite作为目前最为流行的开源嵌 ...
- 【转载】设备坐标(窗口/window)和逻辑坐标(视口/viewport)
一.预备知识 1.窗口是基于逻辑坐标的. 2.视口是基于设备坐标. 3.设备坐标是以像素为单位的,逻辑坐标是以.cm,m,mm,..... 4.系统最后一定要把逻辑坐标变为设备坐标. 5.设备坐标有3 ...
- Python每日一练(1):计算文件夹内各个文章中出现次数最多的单词
#coding:utf-8 import os,re path = 'test' files = os.listdir(path) def count_word(words): dic = {} ma ...
- Bootstrap 响应式瀑布流 (使用wookmark)
使用瀑布布局 官方 http://www.wookmark.com/jquery-plugin GitHub https://github.com/GBKS/Wookmark-jQuery (下载后 ...
- Mathematica
Mathematica是一款科学计算软件,很好地结合了数值和符号计算引擎.图形系统.编程语言.文本系统.和与其他应用程序的高级连接.很多功能在相应领域内处于世界领先地位,它也是使用最广泛的数学软件之一 ...
- 转;说说AngularJS中的$parse和$eval
说说AngularJS中的$parse和$eval AngularJS的初学者常常会对$parse和$eval两个内建服务感到有些困惑,今天我们就来说说AngularJS中的$parse和$eval. ...
- html文件中文在浏览器中显示乱码问题解决
利用浏览器打开html文件时,中文显示乱码,如下是原文件的内容 1 <html> 2 <head> 3 <title> ...
- protubuf在cocos2dx的应用安装
将protubuf放置在external文件夹 在vs工程里的luacocos2d里新建筛选器protubuf,将pb.c加入到该筛选器下. 将pb.c文件的属性里设置改文件编译为C文件. 将luas ...
- golang语法学习(一):变量,常量以及数据类型
学习一门新的语言肯定是要从他的主要的语法開始,语法构成了整个程序设计的基础,从语法中我们也能够看到这门语言的一些特性.可是话说回来.语法这东西,不同的语言大同小异,所以这也对语法的记忆造成了一定的难度 ...