Hibernate(5.2.10)快速入门第一篇

 一、初识别Hibernate

                 Hibernate是一个开放源代码的ORM(对象关系映射)框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate

                可以自动生成SQL语句,自动执行,使得我们可以随心所欲的使用对象编程思维来操纵数据库, Hibernate可以应用在任何使用JDBC的场合。

二、ORM概述

在介绍Hibernate的时候,说了Hibernate是一种ORM的框架。那什么是ORM呢?ORM是一种思想

  • O代表的是Objcet
  • R代表的是Relative
  • M代表的是Mapping

ORM->对象关系映射....ORM关注是对象与数据库中的列的关系

三、

我们所要了解的是ORM的使用,所以我们需要了解一下ORM的环境构建以及一个基本的Demo的实现。

首先我们需要了解一下ORM的原理,所谓的ORM就是利用描述对象和数据库表之间映射的元数据,自动把Java应用程序中的对象,持久化到关系型数据库的表中。通过操作Java对象,就可以完成对数据库表的操作。

然后我们要了解一下ORM的环境构建:首先需要一个软件开发工具,eclipse/myeclipse都可以,在开发工具中要有一个jdk1.8的开发环境,然后需要一些有关Hibernate相关的Jar包进行程序驱动,还需要一个数据库(MYSQL或ORACLE,我所用的是MYSQL数据库)以便我们进行数据访问,而在程序编写时不仅需要一个实例化的具体类,还需要一个与该实体类所契合的*.hbm.xml文件.

第一个demo创建过程

创建Oracle数据库





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


引用相应jar包


创建类Customer:

[java]  view plain  copy
  1. package com.entity;  
  2.   
  3. public class Customer {  
  4.     private int id;  
  5.     private String username;  
  6.     private int age;  
  7.     private String sex;  
  8.     private String city;  
  9.     public int getId() {  
  10.         return id;  
  11.     }  
  12.     public void setId(int id) {  
  13.         this.id = id;  
  14.     }  
  15.     public String getUsername() {  
  16.         return username;  
  17.     }  
  18.     public void setUsername(String username) {  
  19.         this.username = username;  
  20.     }  
  21.     public int getAge() {  
  22.         return age;  
  23.     }  
  24.     public void setAge(int age) {  
  25.         this.age = age;  
  26.     }  
  27.     public String getSex() {  
  28.         return sex;  
  29.     }  
  30.     public void setSex(String sex) {  
  31.         this.sex = sex;  
  32.     }  
  33.     public String getCity() {  
  34.         return city;  
  35.     }  
  36.     public void setCity(String city) {  
  37.         this.city = city;  
  38.     }  
  39. }
配置Customer.hbm.xml文件:
[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!DOCTYPE hibernate-mapping PUBLIC  
  4.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  5.         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  6. <hibernate-mapping>  
  7.     <class name="com.entity.Customer" table="CUSTOMER">  
  8.       <id name="id" column="id" type="java.lang.Integer">
  9.         <generator class="increment"></generator>
  10.       </id>
  11.      <property name="username" column="username" type="string" />  
  12.      <property name="age" column="age" type="int" />  
  13.      <property name="sex" column="sex" type="string" />  
  14.      <property name="city" column="city" type="string" />  
  15.     </class>  
  16. </hibernate-mapping>  

配置hibernate.cfg.xml文件:

[html]  view plain  copy
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2.   
  3. <!DOCTYPE hibernate-configuration PUBLIC  
  4.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  5.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  6. <!-- Generated by MyEclipse Hibernate Tools.                   -->  
  7. <hibernate-configuration>  
  8.   
  9.     <session-factory>  
  10.         <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>  
  11.         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>  
  12.         <property name="connection.username">qxgl</property>  
  13.         <property name="connection.password">123</property>  
  14.         <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>  
  15.     <mapping  resource="com/entity/Customer.hbm.xml"/>  
  16.     </session-factory>  
  17. </hibernate-configuration>  
创建测试类 :TestDemo1:
[html]  view plain  copy
  1. package com.entity;  
  2. import org.hibernate.Session;  
  3. import org.hibernate.SessionFactory;  
  4. import org.hibernate.Transaction;  
  5. import org.hibernate.cfg.Configuration;  
  6. import org.junit.Test;  
  7. public class TestDemo {  
  8.     @Test  
  9.         public  void sss() {  
  10.             Configuration  cig = new Configuration().configure();  
  11.             SessionFactory sessionFactory = cig.buildSessionFactory();  
  12.             Session session = sessionFactory.openSession();  
  13.             Transaction t = session.beginTransaction();  
  14.             Customer c = (Customer) session.get(Customer.class, 1);   
  15.             System.out.println(c.toString());  
  16.             t.commit();  
  17.             session.close();  
  18.             sessionFactory.close();  
  19.         }  
  20. }  

运行结果:



  对于此次的Hibernate Demo的基本实现,我所遇到的问题就是有关于Java和数据库之间的连接问题,因为不同的数据库与项目之间的连接方式是不一样的。

  在搭建orm环境过程中,jdk版本不够,后来使用了老师发送的1.8jdk版本,最后是因为在数据库建表的时候添加的数据忘记提交,在连接数据库的过程中出现了一个问题查找了两个小时,以后还要认真仔细。

  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值