Hibernate3.5.4---java application的xml和annotation环境搭建(hibernate.cfg.xml配置文件说明,映射文件Student.hbm.xml说明)

来源:互联网 发布:沙驰皮鞋 知乎 编辑:程序博客网 时间:2024/05/19 01:11

原文转自:http://blog.csdn.net/centre10/article/details/6050466

Hibernate负责持久层,将modal持久化到数据库。由于jdbc使用的sql语言不是面向对象,感觉不舒服,写起来也比较复杂。Hibernate对其进行了封装,让我们可以按照面向对象的思维方式编程。Hibernate采用了2中方式将modal持久化到DB。

环境搭建:到官网下载http://www.hibernate.org/  hibernate-distribution-3.5.4-Final-dist.zip

在myeclipse中添加userlibary:Window->Preferences->Java->Bulid Path->User Libraries->New,加入相关包。Hibernate3.jar(hibernate核心包,包含了以前版本所需要的hibernate-commons-annotations.jar,hibernate-annotations.jar),antlr-2.7.6.jar,commos-collections-3.1.jar,dom4j-1.6.1.jar,javassist-3.9.0.GA.jar,jta-1.1.jar,slf4j-api-1.5.8.jar(这是一个接口包,其实现为:slf4j-nop-1.5.8.jar)slf4j-nop-1.5.8.jar(其下载地址:http://www.slf4j.org/download.html),hibernate-jpa-api-1.0.0.Filnal.jar。

 

3.5版本以前的hibernate还需要hibernate-commons-annotations.jar,hibernate-annotations.jar(反射时需要的包),ejb3-persistence.jar

Xml实现的持久化:

Hibernate.cfg.xml

<?xmlversion='1.0'encoding='utf-8'?>

<!DOCTYPEhibernate-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 -->

        <propertyname="connection.driver_class">com.mysql.jdbc.Driver</property>

        <propertyname="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate</property>

        <propertyname="connection.username">root</property>

        <propertyname="connection.password">centre</property>

 

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

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

 

        <!-- SQL dialect 方言,官方的HSQL语言,将转换成各种数据库的sql-->

        <propertyname="dialect">org.hibernate.dialect.MySQLMyISAMDialect</property>

 

        <!-- Enable Hibernate's automatic session context management -->

        <propertyname="current_session_context_class">thread</property>

 

        <!-- Disable the second-level cache优化hibernate  -->

        <!--

        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

 

        -->

        <!-- Echo all executed SQL to stdout打印生成的sql语句 -->

        <propertyname="show_sql">true</property>

 

        <!-- Drop and re-create the database schema on startup ddl数据定义语句-->

        <!--

        <property name="hbm2ddl.auto">update</property>

-->

        <mapping resource="com/xie/hibernate/modal/Student.hbm.xml"/>

 

    </session-factory>

 

</hibernate-configuration>

映射xml

Student.hbm.xml前面的Studentmodal名完全相同,同时该文件应该与modal为与同一包中

<?xmlversion="1.0"?>

<!DOCTYPEhibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- hibernate映射,包名对应java的包名 -->

<hibernate-mappingpackage="com.xie.hibernate.modal">

<!-- class用于指定modal名和表名对应,name属性指modal名,table指与modal对应的表名,

2者相同时,可以不指定table -->

    <classname="Student"table="Student">

    <!-- id指定modal的属性对应与表的主键 -->

         <idname="id"column="id"></id>

         <!-- property指定modal属性与表的字段的对应,如果名字相同,column可以不写 -->

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

         <propertyname="age"column="age"></property>

    </class>

</hibernate-mapping>

Mysql创建数据库和表的语句:

create database hibernate;

create table student(

id int primary key auto_increment,

name varchar(12) not null,

age int);

annotation持久化(目前公司常用):

这种方式不用配置*.hbm.xml文件,在modal中的设置如下:

package com.xie.hibernate.modal;

 

import javax.persistence.Entity;

import javax.persistence.Id;

 

import org.hibernate.annotations.Table;

//指明这是一个实体

@Entity

@Table(appliesTo = "Teacher")

public class Teacher {

     private int id;

     private String title;

     private String name;

     private int age;

//指明主键,放于get方法前

@Id    

         public int getId() {

                   return id;

         }

         public void setId(int id) {

                   this.id = id;

         }

         public String getTitle() {

                   return title;

         }

         public void setTitle(String title) {

                   this.title = title;

         }

         public int getAge() {

                   return age;

         }

         public void setAge(int age) {

                   this.age = age;

         }

         public String getName() {

                   return name;

         }

         public void setName(String name) {

                   this.name = name;

         }

    

    

}

在hibernate.cfg.xml指定如下:

<mapping class="com.xie.hibernate.modal.Teacher"/>

Msyql建表语句为:

create table teacher(

id int primary key auto_increment,

title varchar(12) not null,

name varchar(12) not null,

age int);

 

说明:hibernate能帮助我们建表。

主函数类如下:

package com.xie.hibernate.main;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.AnnotationConfiguration;

import org.hibernate.cfg.Configuration;

 

//import com.xie.hibernate.modal.Student;

import com.xie.hibernate.modal.Teacher;

 

public class TestHibernate {

    public static void main(String[] args) {

/*         Student s=new Student();

             s.setId(1);

             s.setName("xie");

             s.setAge(25);

            

             Configuration cfg=new Configuration();

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

             Session session=sf.openSession();

             session.beginTransaction();

             session.save(s);

             session.getTransaction().commit();

             session.close(); */

            

             Teacher t=new Teacher();

             t.setId(3);

             t.setName("liu");

             t.setAge(52);

             t.setTitle("中级");

             Configuration cfg=new AnnotationConfiguration();

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

             Session session=sf.openSession();

             session.beginTransaction();

             session.save(t);

             session.getTransaction().commit();

             session.close();

            

         }

}

原创粉丝点击