Hibernate4 HelloWorld

一个基本的 Hibernate 应用的开发过程如下:

1 编写配置文件 hibernate.cfg.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
    <!-- 配置连接数据库的基本信息 -->
        <property name="connection.username">root</property>
        <property name="connection.password">stu312</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql:///ld</property>

        <!-- 配置 hibernate 的基本信息 -->
        <!-- hibernate 所使用的数据库方言 -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> -->

        <!-- 执行操作时是否在控制台打印 SQL -->
        <property name="show_sql">true</property>

        <!-- 是否对 SQL 进行格式化 -->
        <property name="format_sql">true</property>

        <!-- 指定自动生成数据表的策略 -->
        <property name="hbm2ddl.auto">create</property>

        <!-- 指定关联的 .hbm.xml 文件 -->
        <mapping resource="com/shan/hello/News.hbm.xml" />
    </session-factory>
</hibernate-configuration>

2 创建实体类

package com.shan.hello;

import java.util.Date;

public class News {

    private Integer id;
    private String title;
    private String author;
    private Date date;

    private String desc;

    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 String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }


    public News() {
        super();
    }

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

    public News(String title, String author, Date date) {
        super();
        this.title = title;
        this.author = author;
        this.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 2015-11-1 17:50:05 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping package="com.shan.hello">
    <class name="News" table="NEWS" dynamic-insert="true">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
            <generator class="native" />
        </id>

        <property name="title" not-null="true" unique="true" index="news_index" length="50" type="java.lang.String" column="TITLE">
        </property>

        <property name="author" type="java.lang.String" index="news_index">
            <column name="AUTHOR" />
        </property>

        <property name="date" type="date">
            <column name="DATE" />
        </property>

        <property name="desc" formula="(SELECT concat(title, ',', author) FROM NEWS n WHERE n.id = id)"></property>
    </class>
</hibernate-mapping>

4 在程序用使用 Hibernate 的基本模式

package com.shan.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 org.junit.Test;

public class HiberanteTest {
    @Test
    public void testHibernateHello(){
        /** * 1 创建一个 SessionFactory 对象: * 1). 创建 Configuration 对象,对应 hibernate 的基本配置信息和对象关系映射文件信息 * 2). 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象 * hibernate 的任何配置和服务都需要在该对象中注册后才能有效. * 3). Configuration 对象创建 sessionFactory 对象。 */
        SessionFactory sessionFactory = null;
        Configuration configuration = new Configuration().configure();
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().
                applySettings(configuration.getProperties()).buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        /** * 2 创建一个 session 对象,开启事务,执行保存操作向数据库中写入数据 */
        Session session = sessionFactory.openSession();
        Transaction transactin = session.beginTransaction();
        News news = new News("HelloWorld","shan",new Date(new java.util.Date().getTime()));
        session.save(news);

        /** * 3 提交事务,关闭 session,关闭 sessinFactory 对象。 */
        transactin.commit();
        session.close();
        sessionFactory.close();

    }

}

5 运行测试代码所打印出的 SQL 语句

Hibernate: 
    drop table if exists NEWS Hibernate: create table NEWS ( ID integer not null auto_increment, TITLE varchar(50) not null, AUTHOR varchar(255), DATE date, primary key (ID) ) Hibernate: alter table NEWS add constraint UK_duq2gjdo5k53otrakypw0888b unique (TITLE) Hibernate: create index news_index on NEWS (TITLE, AUTHOR) 十一月 01, 2015 6:56:04 下午 org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: HHH000230: Schema export complete Hibernate: insert into NEWS (TITLE, AUTHOR, DATE) values (?, ?, ?) 

你可能感兴趣的:(Hibernate)