Myeclipse+Hibernate+Oracle小实例

来源:互联网 发布:js防止表单重复提交 编辑:程序博客网 时间:2024/05/22 03:20

精彩配置:

http://cache.baidu.com/c?m=9f65cb4a8c8507ed4fece763104c8c711923d030678197027fa3c215cc7919121b25b7a6777c4d51ce9e3a305eb2480bb9f56672300574b19dc9824edeec937b2f832633201d914165895ff09552609c60c655b6e44eb6e4ab70d2f9c5d3ae0e089a044526d3b6dc4d0065972fa3456fb4&p=86759a46d18152e80be29539584e&user=baidu

 

建表:

create table sun(

     id number(4) primary key,

    name  varchar2(20)

     )

 

hibernate.cfg.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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

 <session-factory>
  <property name="connection.username">sun</property>
  <property name="connection.url">
   jdbc:oracle:thin:@localhost:1521:SUNXF
  </property>
  <property name="dialect">
   org.hibernate.dialect.Oracle9Dialect
  </property>
  <property name="myeclipse.connection.profile">
   jdbc-oracle
  </property>
  <property name="connection.password">sunxf</property>
  <property name="connection.driver_class">
   oracle.jdbc.driver.OracleDriver
  </property>
  <mapping resource="Sun.hbm.xml" />

 </session-factory>

</hibernate-configuration>

 

sun.hbm.xml配置:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="Sun" table="SUN" schema="SUN">
        <id name="id" type="long">
            <column name="ID" precision="4" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="NAME" length="20" />
        </property>
    </class>
</hibernate-mapping>

 

javabean配置:


public class Sun {
 private long id;
 private String name;
 public Sun(){};
 public Sun(long text1,String text2){
  this.id=text1;this.name=text2;};
 public long getId() {
  return id;
 }
 public void setId(long id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }

}

 

测试用例test.java

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class test {
 public static void main(String[] args) {
  Session session = new Configuration()
        .configure().buildSessionFactory().openSession() ;
Transaction tx = session.beginTransaction();
for(int i=10;i<100;i++){
Sun sunxf=new Sun(i,"006");
System.out.println(session.save(sunxf));}
tx.commit();
session.close();
    }
}

 

原创粉丝点击