Hibernate框架的使用。。。

在lib文件夹里面导入Hibernate开发的架包和连接mysql,连接数据源c3p0的架包...

  在src目录下建立Hibernate Configuration  File(cfg.xml)的配置文件:hibernate.cfg.xml;

其中有配置连接数据库的架包,配置Hibernate的基本信息;最后,指定关系的 .hbm.xml文件 ,包名和在该包下的hbm.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">lxn123</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql:///hibernate</property>
        
        <!-- 配置hibernate的基本信息 -->
        
        <!-- hibernate使用的数据库方言 ,这个是mysql支持事物的方法,在包:
        hibernate-release-4.2.4.Finalhibernate-release-4.2.4.Finalprojectetchibernate.properties.template,文件中找-->
        <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
        
        <!-- 执行操作是否在控制台打印,在eclipse中显示实现操作的sql语句 -->
        <property name="show_sql">true</property>
        
        <!-- 是否对SQL格式化 ,显示一行还是多行,便于阅读-->
        <property name="format_sql">true</property>
        
        <!-- 指定自动生成数据表的策略,一共可以去四个值 -->
        <property name="hbm2ddl.auto">update</property>
        
        <!-- 指定关系的 .hbm.xml文件 ,包名和在该包下的hbm.xml文件名,是一个目录结构-->
        <mapping resource="com/atguigu/hibernate/hello/News.hbm.xml"/>
        
    </session-factory>
</hibernate-configuration>

创建一个持久化类:将要建立的数据表中的属性封装在这个类中;

package com.atguigu.hibernate.hello;

import java.sql.Date;

public class News {
    private Integer id;
    private String title;
    private String author;
    private Date date;
    public News(String title, String author, Date date) {
        super();
        this.title = title;
        this.author = author;
        this.date = date;
    }

    public News() {
        super();
        // TODO Auto-generated constructor stub
    }

    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 String toString() {
        return "News [id=" + id + ", title=" + title + ", author=" + author
                + ", date=" + date + "]";
    }
    
    
}

在持久化类的同一包下建立Hibernate XML Mapping file(hbm.xml)的 一个对象关系映射文件:News.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">
<!-- Generated 2016-9-15 15:51:47 by Hibernate Tools 3.4.0.CR1 -->

<!-- 对象关系映射文件 -->
<hibernate-mapping>

    <class name="com.atguigu.hibernate.hello.News" table="NEWS">
        <!-- 属性名,类型 -->
        <id name="id" type="java.lang.Integer">
        
                <!-- 列名 -->
            <column name="ID" />
            
             <!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
            <generator class="native" />
        </id>
        
        <property name="title" type="java.lang.String">
            <column name="TITLE"></column>
        </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>

最会,建立一个类,实现,实现表的建立;

package com.atguigu.hibernate.hello;


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 junit.framework.TestCase;

public class HeibernateTT extends TestCase {
    
    public static void main(String[] args) {
        //1. 创建一个 SessionFactory 对象,一般在一个项目中建立以,比较消耗资源
        SessionFactory sessionFactory=null;
        
        //1). 创建 Configuration 对象: 对应 hibernate 的基本配置信息和 对象关系映射信息
        Configuration configuration=new Configuration().configure();
        
        //2). 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象
        //hibernate 的任何配置和服务都需要在该对象中注册后才能有效.
        ServiceRegistry serviceRegistry=
                new ServiceRegistryBuilder()
                    .applySettings(configuration
                    .getProperties()).buildServiceRegistry();
        
        sessionFactory=configuration.buildSessionFactory(serviceRegistry);
        
        //2. 创建一个 Session 对象,是用户和数据库之间进行交互操作的单线程对象,
        Session session=sessionFactory.openSession();
        
        //3. 开启事务,Transaction事物
        Transaction transaction=session.beginTransaction();
        
        //4. 执行保存操作
        News news=new News("java", "panpan", new Date(new java.util.Date().getTime()));
        //插入数据
        session.save(news);
        
        //5. 提交事务 
        transaction.commit();
        
        //6. 关闭 Session
        session.close();
        
        //7. 关闭 SessionFactory 对象
        sessionFactory.close();
        
    }
}
原文地址:https://www.cnblogs.com/lxnlxn/p/5892853.html