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:
- package com.entity;
- public class Customer {
- private int id;
- private String username;
- private int age;
- private String sex;
- private String city;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public String getCity() {
- return city;
- }
- public void setCity(String city) {
- this.city = city;
- }
- }
- <?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="com.entity.Customer" table="CUSTOMER">
- <id name="id" column="id" type="java.lang.Integer">
- <generator class="increment"></generator>
- </id>
- <property name="username" column="username" type="string" />
- <property name="age" column="age" type="int" />
- <property name="sex" column="sex" type="string" />
- <property name="city" column="city" type="string" />
- </class>
- </hibernate-mapping>
配置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.Oracle10gDialect</property>
- <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
- <property name="connection.username">qxgl</property>
- <property name="connection.password">123</property>
- <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
- <mapping resource="com/entity/Customer.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
- package com.entity;
- 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();
- }
- }
运行结果:
对于此次的Hibernate Demo的基本实现,我所遇到的问题就是有关于Java和数据库之间的连接问题,因为不同的数据库与项目之间的连接方式是不一样的。
在搭建orm环境过程中,jdk版本不够,后来使用了老师发送的1.8jdk版本,最后是因为在数据库建表的时候添加的数据忘记提交,在连接数据库的过程中出现了一个问题查找了两个小时,以后还要认真仔细。