1. 程式人生 > >使用Hibernate向Oracle中插入資料

使用Hibernate向Oracle中插入資料

Hibernate是ORMapping的實現,通過一個類實現資料庫操作

例項:

hibernate.cfg.xml:Hibernate環境配置檔案

程式程式碼

<?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">system</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:MGC
</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="myeclipse.connection.profile">oracle</property>
<property name="connection.password">admin</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="show_sql">true</property>
<mapping resource="mgc/hibernate/test/Member.hbm.xml" />

</session-factory>

</hibernate-configuration>



Member.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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="mgc.hibernate.test.Member" table="MEMBER" schema="SYSTEM">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="assigned"></generator>
</id>
<property name="username" type="java.lang.String">
<column name="USERNAME" length="20" not-null="true" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" length="20" not-null="true" />
</property>
</class>
</hibernate-mapping>



Member.java:POJO類

程式程式碼

package mgc.hibernate.test;

public class Member {

private long id ;
private String username ;
private String password ;

public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}



MemberOperate.java:資料庫操作類

程式程式碼

package mgc.hibernate.test;

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

public class MemberOperate {
//所有的操作都是通過Session完成
private Session session = null ;

//在構造方法中例項化Session物件
public MemberOperate() {
//找到Hibernate配置
Configuration config = new Configuration().configure() ;
//從配置中取出SessionFactory
SessionFactory factroy = config.buildSessionFactory() ;
//取出一個Session
this.session = factroy.openSession() ;
}

//向資料庫中增加資料
public void insert(Member mem) {
//開始事務
Transaction tran = this.session.beginTransaction() ;
//執行語句
this.session.save(mem) ;
//提交事務
tran.commit() ;
}
}



TestPO.java:應用程式

程式程式碼

package mgc.hibernate.test;

public class TestPO {

/**
* @param args
*/
public static void main(String[] args) {
//生成POJO類例項化物件
Member mem = new Member() ;
mem.setId(11) ;
mem.setUsername("mgc") ;
mem.setPassword("000") ;
//例項化MemberOperate物件
MemberOperate mo = new MemberOperate() ;
mo.insert(mem) ;
}

}