第一次使用hibernate

   今天第一次使用了hibernate框架成功的实现想数据库里插入数据,初次使用可能会觉得很麻烦,但是相比较在web开发中使用原始的拼SQL语句的方法,确实方便许多.

   使用hibernate,我下载的是hibernate-annotations-3.4.0.GA.zip,hibernate-distribution-3.3.2.GA-dist.zip和slf4j-1.5.8.zip,这三个压缩包都是在官网上下载下来的,如果你使用的不是这三个版本,那么就得到官网上查查版本之间的对应关系,因为如果版本匹配的话,可能会在编译期间会出现一些意想不到的问题.

 将这三个压缩包解压之后,你需要分别将hibernate-distribution-3.3.2.GA文件夹下的hibernate3.jar以及lib下面required中的所有jar包,hibernate-annotations-3.4.0.GA文件夹下面的hibernate-annotations.jar和slf4j-1.5.8文件夹中的slf4j-nop-1.5.8.jar导入MyEclipse中.当然还必须得有mysql-connector-java-5.0.8-bin.jar(我使用的是MySQL,下载的是这个版本的jar包),既然要使用hibernate,那么还必须要导入hibernate-annotations-3.4.0.GA文件夹的lib目录下面的hibernate-commons-annotations.jarejb3-persistence.jar两个jar包.一共需要的是12jar包.而在hibernate-distribution-3.3.2.GA\documentation\manual\zh-CN\html_single的index.html就是hibernate的使用手册.

 下面进行第一次使用hibernate.

 首先建立数据库:

create database hibernate;



use hibernate;



create table hibernate(id int primary key,name varchar(20),age int);

 

 建立一个Java工程,建立一个Student类,具有id,name,age三个属性,并写好set和get方法.代码如下:

public class Student {

    private int id;

    private String name;

    private int age;

    private String phoneNumber;

    

    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;

    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }

    public String getPhoneNumber() {

        return phoneNumber;

    }

    public void setPhoneNumber(String phoneNumber) {

        this.phoneNumber = phoneNumber;

    }

}

 按照大多数人的习惯,在建立Student类的同级目录中建立Student.hbm.xml文件.代码如下:

 1 <?xml version="1.0"?>

 2 <!DOCTYPE hibernate-mapping PUBLIC

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

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

 5 

 6 <hibernate-mapping package="com.nenu.hibernate">

 7     <class name="Student" table="student">    <!--将实体类和数据库中的表关联,如果数据库的表明和实体类名一致,则可以省略table="表名"-->

 8         <id name="id" column="id"></id><!-- 指定主键 -->

 9         <property name="name" column="name"></property>    <!-- 属性之间的对应,如果属性名和列名一致,也可以省略column="列名"-->

10         <property name="age"></property>

11     </class>

12 </hibernate-mapping>

 接下来就是写hibernate的配置文件了.在工程的src目录下新建文件hibernate.cfg.xml,在使用手册中copy(建议不用手写,直接copy)配置文件:

 1 <?xml version='1.0' encoding='utf-8'?>

 2 <!DOCTYPE hibernate-configuration PUBLIC

 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

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

 5 

 6 <hibernate-configuration>

 7 

 8     <session-factory>

 9 

10         <!-- Database connection settings -->

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

12         <property name="hibernate.connection.url">

13                <![CDATA[jdbc:mysql://localhost:3306/daycode?useUnicode=true&characterEncoding=utf8]]>        

14         </property>

15         <!-- 数据库地址 -->

16         <property name="connection.url">jdbc:mysql://localhost/hibernate</property>

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

18         <property name="connection.password">root</property>

19 

20         <!-- 数据库连接池 -->

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

22         <!-- <property name="connection.pool_size">1</property> -->

23 

24         <!-- SQL dialect SQL方言,如果使用其他数据库则需要修改方言,具体见手册-->

25         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

26 

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

28         <!-- <property name="current_session_context_class">thread</property>  暂时用不上-->

29 

30         <!-- Disable the second-level cache 二级缓存不可见  二级缓存利于hibernate的优化-->

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

32 

33         <!-- Echo all executed SQL to stdout -->

34         <property name="show_sql">true</property>

35 

36         <!-- Drop and re-create the database schema on startup hibernate自动生成建表语句-->

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

38 

39         <mapping resource="com/nenu/hibernate/Student.hbm.xml"/>

40         

41 

42     </session-factory>

43 

44 </hibernate-configuration>

 至此,已经完成了大部分的工作了,现在只需要新建一个StudentTest类进行测试即可.代码如下:

 1 import org.hibernate.Session;

 2 import org.hibernate.SessionFactory;

 3 import org.hibernate.cfg.Configuration;

 4 

 5 public class studentTest {

 6     public static void main(String[] args) {

 7         Student student = new Student();

 8         

 9         student.setId(1);

10         student.setAge(22);

11         student.setName("hibernate");

12         

13         Configuration cfg = new Configuration();

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

15         Session session = sf.openSession();

16 

17         session.beginTransaction();//进行事务管理    

18         session.save(student);

19         session.getTransaction().commit();

20     }

21 }

 运行studentTest.java,可以看见控制台输出了sql语句:Hibernate: insert into Teacher (age, name, title, id) values (?, ?, ?, ?);则表明成功,查看数据库便可看到数据.

 使用hibernate的方便之处在于能将一个对象方便的插入到一个不是面向对象DBMS中.

你可能感兴趣的:(Hibernate)