码迷,mamicode.com
首页 > 系统相关 > 详细

手动配置hibernate4.3.4的步骤

时间:2014-06-10 06:45:05      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   java   a   

1:创建一个web项目或者java项目

2:找到hibernate包lib目录下面的required下面的包把包放到lib目录下面,在导入buidler path里面

bubuko.com,布布扣

3:写java类,我写的java bean类,代码如下:

package org.hibernate.domain;

import java.util.Date;

public class Event {

 private Long id;

    private String title;
    private Date date;

    public Event() {}

    public Long getId() {
        return id;
    }

    private void setId(Long id) {
        this.id = id;
    }

    public Date getDate() {
        return date;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

}

 

4:在java bean的相同目录下面创建对象的*.hbm.xml文件

比如:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"
http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.domain">

    <class name="Event" table="EVENTS">
        <id name="id" column="EVENT_ID">
            <generator class="native"/>
        </id>
        <property name="date" type="timestamp" column="EVENT_DATE"/>
        <property name="title"/>
    </class>

</hibernate-mapping>

 

 

5:在src目录下面创建文件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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate‘s automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <!-- <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> -->

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <mapping resource="org/hibernate/domain/Event.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

 

6.编写一个工具类得到SessionFactory,代码如下

package org.hibernate.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

 private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
           /* return new Configuration().configure().buildSessionFactory(
       new StandardServiceRegistryBuilder().build() );*/
         Configuration configuration = new Configuration().configure();
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
            SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            return sessionFactory;
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
           
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

 

 

7:编写一个测试类,测试配置是否成功了

package com.demo.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.domain.Event;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.util.HibernateUtil;

public class TestMain {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  

  

       
       
  Session session =HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Event theEvent = new Event();
        theEvent.setTitle("好好学习天天向上3");
        theEvent.setDate(new Date());
        session.save(theEvent);
        session.getTransaction().commit();
       

 }

}

8:控制台输出语句如下,则说明配置成功

Hibernate: insert into EVENTS (EVENT_DATE, title) values (?, ?)

 

9:查看数据库库的表,可以看到数据,注意,这里的表示hibernate自动给我们建的

 

 

 

 

 

 

手动配置hibernate4.3.4的步骤,布布扣,bubuko.com

手动配置hibernate4.3.4的步骤

标签:c   style   class   blog   java   a   

原文地址:http://blog.csdn.net/j903829182/article/details/29623859

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!