首页 > 技术文章 > 用my eclipse 新建hibernate 第一个程序

js19961226 2016-04-27 16:14 原文

Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。

今天就来开始建我们的第一个hibernate程序

1.新建一个java  project 名为hibernate01。

2.导入hibernate插件(选中项目单击鼠标右键-->my eclipse-->project  facets-->hibernate)。

3.在hibernate01这个项目下新建一个包 hibernate.model  。

4.在hibernate.model包下新建一个class  --> Student类

Student.class 代码如下:

package hibernate.model;

public class Student {
private int id;

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;
}
private String name;
private int age;
}

5.在hibernate.model包下新建xml -->Student.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="hibernate.model">
<!-- 如果创建的表名=数据库表名(数据库不区分大小写)   可以不写数据库表名 -->
	<class name="Student" >
	<!-- 主键属性 -->
		<id name="id" />
		<!-- 其它的属性 -->
		<property name="name" />
		<property name="age" />
    </class>
	
</hibernate-mapping>

6.配置hibernate.cfg.xml文件:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings 用到的驱动、数据库名、用户名、密码 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/text</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <!-- JDBC connection pool (use the built-in) -->
       <!--  <property name="connection.pool_size">1</property>-->

        <!-- SQL dialect  数据库方言-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</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>
<!-- 将配置的student表的配置  引进来 -->
        <mapping resource="hibernate/model/Student.hbm.xml"/>
        
    </session-factory>

</hibernate-configuration>

7.将mysql驱动导入项目。

  1.    在项目中新建一个文件夹 
  2. 将驱动放入文件夹
  3. 选中驱动鼠标右键 build Path -->add

8.在mysql数据库中新建一个表 名为student  (int id PRIMARY KEY,verchar(20) name,int age)

9.在hibernate.model 包下新建一个测试类StudentTest

代码如下:

package hibernate.model;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class StudentTest {
public static void main(String[] args) {
	Student s= new Student();
	s.setId(1);
	s.setName("s1");
	s.setAge(12);
	Configuration cfg= new Configuration();
    SessionFactory sf=cfg.configure().buildSessionFactory();
	Session session=sf.openSession();
	session.beginTransaction();
	session.save(s);
	session.getTransaction().commit();
	session.close();
}
}

10.运行StudentTest,结果如下:

推荐阅读