配置Hibernate的流程

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

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();
    }
    
}

 

posted @ 2014-09-28 09:16  Jeremy_software  阅读(247)  评论(0编辑  收藏  举报