參考源碼:http://download.csdn.net/detail/guoquanyou/3614666
在Oracle資料庫中添加表student
create table Student(
Student_ID number(6) NOT NULL PRIMARY KEY,
Student_Name varchar2(10) NOT NULL,
Student_Age number(2) NOT NULL
);
定義自增SEQUENCE
CREATE SEQUENCE student_sequence
INCREMENT BY 1
START WITH 1000
NOMAXVALUE
NOCYCLE
CACHE 10;
項目目錄結構圖:
配置hibernate.xml檔案
<?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>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:orcl
</property>
<property name="connection.username">scott</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="myeclipse.connection.profile">oracle5</property>
<property name="show_sql">true</property>
<mapping resource="entity/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
實體類Student
package entity;
public class Student implements java.io.Serializable {
private Integer studentId;
private String studentName;
private Byte studentAge;
public Student() {
}
public Student(String studentName, Byte studentAge) {
this.studentName = studentName;
this.studentAge = studentAge;
}
public Integer getStudentId() {
return this.studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return this.studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Byte getStudentAge() {
return this.studentAge;
}
public void setStudentAge(Byte studentAge) {
this.studentAge = studentAge;
}
}
實體對應檔Student.hbm.xml
<?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" schema="SCOTT">
<id name="studentId" type="java.lang.Integer">
<column name="STUDENT_ID" precision="6" scale="0" />
<generator class="sequence" >
<param name="sequence">student_sequence</param>
</generator>
</id>
<property name="studentName" type="java.lang.String">
<column name="STUDENT_NAME" length="10" not-null="true" />
</property>
<property name="studentAge" type="java.lang.Byte">
<column name="STUDENT_AGE" precision="2" scale="0" not-null="true" />
</property>
</class>
</hibernate-mapping>
資料的查詢與顯示
package test;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import entity.Student;
//異常處理未添加,請各位自行添加
public class Test {
//查詢學生資訊
public void query(){
Session session=HibernateSessionFactory.getSession();
Query query=session.createQuery("from Student");
List list=query.list();
for (Object object : list) {
Student student=(Student)object;
System.out.println(student.getStudentName()+" "+student.getStudentAge());
}
session.close();
}
//儲存學生的資訊
public void save(){
Session session=HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
Student student=new Student("張三丰",(byte) 28);
session.save(student);
tx.commit();
session.close();
}
//修改學生的資訊
public void update(){
Session session=HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
Student student=(Student) session.get(Student.class, 1000);
student.setStudentName("jCuckoo");
session.update(student);
tx.commit();
session.close();
}
//刪除學生的資訊
public void delete(){
Session session=HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
Student student=(Student) session.get(Student.class, 1000);
session.delete(student);
tx.commit();
session.close();
}
//增刪改查測試
public static void main(String[] args) {
Test test=new Test();
test.save();
System.out.println("******************");
test.query();
System.out.println("******************");
test.update();
test.query();
System.out.println("******************");
test.delete();
test.query();
}
}