orm环境构建及基本的demo

       Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的J2EE架构中取代CMP,完成数据持久化的重任。

       若要完成一个ORM的环境构建,需要一个版本比较高的myeclipse,我用的是myeclipse2014版本,开发过程中需要配置1.8版本的jdk,而且需要配置hibernate.cfg.xml文件和Customer.hbm.xml文件,然后导入hibrinate所需的jar包以及数据库驱动包。

    第一个demo创建过程:

1.利用mySQL创建数据库


首先创建一个java工程:hibernateDemo1  创建package:com.wangziq

2.创建类customer

package com.wzq;  
  
public class Customer {  
    private int id;  
    private String name;  
    private int idnumber;    
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public int getIdnumber() {  
        return idnumber;  
    }  
    public void setIdnumber(int idnumber) {  
        this.idnumber = idnumber;  
    }  


    @Override  
    public String toString() {  
        return "Customer [id=" + id + ", name=" + name + ", idnumber=" + idnumber 
                + "]";  
    }  
} 

3.配置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="hibernate.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">scott</property>  
        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>  
    <mapping  resource="com/wangziq/Customer.hbm.xml"/>  
    </session-factory>  
</hibernate-configuration>

4.配置Customer.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="wzq.Customer " table="wangziq">
        <id name="id" column="id" >
            <generator class="native"/>
        </id>
     <property name="name" column="name" type="string" />
     <property name="idnumber" column="idnumber" type="string" />
    </class>
</hibernate-mapping>

5.创建测试类TestDemo:

package com.wzq;  
import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.Transaction;  
import org.hibernate.cfg.Configuration;  
import org.junit.Test;  
public class TestDemo {  
    @Test  
        public  void sss() {  
            Configuration  cig = new Configuration().configure();  
            SessionFactory sessionFactory = cig.buildSessionFactory();  
            Session session = sessionFactory.openSession();  
            Transaction t = session.beginTransaction();  
            Customer c = (Customer) session.get(Customer.class, 1);   
            System.out.println(c.toString());  
            t.commit();  
            session.close();  
            sessionFactory.close();  
        }  
} 

测试结果


链接成功

   问题总结:由于之前的jdk版本为1.7,并非老师所要求的1.8版本。myeclipse为2014版本,链接数据库过程中因为密码输入错误而导致了数据库始终链接不上。导包时出现了一些问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值