測試小例---Hibernate實現Oracle中資料的增刪改查____Oracle

來源:互聯網
上載者:User

參考源碼: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();
  
 }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.