使用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) ;
    }

}

你可能感兴趣的:(java,oracle,Hibernate,MyEclipse,jdbc)