Hibernate基础知识
 
概念: Hibernate是一个面向Java环境的对象/关系数据库映射的工具。Hibernate使用数据库和配置信息为应用程序提供持久化的服务。
作用:Hibernate对JDBC进行了对象封装,使得Java程序员可以随心所欲地使用对象编程思维来操纵数据库,Hibernate不仅仅管理Java类到数据库表的映射,还可以提供数据查询和获取数据的方法,可以大幅减少开发时人工使用SQL和JDBC处理数据的时间。
原理:用公共类实现数据库的连接,查询,删除,更新以及关闭连接等操作。把连接信息配置在文本文件中,这样修改环境不需要编译Java文件。对操作结束后的连接不马上关闭,而是维持一个合理的空间连接数,获取连接也不一定完全新建连接,可以从空闲连接池中获取。数据库操作不局限于表、列的形式,而可以把表映射为类对象,对数据库连接池、会话的管理实行统一的框架化。
 
实践:
一、新建项目并配置环境:
1、新建一个项目目录d:\myprojects\hibernatetest,在项目目录下新建两个子目录src、lib,其中src目录用于存放源文件和xml文件,lib目录用于存放项目开发需要的库文件和数据库驱动程序。
2、创建名为testdb的mysql数据库,并建立数据表students
二、编写持久化类
package entity;

/**
* Created by IntelliJ IDEA.
* User: xiaohui
* Date: 2008-12-4 11:34:01
*/

public class Student {
         private long id;
         private String name;
         private int age;
         private String degree;
         private String speciality;

         public Student() {
        }

         public Student(String name, int age, String degree, String speciality) {
                 this.name = name;
                 this.age = age;
                 this.degree = degree;
                 this.speciality = speciality;
        }

         public long getId() {
                 return id;
        }

         public void setId( long id) {
                 this.id = id;
        }

         public String getName() {
                 return name;
        }

         public void setName(String name) {
                 this.name = name;
        }

         public int getAge() {
                 return age;
        }

         public void setAge( int age) {
                 this.age = age;
        }

         public String getDegree() {
                 return degree;
        }

         public void setDegree(String degree) {
                 this.degree = degree;
        }

         public String getSpeciality() {
                 return speciality;
        }

         public void setSpeciality(String speciality) {
                 this.speciality = speciality;
        }

        @Override
         public String toString() {
                 return "Student{" +
                                 "id=" + id +
                                 ", name='" + name + '\'' +
                                 ", age=" + age +
                                 ", degree='" + degree + '\'' +
                                 ", speciality='" + speciality + '\'' +
                                '}';
        }
}

三、编写映射文件
<? xml version ="1.0" encoding ="utf-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

< hibernate-mapping >
         < class name ="entity.Student" table ="Student" >
                 < id name ="id" type ="java.lang.Long" >
                         < column name ="id" />
                         < generator class ="native" > </ generator >
                 </ id >
                 < property name ="name" type ="java.lang.String" column ="name" />
                 < property name ="age" type ="java.lang.Integer" column ="age" />
                 < property name ="degree" type ="java.lang.String" column ="degree" />
                 < property name ="speciality" type ="java.lang.String" column ="speciality" />
         </ class >
</ hibernate-mapping >

四、Hibernate配置
<? xml version ='1.0' encoding ='utf-8' ?>
<!DOCTYPE hibernate-configuration PUBLIC
                "-//Hibernate/Hibernate Configuration DTD//EN"
                "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
< hibernate-configuration >
         < session-factory >
                 < property name ="connection.url" >jdbc:mysql://localhost:3306/testdb </ property >
                 < property name ="connection.driver_class" >com.mysql.jdbc.Driver </ property >
                 < property name ="connection.username" >root </ property >
                 < property name ="connection.password" >xiaohui </ property >
                <!-- DB schema will be updated if needed -->
                 < property name ="hbm2ddl.auto" >update </ property >
                 < property name ="show_sql" >true </ property >
                 < property name ="format_sql" >true </ property >
                 < property name ="connection.autocommit" >true </ property >

                 < mapping resource ="entity/User.hbm.xml" />
                 < mapping resource ="entity/News.hbm.xml" />
                 < mapping resource ="entity/Student.hbm.xml" />
         </ session-factory >
</ hibernate-configuration >

五、编写应用文件
package dao;

import utils.BaseDAO;
import entity.Student;
import org.hibernate.Session;


/**
* Created by IntelliJ IDEA.
* User: xiaohui
* Date: 2008-12-4 12:18:13
*/

public class StudentDAO extends BaseDAO {
         public Student saveStudnet(Student student){
                Session session=getSession();
                session.save(student);
                session.close();
                 return student;

        }

         public static void main(String[] args) {
            Student student= new Student();
            student.setName( "小周");
            student.setAge(22);
            student.setDegree( "本科");
            student.setSpeciality( "装潢设计");
            StudentDAO studentDAO= new StudentDAO();
            studentDAO.saveStudnet(student);

                System.out.println(student);
        }

}
运行结果:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate:    
        insert    
        into
                Student
                (name, age, degree, speciality)    
        values
                (?, ?, ?, ?)
Student{id=14, name='小周', age=22, degree='本科', speciality='装潢设计'}

Process finished with exit code 0