【Hibernate】将对象保存到数据库表中


  上一篇文章简单介绍下了Hibernate的基本原理,这篇文章主要介绍下Hibernate的使用,将对象保存到数据库的

表中的流程。


一、搭建hibernate环境

    1.将hibernate的jar包加入到工作环境中。

    2.编写Hibernate配置文件,命名为:hibernate.cfg.xml

     这里以User表为例


<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.show_sql">true</property>
		
		<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/> 
	</session-factory>
</hibernate-configuration>



二、创建User实体类

     get,set方法省略不写啦


public class User {

	private String id;
	private String name;
	private String password;
	private Date createTime;
	private Date expireTime;

}



三、创建实体映射文件

      这里的实体关系映射采用配置文件的形式,还有一种是注解的形式,在这里先不做介绍。

<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.show_sql">true</property>
		
		<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/> 
	</session-factory>
</hibernate-configuration>



四、编写客户端,将User表中的数据写入到数据库中

     

package com.bjpowernode.hibernate;

import java.sql.Date;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

public class Client {
	public static void main(String[] args) {

		// 1.第一步:读取hiber.cfg.xml文件
		Configuration cfg = new Configuration().configure();
		// 2.第二步:建立sessionfactory
		SessionFactory factory = cfg.buildSessionFactory();

		// 3.第三步:取得session,
		Session session = null;
		try {
			session = factory.openSession();
			// 4.第四步:开启事务
			session.beginTransaction();

			// 获取当前日期
			java.sql.Date currentDate = new java.sql.Date(
					System.currentTimeMillis());
			// 实例化User类
			User user = new User();
			// 给User实体类赋值
			user.setCreateTime(currentDate);
			user.setExpireTime(currentDate);
			user.setName("123123");
			user.setPassword("123123");

			// 保存user对象
			session.save(user);
			// 提交事务
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			// 回滚事务
			session.getTransaction().rollback();
		} finally {
			if (session != null) {
				if (session.isOpen()) {
					// 关闭session
					session.close();
				}
			}
		}
	}
}

     上面介绍了下Hibernate是怎样存储数据的,Hibernate还有一个功能是自动在数据库中生成相对应的表,但是


数据库得由我们自己手动创建

         

/**
 * 将hbm生成ddl
 * @author Administrator
 *
 */
public class ExportDB {

	public static void main(String[] args) {
		
		//默认读取hibernate.cfg.xml文件
		Configuration cfg = new Configuration().configure();
		SchemaExport export = new SchemaExport(cfg);
		//在数据库中创建所对应的表,先读取配置文件的信息,逐一创建表
		export.create(true, true);
	}
}

     好啦,一个简单的实例介绍了下hibernate将对象写入 到数据库中的具体操作,下篇文章将介绍Hibernate的


映射关系!

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值