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. Hadoop-2.2.0中文文档——Apache Hadoop 下一代 MapReduce (YARN)

    MapReduce在hadoop-0.23中已经经历了一次全然彻底地大修.就是如今我们叫的MapReduce 2.0 (MRv2) or YARN. MRv2的基本思想是把JobTracker分成两个 ...

  2. python 编写的经纬度坐标转换类

    # -*- coding: utf-8 -*- # /** # * 各地图API坐标系统比较与转换; # * WGS84坐标系:即地球坐标系,国际上通用的坐标系.设备一般包含GPS芯片或者北斗芯片获取 ...

  3. 在Mac OSX上安装ffmpeg && ffmpeg命令行将h264封装为mp4

    ffmpeg功能强大,可以通过命令行来对音视频进行处理.为了使用其功能,我在Mac上对其进行了安装. 我的Mac OS X 系统版本:OS X Yosemite, 10.10.14 关于ffmpeg在 ...

  4. IO流读取文件内容时,出现空格的问题(未找到原因)

    import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOExceptio ...

  5. 外观模式(Facade)C++实现

    外观模式 意图: 为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一系统更加容易使用. 适用性: 1.在设计初期阶段,应该要有意识的将不同的两个层分离,比如经典的三层架 ...

  6. 洛谷P1208 [USACO1.3]混合牛奶 Mixing Milk(贪心)

    题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是 ...

  7. mybatis 高级映射和spring整合之与Spring整合(6)

    mybatis 高级映射和spring整合之mybatis与Spring整合 3.0 mybatis和spring整合(掌握) 3.1 整合思路 需求spring通过单例方式管理SqlSessionF ...

  8. postgreSQL格式化时间的函数详解

    数据类型格式化函数:    PostgreSQL格式化函数提供一套有效的工具用于把各种数据类型(日期/时间.integer.floating point和numeric)转换成格式化的字符串以及反过来 ...

  9. 解决strip: Unable to recognise the format of the input file问题

    前言   在编译xilinx的uboot的时候出现了一个问题,始终报错:“strip: Unable to recognise the format of the input file `gen_et ...

  10. Sublime Text 3 Build 3047 安装/插件安装/C编程环境配置

    Sublime Text 3 Build 3047 安装 参考:http://sublimetext.iaixue.com/forum.php?mod=viewthread&tid=29 插件 ...