Hibernate框架简介 和 一定运行不起来的Demo

版权声明:本文为Banana原创文章,未经Banana允许不得转载。评论请留下你认真观看后的想法或者意见,非常感谢! https://blog.csdn.net/qq1515312832/article/details/85124446

Hibernate简介

内容不全面,持续更新...

Hibernate是什么?

Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,Hibernate可以在应用EJB的JaveEE架构中取代CMP,完成数据持久化的重任。

总的来说,Hibernate是一个持久层的ORM框架。

 

什么是ORM?

对象关系映射,Object-Relational Mapping,用于编程语言中的对象和数据库里的关系进行转换,也就是说在项目中的对象对应着数据库里面的表。

                                     

在java中是如何使用的:

 

更高级的视图:

 

Hibernate下载

点击此处        提取码:6otj 

我画的图

Demo

引入jar包

1.数据库驱动包

2.Hibernate的必须jar包

3.Hibernate的日志记录包

在数据库中创建所需要的数据表

 

创建Model

package com.Demo.Model;

public class Stu
       private int id;
       private String name;
       private int age;
       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 getAge() {
              return age;
       }
       public void setAge(int age) {
              this.age = age;
       }     

}

 

用xml来进行映射

stu.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
       <class name="com.hibernate.demo.Stu" table="t_stu">
              <id name="id" column="id">
                     <generator class="native" />
              </id>
              <property name="name" column="name" />
              <property name="age" column="age" />
       </class>
</hibernate-mapping>

 

Hibernate配置xml

Hibernage.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
       "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
       "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
       <session-factory>
              <!-- 链接数据库的基本参数 -->
              <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
              <property name="hibernate.connection.url">jdbc:mysql:///Student</property>
              <property name="hibernate.connection.username">root</property>
              <property name="hibernate.connection.passord">abc</property>
              <!-- 配置Hibernate的方言 -->
              <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
              <mapping resource="com/hibernate/demo/stu.hbm.xml"
       </session-factory>
</hibernate-configuration>

 

测试类

HibernateDemo01.java


package com.hibernate.demo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class HibernateDemo01 {

       @Test
       public void Demo01(){
              //1.加载Hibernate的核心配置文件
              Configuration configuration=new Configuration().configure();

              //2.创建一个SessionFactory对象:类似于JDBC中连接池
              SessionFactory sessionFactory=configuration.buildSessionFactory();

              //3.通过SessionFactory获取到Session对象:类似于JDBC中Connection
              Session session=sessionFactory.openSession();

              //4.手动开启事务
              Transaction transaction = session.beginTransaction();

              //5.编写代码
              Stu stu=new Stu();
              stu.setName("zhangsan");
              session.save(stu);

              //6.事务提交
              transaction.commit();

              //7.资源释放
              session.close();
       }
}

总结

初次学习,必定有些问题,还望大家多多指出,另外为什么是一定运行不起来,这里是把一个Demo改编了一下,然后变了数据库,然后可能是由于版本问题,死活连不上数据库,于是这个Demo是中规中矩的,但是却没有连接数据库测试。

 

猜你喜欢

转载自blog.csdn.net/qq1515312832/article/details/85124446
今日推荐