天天看点

Hibernate学习手记(1)--搭建环境搭建环境:

搭建环境:

Eclipse3.4

Build Path Configeration: 新建 hibernate 库,并加进外部类包:

基本的有 hibernate3 以及 hibernate-distribution-3.3.2.GA/lib/required 目录下的全部类包

还有 hibernate-annotations , hibernate-commons-annotations , ejb3-persistence

日志系统的: slf4j-nop-1.5.11 和 slf4j-api-1.5.11

测试系统的: junit-4.8.2

Hibernate 可以通过 XML 解析或 annotations 标注对实体对象进行映射,实现用面向对象的方式操作数据库。

Hibernate 基本配置:

Hibernate.cfg.xml 文件:(注意该文件最好不要私自改名,且要放在项目的根目录下)

最主要的声明部分:

< hibernate-configuration >

    < session-factory >

        <!-- Database connection settings -->

        < property name = "connection.driver_class" >

                com.microsoft.sqlserver.jdbc.SQLServerDriver

        </ property >

        < property name = "connection.url" >

             jdbc:sqlserver://localhost :1433;databaseName=Test

        </ property >

        < property name = "connection.username" > username </ property >

         < property name = "connection.password" > *************

        </ property >

        <!-- JDBC connection pool (use the built-in) -->

        < property name = "connection.pool_size" > 1 </ property >

        <!-- SQL dialect -->

        < property name = "dialect" >

                   org.hibernate.dialect.SQLServerDialect

        </ 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.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 >

        <!—-format the sql-->

        <property name="format_sql">true</property>

        <!-- 实体映射对象 -->

        < mapping resource = "com/Test/Demo.hbm.xml" />

       < mapping class = "com.Test.Demo" />

        <!-- 以上声明为 XML 映射方式,下面为 annotations 映射方式的声明 -->

       < mapping class = "com.Test.T" />

    </ session-factory >

</ hibernate-configuration >

XML 解析方式:

  对象映射文件 Demo.hbm.xml ,命名规则一般把被映射的类的类名作为最顶级的名再加上后缀 .hbm.xml , 并与被映射的类放在同一个包里 。

< hibernate-mapping >

  < class name = "com.Test.Demo" table = "[dbo].[T]" >

         < id name = "id" column = "id" type = "java.lang.Integer" >

            < generator class = "native" />

         </ id >

         < property name = "name" column = "name" />

         < property name = "no" column = "no" />

  </ class >

</ hibernate-mapping >

Annotations 注析方式:

package com.Test;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

@Entity

@Table(name="_T")

public class T {

    private int id;

    @Id

    public int getId() {

       return id;

    }

    public void setId(int id) {

       this.id = id;

    }

    @Column(name="_name")

    public String getName() {

       return Name;

    }

    public void setName(String name) {

       Name = name;

    }

    public int getNo() {

       return no;

    }

    public void setNo(int no) {

       this.no = no;

    }

    private String Name;

    private int no;

}

Junit 测试

使用 Junit 进行对象映射的测试,同时验证数据库里的数据

package com.TestHibernate; // 测试包命名规则,一般为 Test+ 被测试的项目名

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.AnnotationConfiguration;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import static org.junit.Assert.*;

import static org.hamcrest.CoreMatchers.*;

//assertThat 和 is 断言要用到的外部类包

import com.Test.T;

import junit.framework.TestCase;

public class TestT extends TestCase {

    Session session ;

    SessionFactory sf ;

    T t ;

    @Before

    public void setUp() throws Exception {

         AnnotationConfiguration cfg =

                     new AnnotationConfiguration();

         sf = cfg.configure().buildSessionFactory();

         session = sf .openSession();

         session .beginTransaction();

         t = new T();

    }

    @After

    public void tearDown() throws Exception {

        session .close();

        sf .close();

    }

    @Test

    public void testT() {

       int x= 3;

       String y = "x" ;

       int z = 11;

       t .setId(x);

       t .setName(y);

       t .setNo(z);

       session .save( t );

       session .getTransaction().commit();

       assertThat (x,is ( t .getId()));

       assertThat (y,is ( t .getName()));

       assertThat (z,is ( t .getNo()));      

    }

}