配置项目的前提下你应该配置好你的开发环境

1新建hibernate.cfg.xml文件,放在src目录里

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库连接的配置信息,驱动类、数据库连接地址、用户名、密码 -->
<property name="connection.driver.class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mydb1</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property> <!-- hibernate 所使用的数据库方言 ,配置不正确时可能会出现错误(不正确的时候建议从网上搜你数据的所有方言,然后一个一个尝试,数据库方言不会很多的)-->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 执行操作时是否在控制台打印 SQL -->
<property name="show_sql">true</property>
<!-- 是否对 SQL 进行格式化 --><!--就是SQL语句分行显示,让我们读起来更加舒服-->
<property name="format_sql">true</property> <!-- 指定自动生成数据表的策略 --><!-- 当我们运行程序时Hibernate程序会帮我们自动的在数据库里面创建表-->
<property name="hbm2ddl.auto">update</property> <!-- 指定关联的 .hbm.xml 文件,让持久化对象和数据库关联起来 -->
<mapping resource="com/jeremy/hibernate/helloworld/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>

2新建持久化对象,

package com.jeremy.hibernate.helloworld;

import java.sql.Date;

public class News {
private Integer id;//持久化对象一般都提供ID,因为就是的数据库的primarykey,指定数据的唯一性
private String title;
private String author;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
   public News() {//而且持久化对象也一定要有一个无参的构造器,方便Hibernate调用

    }
 public News(String title, String author, Date date) {
super();
this.title = title;
this.author = author;
this.date = date;
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]";
}

3新建对象关系映射文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2014-9-27 21:57:33 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.jeremy.hibernate.helloworld.News" table="NEWS"><!--类和表关联起来-->
<id name="id" type="java.lang.Integer">
<column name="ID" />
<!--生成主键的方式-->
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<property name="author" type="java.lang.String">
<column name="AUTHOR" />
</property>
<property name="date" type="java.sql.Date">
<column name="DATE" />
</property>
</class>
</hibernate-mapping>

4建立测试类:

package com.jeremy.hibernate.helloworld;

import java.sql.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test; public class HibernateTest { @Test
public void test() { System.out.println("test..."); //1. 创建一个 SessionFactory 对象
SessionFactory sessionFactory = null; //1). 创建 Configuration 对象: 对应 hibernate 的基本配置信息和 对象关系映射信息
Configuration configuration = new Configuration().configure(); //4.0 之前这样创建
// sessionFactory = configuration.buildSessionFactory(); //2). 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象
//hibernate 的任何配置和服务都需要在该对象中注册后才能有效.
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry(); //3).
sessionFactory = configuration.buildSessionFactory(serviceRegistry); //2. 创建一个 Session 对象
Session session = sessionFactory.openSession(); //3. 开启事务
Transaction transaction = session.beginTransaction(); //4. 执行保存操作
News news = new News("Java12345", "ATGUIGU", new Date(new java.util.Date().getTime()));
session.save(news); //5. 提交事务
transaction.commit(); //6. 关闭 Session
session.close(); //7. 关闭 SessionFactory 对象
sessionFactory.close();
} }

配置Hibernate的流程的更多相关文章

  1. Hibernate工作流程

    Hibernate创建步骤 (五大核心接口:Configuration/SessionFactory/Session/Transaction/Query) 1.新建工程,导入需要的jar包. 2.利用 ...

  2. mybatis与hibernate运行流程比较

    hibernate长时间没用,感觉生疏了,正好借这篇文章整合下知识,顺便复习比较下两种框架. 概述: Mybatis和hibernate不同,它不完全是一个ORM框架,因为MyBatis需要程序员自己 ...

  3. Hibernate 开发流程

    Hibernate内部分装的技术:JDBC(Java Data Base Connectivity), JTA(Java Transaction API) , JNDI(Java Naming and ...

  4. 配置Hibernate二级缓存时,不能初始化RegionFactory的解决办法

    配置Hibernate 二级缓存时,出现以下bug提示: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder&quo ...

  5. 菜鸟学习Hibernate——配置Hibernate环境

    一.概念. Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库.既然学习Hibernate那么第 ...

  6. 配置Hibernate二级缓存步骤

    配置Hibernate二级缓存步骤: 加入二级缓存的jar包及配置文件 jar包位置:hibernate-release-4.1.8.Final\lib\optional\ehcache下所有jar包 ...

  7. *IntelliJ IDEA配置Hibernate

    为IntelliJ IDEA安装Hibernate插件

  8. 第一次使用并配置Hibernate

    1. 环境配置 1.1 hiberante环境配置 hibernate可实现面向对象的数据存储.hibernate的官网:http://hibernate.org/ 官网上选择hibernate OR ...

  9. SSH整合,applicationContext.xml中配置hibernate映射文件问题

    今天在applicationContext.xml中配置sessionFactory时遇到了各种头疼的问题,现在总结一下: 1.<property name="mappingDirec ...

随机推荐

  1. java线程同步方法,方法块差别

    先说同步方法.它究竟是锁定的当前对象,还是当前类 代码块1 package com.ssss; public class Thread1 implements Runnable { //public ...

  2. THREADSPOOL

    STPStartInfo stp = new STPStartInfo();//线程详细配置参数 stp.CallToPostExecute = CallToPostExecute.Always;// ...

  3. CSS3边框圆角知识

    <div class="item" data-brief="整圆"> <div class="border-radius" ...

  4. hadoop3: mkdir: cannot create directory `/usr/local/hadoop/bin/../logs’: Permission denied

    1.hadoop3: mkdir: cannot create directory `/usr/local/hadoop/bin/../logs': Permission denied把所有Datan ...

  5. 这不是bug,而是语言特性

    分析编程语言缺陷的一种方法是把所有的缺陷归于3类:不该做的做了,该做的没做,该做但做得不合适. 在使用switch case时,如果使用缺省的 fall through,请一定在旁边注释,因为97%的 ...

  6. 彻底征服 Spring AOP 之 理论篇

    基本知识 其实, 接触了这么久的 AOP, 我感觉, AOP 给人难以理解的一个关键点是它的概念比较多, 而且坑爹的是, 这些概念经过了中文翻译后, 变得面目全非, 相同的一个术语, 在不同的翻译下, ...

  7. [uboot]uboot如何引导系统

    转自:http://bbs.elecfans.com/jishu_455028_1_1.html 如6410的bootcmd和bootargs默认存在于uboot1.1.6/include/confi ...

  8. sublime text 3中配置golang开发环境

    1:首先下载 Go源码 https://golang.org/dl/  [根据不同的环境选择] 2:新建文件项目文件夹 存放  D:/Go_project 分别建立  bin  src  pkg  子 ...

  9. Linux 串口编程

    今天对应用层串口编程进行了验证.程序来源于以下参考链接,自己进行了一些注释和更改,记录于此. Tony Liu, 2016-6-17, Shenzhen 参考链接 https://www.ibm.co ...

  10. OSG 中 相交測试 模块 工作流程及原理

    主要涉及三个类: 1. osgUtil::PolytopeIntersector // 详细不同算法实现类 2. osgUtil::IntersectionVisitor //用来遍历节点树的每一个节 ...