1、工程目录结构如下

2、引入需要的jar包,如上图。

3、创建持久化类User对应数据库中的user表

package com.hibernate.配置文件.pojo;

import java.sql.Date;

public class User {
private Integer id; private String username; private String password; private Date update_time; public User() {
super();
} public User( String username, String password, Date update_time) {
super();
this.username = username;
this.password = password;
this.update_time = update_time;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public Date getUpdate_time() {
return update_time;
} public void setUpdate_time(Date update_time) {
this.update_time = update_time;
} }

4、创建持久化类对应的配置文件User.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.hibernate.配置文件.pojo">
<class name="User" table="user" dynamic-insert="true">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
<generator class="native" />
</id> <property name="username" type="java.lang.String" column="username"/>
<property name="password" type="java.lang.String" column="password"/>
<property name="update_time" type="date">
<column name="update_time" />
</property>
</class>
</hibernate-mapping>

5、创建hibernate的配置文件hibernate.cfg.xml

<?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.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1/hibernate</property> <!-- 配置hibernate的基本信息 -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 执行操作时是否在控制台打印 SQL -->
<property name="show_sql">true</property> <!-- 是否对 SQL 进行格式化 -->
<property name="format_sql">true</property> <!-- 指定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property> <!-- 指定关联的.hbm.xml文件 -->
<mapping resource="com/hibernate/配置文件/pojo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>

6、创建SessionFactoryUtil,单例模式,用于生成SessionFactory对象

package com.hibernate.配置文件.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder; public class SessionFactoryUtil {
private SessionFactoryUtil(){}
private static SessionFactory sessionFactory = null; public static SessionFactory getSessionFactory() { if(sessionFactory == null){
/**
* 1、 Configuration 类负责管理 Hibernate 的配置信息。包括如下内容: Hibernate
* 运行的底层信息:数据库的URL、用户名、密码、JDBC驱动类,数据库Dialect,数据库连接池等(对应
* hibernate.cfg.xml 文件)。 持久化类与数据表的映射关系(*.hbm.xml 文件)
*/
Configuration configeration = new Configuration().configure(); /**
* 2、 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象 hibernate
* 的任何配置和服务都需要在该对象中注册后才能有效.
*/
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configeration.getProperties())
.buildServiceRegistry();
/**
* 3、创建SessionFactory对象
*/
sessionFactory = configeration.buildSessionFactory(serviceRegistry);
}
return sessionFactory; }
}

7、测试代码

package com.hibernate.配置文件.service.impl;

import java.sql.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction; import com.hibernate.配置文件.pojo.User;
import com.hibernate.配置文件.service.HibernateService;
import com.hibernate.配置文件.util.SessionFactoryUtil; public class HibernateServiceImpl implements HibernateService { public void hibernateTest(){
//1、创建SessionFactory对象
SessionFactory sessionFactory = SessionFactoryUtil.getSessionFactory(); //2、创建session
Session session = sessionFactory.openSession(); //3、开启事务
Transaction tran = session.beginTransaction(); //4、执行持久化操作
User user = new User("lvyf","123456",new Date(new java.util.Date().getTime()));
session.save(user); //5、提交事务
tran.commit(); //6、关闭session
session.close(); //7、关闭sessionFactory
sessionFactory.close();
} public static void main(String[] args) {
HibernateServiceImpl hs = new HibernateServiceImpl(); hs.hibernateTest();
}
}

hibernate操作数据库例子的更多相关文章

  1. hibernate操作数据库总结

    这篇文章用于总结hibernate操作数据库的各种方法 一.query方式 1.hibernate使用原生态的sql语句执行数据库查询 有些时候有些开发人员总觉得用hql语句不踏实,程序出现了错误,就 ...

  2. Java_Web三大框架之Hibernate操作数据库(三)

    使用Hibernate操作数据库需要七个步骤: (1)读取并解析配置文件 Configuration conf = newConfiguration().configure(); (2)读取并解析映射 ...

  3. hibernate操作数据库总结(转)

    一.query方式 1.hibernate使用原生态的sql语句执行数据库查询 有些时候有些开发人员总觉得用hql语句不踏实,程序出现了错误,就猜测因为不是原生态的sql语句,数据库不支持,因此情愿选 ...

  4. Hibernate操作数据库的回调机制--Callback

     1:一般情况下,在使用Hibernate Session存取数据库的代码中,基本上大部分是相同的,如下两个方法所示, //查询Teacher操作 ublic Teacher getTeacher ...

  5. Hibernate 操作数据库

    com.tao.pojo实体类 package com.tao.pojo; public class User { private int id; private String name; priva ...

  6. 转 使用Hibernate操作数据库时报:No CurrentSessionContext configured! 异常

    没有currentSession配置错误,即在我们使用currentSession的时候要在hibernate.cfg.xml中进行相关的事务配置:1.本地事务<property name=&q ...

  7. hibernate操作数据库时报错解决方式

    java.sql.SQLException: Parameter index out of range (28 > number of parameters, which is 27). 这个说 ...

  8. Spring MVC基础知识整理➣Spring+SpringMVC+Hibernate整合操作数据库

    概述 Hibernate是一款优秀的ORM框架,能够连接并操作数据库,包括保存和修改数据.Spring MVC是Java的web框架,能够将Hibernate集成进去,完成数据的CRUD.Hibern ...

  9. 5 -- Hibernate的基本用法 --2 2 Hibernate的数据库操作

    在所有的ORM框架中有一个非常重要的媒介 : PO(持久化对象:Persistent Object).持久化对象的作用是完成持久化操作,简单地说,通过该对象可对数据执行增.删.改的操作 ------ ...

随机推荐

  1. Mono Json序列化和Windows 下的差别

    在Window下DataContractJsonSerializer 的序列化的时候 只要属性具有Get访问器就可以序列化为string 但是Mono下要想序列话 那么属性必须具有Get 和Set才能 ...

  2. 深入探索Android中的Handler

    一.概述 1. 什么是Handler Handler是Android消息机制的上层接口,它为我们封装了许多底层的细节,让我们能够很方便的使用底层的消息机制.Handler的最常见应用场景之一便是通过H ...

  3. BASE64Decoder 编码(sun.jar)

    Base64 是网络上最常见的用于传输8Bit 字节代码的编码方式之一,大家可以查看RFC2045 -RFC2049 ,上面有MIME 的详细规范.  Base64 要求把每三个8Bit 的字节转换为

  4. unity3d NGUI制作角色展示框

    最近在搞赛车漂移,所以一直没有更新博客 现在已经实现圈数检测.复位点检测.反向检测等功能 本来准备写成三篇文章的,太忙了,等过段时间不忙了在写吧 今天有朋友问我3D角色怎么给他固定在一个框里面 这个功 ...

  5. SQL Server 收缩日志

    一. SQL Server 2008 收缩日志 (1) 使用SQL管理器收缩日志 第一步执行如下命令 ALTER DATABASE platform SET RECOVERY SIMPLE GO 第二 ...

  6. 重构笔记---MEF框架(上)

    概述 这篇文章的目的是简要分析对比MAF和MEF,并详细举出MEF设计中的细节和扩展上的细节,达到让读者能实际操作的目的.其中,MAF的设计会附上我的代码,其实就是官方的代码我自己手动联系了一遍,但还 ...

  7. SpringMVC实战

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...

  8. 【转】从Go、Swift语言出发

    Google于2009年第一次提出了Go的构思,Facebook在去年春天引入了Hack,随后不久Apple也发布了其Swift语言. 在战争中,胜利者写历史书:在科技中,赢的公司都在写编程语言.互联 ...

  9. __name__和__main的含义

    if __name__ == "__main__": main() This module represents the (otherwise anonymous) scope i ...

  10. 11.Android之常用对话框AlertDialog学习

    (1)首先我们写个简单的AlertDialog对话框,要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法,然后创建对话框可以设置对话框的属性,比如 ...