测试小例---Hibernate实现Oracle中数据的增删改查

来源:互联网 发布:apache加载php 编辑:程序博客网 时间:2024/06/05 21:09

参考源码: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();
  
 }
}

 

原创粉丝点击