1.在myeclipse以下创建一个javaproject或者webproject,我创建的时webproject,用的myeclipse2013

2.导入spring的依赖包

3.导入hibernate的依赖包

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvajkwMzgyOTE4Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

4.在src文件夹以下配置hibernate的核心文件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>

<!-- Database connection settings -->

        <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>

        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>

        <property name="connection.username">root</property>

        <property name="connection.password">root</property>

<!-- JDBC connection pool (use the built-in) -->

        <property name="connection.pool_size">1</property>

<!-- SQL dialect -->

        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->

        <property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache  -->

        <!-- <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> -->

<!-- Echo all executed SQL to stdout -->

        <property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->

        <property name="hbm2ddl.auto">update</property>

<mapping resource="org/hibernate/domain/Event.hbm.xml"/>

        <!-- annotation的配置方式 -->

        <mapping class="org.hibernate.domain.Teacher"/>

</session-factory>

</hibernate-configuration>

5.在WEB-INFO文件夹以下配置spring的核心配置文件applicationContext

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans.xsd">

 <!-- <bean id="..." class="..."> collaborators and configuration for this

  bean go here </bean> <bean id="..." class="..."> collaborators and configuration


  for this bean go here </bean> more bean definitions go here -->

 <!-- 配置sessionFactory -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

  <property name="configLocation">

   <value>classpath:hibernate.cfg.xml</value>

  </property>

 </bean>

<bean id="DaoImp" class="com.persistent.DaoImp">

  <property name="sessionFactory" ref="sessionFactory"></property>

 </bean>

</beans>

6.编写持久化类

package org.hibernate.domain;

import javax.persistence.Entity;

import javax.persistence.Id;

//定义一个实体类

@Entity

public class Teacher {

private int id;//id

 private String title;//职场

 private String name;//姓名

 //定义一个主键

 @Id

 public int getId() {

  return id;

 }

 public void setId(int id) {

  this.id = id;

 }

 public String getTitle() {

  return title;

 }

 public void setTitle(String title) {

  this.title = title;

 }

 public String getName() {

  return name;

 }

 public void setName(String name) {

  this.name = name;

 }

 

}

7.编写保存之久话的类和方法

package com.persistent;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.domain.Teacher;

public class DaoImp {

private SessionFactory sessionFactory;

 

 public SessionFactory getSessionFactory() {

  return sessionFactory;

 }

public void setSessionFactory(SessionFactory sessionFactory) {

  this.sessionFactory = sessionFactory;

 }

public void save(){

  Session session=sessionFactory.getCurrentSession();

  session.beginTransaction();

        Teacher teacher = new Teacher();

        teacher.setId(6);

        teacher.setName("hibernate!!");

        teacher.setTitle("we will");

        session.save(teacher);//利用annotation进行插入操作

        session.getTransaction().commit();

 }

}

8.在hibernate.cfg.xml文件里配置

<!-- annotation的配置方式 -->

        <mapping class="org.hibernate.domain.Teacher"/>

9.进行測试

package com.demo.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.persistent.DaoImp;

public class TestMain2 {

/**

  * @param args

  */

 public static void main(String[] args) {

  // TODO Auto-generated method stub

  

      

  

 

  

  ApplicationContext context =

       new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");

        /*Hello hello=(Hello) context.getBean("hello");

        hello.display();*/

  DaoImp di = (DaoImp)context.getBean("DaoImp");

  di.save();

  

 }

}

10.能够看查看数据库,已经有数据了,控制台也打印出了sql语句

spring4和hibernate4.0.0的整合的更多相关文章

  1. SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常见问题总结

    下载地址: http://pan.baidu.com/s/1qWDinyk 一 开发环境 1.动态web工程 2.部分依赖 hibernate-release-4.1.0.Final.zip hibe ...

  2. SpringBoot2.0之四 简单整合MyBatis

    从最开始的SSH(Struts+Spring+Hibernate),到后来的SMM(SpringMVC+Spring+MyBatis),到目前的S(SpringBoot),随着框架的不断更新换代,也为 ...

  3. SpringBoot2.0+Shiro+JWT 整合

    SpringBoot2.0+Shiro+JWT 整合 JSON Web Token(JWT)是一个非常轻巧的规范.这个规范允许我们使用 JWT 在用户和服务器之间传递安全可靠的信息. 我们利用一定的编 ...

  4. SpringBoot2.0之三 优雅整合Spring Data JPA

      在我们的实际开发的过程中,无论多复杂的业务逻辑到达持久层都回归到了"增删改查"的基本操作,可能会存在关联多张表的复杂sql,但是对于单表的"增删改查"也是不 ...

  5. Spring Boot 2.0 快速集成整合消息中间件 Kafka

    欢迎关注个人微信公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site ...

  6. Springboot(2.0.0.RELEASE)+spark(2.1.0)框架整合到jar包成功发布(原创)!!!

    一.前言 首先说明一下,这个框架的整合可能对大神来说十分容易,但是对我来说十分不易,踩了不少坑.虽然整合的时间不长,但是值得来纪念下!!!我个人开发工具比较喜欢IDEA,创建的springboot的j ...

  7. Struts2+Spring4.2+Hibernate4.3整合

    一.导包 antlr-2.7.7.jarasm-3.3.jarasm-commons-3.3.jarasm-tree-3.3.jarcom.springsource.com.mchange.v2.c3 ...

  8. Node.js 4.0.0:灵雀云和 OneAPM 的整合测试

    关于 Node.js 4.0.0 稳定版刚刚推出,备受期待,迫不及待地想用它写点东西:此外,要把 Demo 放到 Internet 上得有一个公网 IP ,看到灵雀云挺不错的而且提供域名解析,简直业界 ...

  9. springboot2.0和Druid整合配置数据源

    1. idea使用spring 初始化工具初始化springboot项目(要选中web) 下一步,下一步 2. 在pom.xml中,引入Druid连接池依赖: <dependency> & ...

  10. ShardingSphere-proxy-5.0.0企业级分库分表、读写分离、负载均衡、雪花算法、取模算法整合(八)

    一.简要说明 以下配置实现了: 1.分库分表 2.每一个分库的读写分离 3.读库负载均衡算法 4.雪花算法,生成唯一id 5.字段取模 二.配置项 # # Licensed to the Apache ...

随机推荐

  1. Windows 10家庭版也能共享打印机(中)解除Guest账户网络登录限制,实现局域网共享

    由于Windows系统默认是禁止Guest账户从网络登录的.我们须要解除这个限制.首先想到的是用组策略编辑器gpedit.msc. 可是Windows 10家庭版没有组策略编辑器,我们先尝试用U盘把W ...

  2. 深度学习实战篇-基于RNN的中文分词探索

    深度学习实战篇-基于RNN的中文分词探索 近年来,深度学习在人工智能的多个领域取得了显著成绩.微软使用的152层深度神经网络在ImageNet的比赛上斩获多项第一,同时在图像识别中超过了人类的识别水平 ...

  3. 样条函数(spline function)—— 分段多项式函数(piecewise polynomial function)

    1. 分段多项式函数 样条函数是某种意义上的分段函数. Spline (mathematics) - Wikipedia 最简单的样条函数是一种分段多项式函数(piecewise polynomial ...

  4. 10.TCPIP监听器

    给它做代理之后你这个端口是什么? 做一个代理. Local monitoring port:本地的监听端口.你要给谁做代理,那么给它做代理之后,你这个代理的端口是什么? 现在要给百度做一个代理. 能看 ...

  5. 用fiddler不能抓取https及证书无法导出

    本次说的不是首次安装fiddler 1.不管有没有安装成功,先查看有没有安装过证书,有的话删除,重新进行安装 打开fiddler,找到Tools-HTTPS-Athons-Open windows C ...

  6. 最详细的CentOS 6与7对比(一):常见设置对比

    本主题将从3个角度进行对比 常见设置(CentOS 6 vs CentOS 7) 服务管理(Sysvinit vs Upstart vs Systemd) 性能测试(cpu/mem/io/oltp) ...

  7. 抽象类(abrstract class)与接口(interface)有何异同

    抽象类:如果一个类中包含抽象方法(用abstract修饰的方法),那么这个类就是抽象类 接口:是指一个方法的集合,接口中的所有方法都没有方法体 相同点: 1)都不能被实例化 2)接口的实现类或抽象类的 ...

  8. rsync文件备份同步

    1.rsync有两种认证协议: ssh认证协议 rsync server端不需要启动daemon进程,所以不用配置/etc/rsyncd.conf,只需要获取远程host的用户名密码 例: rsync ...

  9. POJ 1654 乱搞题?

    题意: 从一个点出发,8个方向,给出每一步的方向,求出走过的路径形成的多边形的面积. 思路: 先普及一下向量叉乘.. (摘自度娘) 也就是x1y2-x2y1. 那这不就好说了嘛. 一个经过原点的闭合多 ...

  10. Java程序连接各种数据库的driver和url形式

    1.Oracle数据库 Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String url = & ...