Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17491
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 211
  • 用 户 组: 普通用户
  • 注册时间: 2015-12-09 16:47
文章分类

全部博文(21)

文章存档

2017年(1)

2016年(9)

2015年(11)

我的朋友
最近访客

分类: Java

2015-12-11 09:31:38

首先强调我也是刚学三大框架的,对一些东西并不是很熟悉,所以也是一边学一边记录我的学习经过。由于struts2是很久就学的了,所以就没怎么去写它,不过后面应该会深入学习,那时候可能也会发一些涉及到原理的东西,现在我要讲的是学习hibernate。说道hibernate,就要先说说ORM框架,所谓的ORM(object/relation mapping)即对象关系映射,我们应该都了解原来的数据库模式是关系映射型数据库。而我们的开发语言都是面向对象的,所以这就带来了不便,于是为了更好地让开发者能够以面向对象的方式操纵数据库,ORM顺应而生。以下简单的说明怎么使用hibernate吧!首先必须要引入相关jar包。hibernate框架的入口是,hibernate.cfg.xml,其实也可以在hibernate.properties中配置,但我习惯使用hibernate.cfg.xml。
代码如下。

点击(此处)折叠或打开

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4.           "">

  5. <!-- Generated by MyEclipse Hibernate Tools. -->
  6. <hibernate-configuration>
  7.     <session-factory>
  8.         <!-- 定义数据库连接驱动 -->
  9.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  10.         <!-- 指定数据库连接方式 -->
  11.         <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
  12.         <property name="connection.username">root</property>
  13.         <property name="connection.password">root</property>
  14.         
  15.         <property name="hibernate.show_sql">true</property>
  16.         
  17.         <!-- org.hibernate.dialect.MySQL5InnoDBDialect -->
  18.         <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
  19.         <!-- <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> -->
  20.         <!-- 是否自动创建表 -->
  21.         <property name="hbm2ddl.auto">update</property>
  22.         
  23.         <mapping resource="model/hibernate-person.xml"/>
  24.     </session-factory>


  25. </hibernate-configuration>
在这里可能很多人都迷惑一点,既然是面向对象的,那怎么才能做到面向对象呢,其实hibernate将没一张表都作为一个对象,每一个字段都作为对象的属性,这样你对对象的crud就可以是对表的crud操作了。既然这样,那就需要一个xml告诉系统类与数据库的映射关系。于是上边的一句
就起到作用了。对应的内容如下。

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "">
  4. <hibernate-mapping>
  5.     <class name="model.News" table="news">
  6.         <id name="fdId" column="fd_id">
  7.             <generator class="uuid"></generator>
  8.         </id>
  9.         <property name="title" column="title"/>
  10.         <property name="content"/>
  11.         <property name="createTime"/>
  12.         <property name="createName"/>
  13.     </class>

  14. </hibernate-mapping>
对应的News类如下

点击(此处)折叠或打开

  1. package model;

  2. import java.util.Date;

  3. public class News {
  4.     private String fdId;
  5.     private String title;
  6.     private String content;
  7.     private Date createTime;
  8.     private String createName;
  9.     public String getFdId() {
  10.         return fdId;
  11.     }
  12.     public void setFdId(String fdId) {
  13.         this.fdId = fdId;
  14.     }
  15.     public String getTitle() {
  16.         return title;
  17.     }
  18.     public void setTitle(String title) {
  19.         this.title = title;
  20.     }
  21.     public String getContent() {
  22.         return content;
  23.     }
  24.     public void setContent(String content) {
  25.         this.content = content;
  26.     }
  27.     public Date getCreateTime() {
  28.         return createTime;
  29.     }
  30.     public void setCreateTime(Date createTime) {
  31.         this.createTime = createTime;
  32.     }
  33.     public String getCreateName() {
  34.         return createName;
  35.     }
  36.     public void setCreateName(String createName) {
  37.         this.createName = createName;
  38.     }
  39.     
  40.     

  41. }
上边的映射已经解决了,接下来就是实现crud的操作了,如下面的类

点击(此处)折叠或打开

  1. package test;

  2. import java.util.Date;

  3. import model.News;

  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.Transaction;
  6. import org.hibernate.cfg.Configuration;
  7. import org.hibernate.classic.Session;

  8. public class HibernateTest {
  9.     
  10.     
  11.     public static void main(String args[]){
  12.         //实例化hibernate配置文件对象

  13.         Configuration conf = new Configuration()
  14.              //加载hibernate.cfg.xml

  15.                  .configure();//获取到hibernate配置文件的

  16.         //创建会话工厂

  17.         SessionFactory sessionFactory = conf.buildSessionFactory();
  18.         //打开session

  19.         Session session = sessionFactory.openSession();
  20.         //开启事务

  21.         Transaction trans = session.beginTransaction();
  22.         News ne = new News();//瞬态
  23.         ne.setContent("this is a test!!!!");
  24.         ne.setCreateName("adminstrator");
  25.         ne.setCreateTime(new Date());
  26.         ne.setTitle("1222222");
  27.         session.save(ne);//此时的ne是持久化态
  28.         trans.commit();
  29.         session.close();//此时ne是脱管态
  30.         sessionFactory.close();
  31.         
  32.         
  33.         
  34.     }

  35. }
这样我们就可以查看一下整个hibernate的大致流程了
1:开发持久化类,即上面的映射文件和pojo类组成的。
2:根据hibernate.cfg.xml或者hibernate.properties创建Configuration对象,此对象是SessionFactory的生产工厂,通过他可以创建一个数据库内存镜像
3:SessionFactory 是创建session的工厂,也是数据库的内存镜像,而session是一个数据库连接的一次会话。
4:根据session开启事务
5:在事务中通过对对象的操作完成对数据库的操作。
6:关闭事务,关闭session。


 


 


阅读(198) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~