【Hibernate】(一)快速入门

Hibernate

       Hibernate核心内容是ORM(关系对象模型)。可以将对象自动的生成数据库中的信息,使得开发更加的面向对象。这样作为程序员就可以使用面向对象的思想来操作数据库,而不用关心繁琐的JDBC。所以,Hibernate处于三层架构中的D层(持久层)如下图:


【优点】

    1、面向对象一体化。面向对象的分析,到面向对象的设计,到面向对象的开发,使得面向对象更加一体化,使开发更加对象化。

    2、很好的移植性。Hibernate对持久层重新做了封装,那么所有持久层的代码就具有复用性,那么修改数据库时就可以直接修改了。

    3、轻量级框架。Hibernate只需要Java环境即可,不需要容器,测试方便,提高生产力。

    4、没有侵入性。支持透明持久化。Hibernate不需要继承任何类,不需要实现任何接口。

    5.使用Hibernate可以使我们采用对象化的思维操作关系型数据库。

【缺点】

    1、使用数据库特性的语句,将很难调优

    2、对大批量数据更新存在问题

    3、系统中存在大量的攻击查询功能

【入门小程序】

1.创建Java Project,项目结构如下:


2.引入jar

        Hibernate3.jar、数据库驱动、和一些第三方jar包,下载地址:          http://download.csdn.net/detail/u013036274/9763940

3.提供hibernate缺省配置文件hibernate.cfg.xml,完成基本的配置:

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<!-- 数据库驱动、url、用户名、密码 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first?characterEncoding=utf-8</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
		<!--关于MySQL的数据库语言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.show_sql">true</property>
		<!-- 根据resource的链接找到对应的User类和数据库的映射文件 -->
		<mapping resource="com/bjpowernode/hibernate/User.hbm.xml" />
	</session-factory>
</hibernate-configuration>

4.创建持久化类User.java

package com.bjpowernode.hibernate;
import java.util.Date;
public class User {

	private String id;

	private String name;

	private String password;

	private Date createTime;

	private Date expireTime;

	// get和set方法略
}

5.提供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>
	<class name="com.bjpowernode.hibernate.User">
		<id name="id">
			<generator class="uuid"/>
		</id>
		<property name="name"/>
		<property name="password"/>
		<property name="createTime"/>
		<property name="expireTime"/>
	</class>
</hibernate-mapping>

6.编写工具类,需要在数据库中创建对应的数据库(数据库名在hibernate.cfg.xml中),将hbm生成ddl,也就是hbm2ddl

package com.bjpowernode.hibernate;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * 将hbm生成ddl
 * @author happy
 *
 */
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);
	}
}

       运行该main方法,执行过程会先判断数据库中是否存在要插入的表(也就是我们的User表),不存在才会创建。执行效果如下:


7.客户端测试类:向表中添加数据

package com.bjpowernode.hibernate;

import java.util.Date;

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

public class Client {

	public static void main(String[] args) {
		
		//读取hibernate.cfg.xml文件
		Configuration cfg = new Configuration().configure();
		
		//建立SessionFactory
		SessionFactory factory = cfg.buildSessionFactory();
		
		//取得session
		Session session = null;
		try {
			session = factory.openSession();
			//开启事务
			session.beginTransaction();
			User user = new User();
			user.setName("楚乔");
			user.setPassword("123");
			user.setCreateTime(new Date());
			user.setExpireTime(new Date());
			
			//保存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();
				}
			}
		}
	}
}

测试结果如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值