1下载hibernate-3.6.0 Final.zip到任意目录,解压缩后得到hibernate目录
2下载slf4j-1.7.13.zip到任意目录,解压缩后得到slf4j-1.7.13
3操作数据库
sqlplus system/Oracle
创建表
create table Student
(
Student_ID number(6) NOT NULLPRIMARY KEY,
Student_Name varchar2(10) NOT NULL,
Student_Age number(2) NOT NULL
);
创建序列号用于给表Student的Student_ID赋值
CREATE SEQUENCEstudent_sequence
INCREMENT BY 1
START WITH 1000
NOMAXVALUE
NOCYCLE
CACHE 10;
4新建一个名为Hiber的工程
5添加包
添加hibernate\jar中的所有包
添加slf4j-1.7.13中的slf4j-nop-1.7.13.jar
添加oracle的jdbc驱动程序ojdbc6.jar
添加完成后
6 添加两个配置文件和两个类
(1)hibernate.cfg.xml
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <!--程序执行的时候是否显示真正的sql语句-->
- <property name="show_sql">true</property>
- <!--使用的SQL对应的“方言”,此处是Oracle11的“方言”-->
- <property name="dialect">org.hibernate.dialect.OracleDialect
- </property>
- <!--连接数据库的Driver-->
- <property name="connection.driver_class">
- oracle.jdbc.driver.OracleDriver
- </property>
- <!--数据库连接url-->
- <property name="connection.url">
- jdbc:oracle:thin:@localhost:1521:orcl
- </property>
- <!--用户名-->
- <property name="connection.username">system</property>
- <!--密码-->
- <property name="connection.password">oracle</property>
- <mapping resource="Student.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
(2)Student.Java
- public class Student
- {
- private int student_id;
- private String student_name;
- private int student_age;
- public int getStudent_id()
- {
- return student_id;
- }
- public String getStudent_name()
- {
- return student_name;
- }
- public int getStudent_age()
- {
- return student_age;
- }
- public void setStudent_id(int id)
- {
- this.student_id = id;
- }
- public void setStudent_name(String name)
- {
- this.student_name = name;
- }
- public void setStudent_age(int age)
- {
- this.student_age = age;
- }
- }
(3)Student.hbm.xml
- <?xml version="1.0"encoding="utf-8"?>
- <!DOCTYPEhibernate-mapping
- PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <classname="Student"table="Student">
- <idname="student_id"column="student_id"type="java.lang.Integer">
- <generatorclass="native">
- <paramname="sequence">student_sequence</param>
- </generator>
- </id>
- <propertyname="student_name"column="Student_Name"
- type="java.lang.String"/>
- <propertyname="student_age"column="Student_Age"
- type="java.lang.Integer"/>
- </class>
- </hibernate-mapping>
(4)Test.java
- importorg.hibernate.*;
- import org.hibernate.cfg.*;
- public class Test
- {
- public static voidmain(String[]args)
- {
- try
- {
- //通过Configuration获得一个SessionFactory对象
- SessionFactory sf = new Configuration().configure().buildSessionFactory();
- //打开一个Session
- Session session= sf.openSession();
- //开始一个事务
- Transaction tx =session.beginTransaction();
- //创建一个Student对象
- Student stu =new Student();
- //通过Student的setter方法改变它的属性
- //注意student_id不用我们设置
- stu.setStudent_name("zhangsan");
- stu.setStudent_age(18);
- //通过session的save()方法将Student对象保存到数据库中
- session.save(stu);
- //提交事务
- tx.commit();
- //关闭会话
- session.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
7 验证
(1)运行Test.java,结果为
Hibernate: select student_sequence.nextvalfrom dual
Hibernate: insert into Student(Student_Name, Student_Age, student_id) values (?, ?, ?)
(2)从Oracle数据库中查询