zoukankan      html  css  js  c++  java
  • [Hibernate 1]Hibernate的环境搭建

    一、Hibernate是什么

    直接使用JDBC操作数据库的步骤很繁琐,JDBC操作的是关系型数据库,而我们用JAVA开发程序,则使用面向对象的思想。Hibernate正是在这两种不同的模型之间建立关联,给我们提供了利用面向对象的思想来操作关系型数据的接口。

    Hibernate可以做什么:

    1,将对象数据保存到数据库

    2,将数据库数据读入对象中


    二、创建Hibernate环境

    1,下载Hibernate,解压缩

    2,使用Eclipse创建新的项目(也可以用别IDE,比如myeclipse等)

    3,引入Hibernate及其依赖库

    4,引入MySQL数据库驱动包(根据自己的项目而定,如果是Oracle,则引入Oracle的驱动包)


    三、具体操作

    1,需要引入的包

    从解压后的Hibernate里引入lib里面的jar包+Hibernate本身的一个jar包


    2,引入数据库驱动包(以MySQL为例,驱动包自行网上下载)

    3,新建一个测试库Hibernate_first

    4,在Java项目中配置MySQL

    新建一个xml文件(hibernate.cfg.xml),也可以直接从解压包里复制:Hibernate3.2-doc-tutorial-src,然后根据自己的情况进行配置,以下是我的测试配置:

    <span style="font-family:KaiTi_GB2312;font-size:18px;"><!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="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">数据库登录名</property>
    		<property name="hibernate.connection.password">登录密码</property>
    		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    		<property name="hibernate.show_sql">true</property>    <!--运行时是否在控制台打印sql语句-->
    		<property name="hibernate.format_sql">true</property>   <span style="font-family: Arial, Helvetica, sans-serif;"><!--是否对控制台的sql语句进行格式化</span><span style="font-family: Arial, Helvetica, sans-serif;">--</span><span style="font-family: Arial, Helvetica, sans-serif;">></span><span style="font-family: Arial, Helvetica, sans-serif;">
    </span>
    		
    		<mapping resource="com/angel/hibernate/User.hbm.xml"/> <!--匹配用户映射实体-->
    	
    	</session-factory>
    </hibernate-configuration></span>

    5,建立实体文件和映射文件

    实体文件User类:

    <span style="font-family:KaiTi_GB2312;font-size:18px;">package com.angel.hibernate;
    
    import java.util.Date;
    
    public class User {
    
    	private String id;
    	private String name;
    	private String password;
    	private Date createTime;
    	private Date expireTime;
    	public String getId() {
    		return id;
    	}
    	public void setId(String id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;
    	}
    	public Date getCreateTime() {
    		return createTime;
    	}
    	public void setCreateTime(Date createTime) {
    		this.createTime = createTime;
    	}
    	public Date getExpireTime() {
    		return expireTime;
    	}
    	public void setExpireTime(Date expireTime) {
    		this.expireTime = expireTime;
    	}
    	
    }</span>
    映射文件User.hbm.xml:

    <span style="font-family:KaiTi_GB2312;font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
    <!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.angel.hibernate.User">
    		<id name="id">
    			<generator class="uuid"></generator>
    		</id>
    		<property name="name"></property>
    		<property name="password"></property>
    		<property name="createTime"></property>
    		<property name="expireTime"></property>
    	</class>
    </hibernate-mapping></span>

    6,根据实体映射文件生成数据库表单:

    <span style="font-family:KaiTi_GB2312;font-size:18px;">package com.angel.hibernate;
    
    import org.hibernate.cfg.Configuration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    
    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);
    	}
    }</span>

    7,对实体进行操作

    <span style="font-family:KaiTi_GB2312;font-size:18px;">package com.angel.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("Angel");
    			user.setPassword("23125");
    			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();
    				}
    			}
    		}
    	}
    }
    </span>

    四、总结

    Configuration类负责管理Hibernate的配置信息。它包括如下内容:
    1,Hibernate运行的底层信息:数据库的URL、用户名、密码、JDBC驱动类,数据库Dialect,数据库连接池等。
    2,Hibernate映射文件(*.hbm.xml)。

    Hibernate配置的两种方法(在本实例中用的第二种):
    1,属性文件(hibernate.properties),调用代码:Configurationcfg= new Configuration();
    2,Xml文件(hibernate.cfg.xml),调用代码:Configurationcfg= new Configuration().configure();

    以上就是对于hibernate简单环境搭建的介绍,下篇博客将介绍持久化对象的三种状态。



  • 相关阅读:
    Docker基础 ubuntu安装docker
    layui.laytpl 模板引擎用法
    golang 中 strings 包的 Replace 用法介绍笔记
    golang之结构体
    Mysql复习秘籍
    NFS 共享存储
    Rsyncd 同步服务
    企业服务器架构
    基础面试题二
    基础面试题一
  • 原文地址:https://www.cnblogs.com/hhx626/p/6010323.html
Copyright © 2011-2022 走看看