Hibernate4 认识Java Project简单实用

来源:互联网 发布:python 求均值和方差 编辑:程序博客网 时间:2024/04/29 10:58

1.配置hibernate.cfg.xm文件,这个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>
        <!-- Database connection settings -->
        <property name="hibernate.connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="hibernate.connection.url">
            jdbc:mysql://localhost:3306/data
        </property>

        <property name="hibernate.connection.username">
            root
        </property>
        <property name="hibernate.connection.password">
            root
        </property>
        <property name="hibernate.connection.pool_size">
            1
        </property>
        <property name="show_sql">
            true
        </property>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="hibernate.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="com/broadvision/hibernatefirst/Person.hbm.xml" />
    </session-factory>
</hibernate-configuration>

本例实用Mysql数据库。

hibernate.connection.url 代表数据库的url,本例中实用本地Mysql服务器中的data数据库。mapping对应xml文件把类和数据库中的表对应。


2.Person.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">
<hibernate-mapping package="com.broadvision.hibernatefirst">
    <class name="Person" table="person">
        <id name="id" type="int">
             <generator class="increment" />  
        </id>

        <property name="name" type="java.lang.String">
        </property>

    </class>
</hibernate-mapping>


Person.hbm.xml 文件和它对应的类Person一起放在包packagecom.broadvision.hibernatefirst 下面。


3.Person类的代码如图:


package com.broadvision.hibernatefirst;

public class Person {
    
    private int id;
    private String name;
    public Person() {
        super();
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }


4.项目用到的的jar可以放到一个library西面,如下图


hibernate4 用到的jar在“${hibernate_home}\lib\required\*”目录下面,不要忘记连接特定数据库的jdbc的jar包

如MySql的mysql-connector-java-5.1.3-rc-bin.jar.


5.如何得到SessionFactory来对数据库进行操作呢?下面的代码就可以了:

public class PersonDetailTest {
    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;

    public static void main(String[] args) {
        Session session = null;
        try {
            try {

                //得到SessionFactory
                Configuration cfg = new Configuration().configure();
                serviceRegistry = new ServiceRegistryBuilder().applySettings(
                        cfg.getProperties()).buildServiceRegistry();
                sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();;


            } catch (Throwable ex) {
                System.err.println("Failed to create sessionFactory object."
                        + ex);
            }
            session = sessionFactory.openSession();

            Person person = new Person();

           //事务里增加一个Person,如果增加时候可以回滚。
            Transaction tx = session.beginTransaction();
            person.setName("Roseindia");
            session.save(person);
            tx.commit();

            System.out.println("Done");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {

           //最后关闭session
            session.close();
        }
    }



原创粉丝点击