hibernate3.5 HelloWorld(1)

下面我们开始写hibernate的第一个小程序,hibernate本身并不难,当然它内部实现很复杂,但是hibernte这个框架它的原理很简单,其实就是把你的Object映射成为相关的sql语句,把sql语句从数据库中查询的结果组装成对象,就这个意思。

下面来编写一个简单的Hibernate HelloWorld,如下就是project工程的目录结构以及所需jar文件:


 

1.在这里我们需要把Student存到数据库中去,因此我们首先编写Student.java

package com.bjsxt.hibernate;

public class Student {
	private int id;
	private String name;
	private int age;
	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;
	}	
}

2.在数据库中建立与Student对应的表,表结构如下

create table student(

   id number primary key,

   name varchar2(20),

   age number(3)

)

 

3.在src目录下新建hibernate.cfg.xml文件,从Hibernate Reference Documentation(注意:这个文档并不是所有的hibernate压缩文件中都会自带,因此你要下载自带该开发文档的压缩文件)中搜索hibernate.cfg.xml并copy到该文件中,然后注释调用其中暂时用不到的内容

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-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 -->
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:121:ORCL</property>
        <property name="connection.username">system</property>
        <property name="connection.password">niit</property>

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

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</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 -->
        <!--是否在console上显示数DML语句,注意:并不会显示DDL语句。如果需要显示DDL语句,需要搭建log4j日志环境-->
        <property name="show_sql">true</property>
        <!--格式化DML语句,注意:这个需要与show_sql搭配使用-->
        <property name="format_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <!--暂时不用<property name="hbm2ddl.auto">update</property>-->

        <mapping resource="org/bjsxt/model/Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

 

4.到这一步连接数据不缺了,hibernate自动帮我连接了。我们Student建了,数据表我们也建了,还缺哪个环节?还缺的环节是我这个实体类Student跟我们数据库到底是怎么关联的呢?实体类的那个属性对应表的那个字段呢?是不是还缺这个关联。

在Student.java同一目录下面建立student.hbm.xml (在hibernate的开发文档中搜索hbm.xml,然后copy改)

<!--实体类的配置文件约定俗成要和实体类在一起-->
<?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="com.bjsxt.model"><!--package指明你映射的是哪一个package里面的类-->
	<class name="Student" table="student"><!--table可以不用配置,在没有配置table的情况下,hibernate会默认我们的表名和我们的name(指定类名)一样是student,因为数据库的表名是不区分大小写的,这是表的映射-->
		<id name="id" column="id" /><!--name指定的是类中的属性id,column指定的是表中的字段id,当表中的字段和类中的属性相同时,column可以不写;如果实体类的属性和表的字段不相同则必须写,id同时也代表主键,字段的映射-->
		<property name="name" />
		<property name="age" />
    </class>
	
</hibernate-mapping>

 

5.创建测试类StudentTest.java

package com.bjsxt.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;//这里不要用经典的Session,即不要引入import org.hibernate.classic.Session,用最新的
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class StudentTest {
	public static void main(String[] args) {
		Student s = new Student();
		s.setId(1);
		s.setName("t1");
		s.setAge(22);
		//理想情况下session.save(t)就将我们的对象存入数据库了,当然你要想拿到这个session的话,首先我们要拿到Configuration对象,
		Configuration cfg=new  Configuration();/*创建一个Configuaration对象,Configuaration看它的名字你就应该知道,它就是用来读配置文件的
		,怎么读配置文件呢?cfg.configure()来读取配置文件。当cfg.configure(参数)的时候,可以用参数来指定你要读哪一个配置文件。如果你不指定
		的话,默认就找class根目录下面的hibernate.cfg.xml*/
		SessionFactory sf=cfg.configure().buildSessionFactory();/*cfg.configure()找class根目录下面的hibernate.cfg.xml,到这步,Configuration就已经把hibernate.cfg.xml文件解析了,解析完以后注意看configure的返回值仍然是一个Configuration,configure完以后返回的是一个已经拥有了配置选项的Configuration对象;调用buildSessionFactory
		方法产生了一个SessionFactory(session工厂)*/
         
		Session session=sf.openSession();//拿到session
     /*在hibernate里面,你的操作都应该放到一个事物(transaction)里面 ,所以说在session里面你要想执行操作呢?你必须执行session.beginTransaction(),
	 beginTransaction()还回的是一个transaction对象,*/
		session.beginTransaction();//还回值可以不用存起来
		session.save(s);/*执行save的时候,hibernate发现s你这个是一个student的类,那么它当然就会去找student这个类的配置文件,既然你要求我save
		这个对象,我当然看看属性和字段怎么对应*/
		session.getTransaction().commit();/*事物完成以后,要将事物提交,session.getTransaction()就拿到了session.beginTransaction()创建的
		事物(transaction)了,commit()将事物提交,这个时候数据库中就有提交的数据了*/
		session.close();
		sf.close();
	}
}

 

 

猜你喜欢

转载自weigang-gao.iteye.com/blog/2155797
今日推荐