第5章 Hibernate的基本用法 5.2 Hibernate入门

来源:互联网 发布:网络文学社简介 编辑:程序博客网 时间:2024/04/28 18:42

5.2.1 Hibernate下载和安装

官网:www.hibernate.org、www.jboss.org

5.2.2 Hibernate数据库操作

PO(Persistent Object,持久化对象)。持久化对象的作用是完成持久化操作,简单地说,通过该对象可对数据执行增、删、改的操作——以面向对象的方式操作数据库。

1.POJO类 News.java

public class News{//消息类的标识属性private Integer id;//消息标题private String title;//消息内容private String content;//id属性的setter和getter方法public void setId(Integer id){this.id = id;}public Integer getId(){return this.id;}//title属性的setter和getter方法public void setTitle(String title){this.title = title;}public String getTitle(){return this.title;}//content属性的setter和getter方法public void setContent(String content){this.content = content;}public String getContent(){return this.content;}}
2.XML映射文件 News.hbm.xml

<?xml version="1.0" encoding="gb2312"?><!-- 指定Hiberante3映射文件的DTD信息 --><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><!-- hibernate-mapping是映射文件的根元素 --><hibernate-mapping package="org.crazyit.app.domain"><!-- 每个class元素对应一个持久化对象 --><class name="News" table="news_table"><!-- id元素定义持久化类的标识属性 --><id name="id"><!-- 指定主键生成策略 --><generator class="identity"/></id><!-- property元素定义常规属性 --><property name="title"/><property name="content"/></class></hibernate-mapping>
3.Hibernate配置文件 hibernate.cfg.xml

<?xml version="1.0" encoding="GBK"?><!-- 指定Hibernate配置文件的DTD信息 --><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><!-- hibernate- configuration是连接配置文件的根元素 --><hibernate-configuration><session-factory><!-- 指定连接数据库所用的驱动 --><property name="connection.driver_class">com.mysql.jdbc.Driver</property><!-- 指定连接数据库的url,hibernate连接的数据库名 --><property name="connection.url">jdbc:mysql://localhost/hibernate</property><!-- 指定连接数据库的用户名 --><property name="connection.username">root</property><!-- 指定连接数据库的密码 --><property name="connection.password">32147</property><!-- 指定连接池里最大连接数 --><property name="hibernate.c3p0.max_size">20</property><!-- 指定连接池里最小连接数 --><property name="hibernate.c3p0.min_size">1</property><!-- 指定连接池里连接的超时时长 --><property name="hibernate.c3p0.timeout">5000</property><!-- 指定连接池里最大缓存多少个Statement对象 --><property name="hibernate.c3p0.max_statements">100</property><property name="hibernate.c3p0.idle_test_perxiod">3000</property><property name="hibernate.c3p0.acquire_increment">2</property><property name="hibernate.c3p0.validate">true</property><!-- 指定数据库方言 --><property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property><!-- 根据需要自动创建数据表 --><property name="hbm2ddl.auto">update</property><!-- 显示Hibernate持久化操作所生成的SQL --><property name="show_sql">true</property><!-- 将SQL脚本进行格式化后再输出 --><property name="hibernate.format_sql">true</property><!-- 罗列所有的映射文件 --><mapping resource="org/crazyit/app/domain/News.hbm.xml"/></session-factory></hibernate-configuration>
Hibernate的配置文件的默认文件名为hibernate.cfg.xml,当程序调用Configuration对象的configure()方法时,Hibernate将自动加载该文件。
数据源会负责维持一个数据连接池,当程序创建数据源实例时,系统会一次性的创建多个数据库连接,并把这些数据库连接保存在连接池中。

hbm2ddl.auto属性,该属性指定是否需要Hibernate根据映射文件来自动创建数据表——本应用指定update,即表示Hibernate会根据映射文件创建数据表。

4.下面是完成消息插入的代码  NewsManager.java

public class NewsManager{public static void main(String[] args)throws Exception{//实例化Configuration,Configuration conf = new Configuration()//下面方法默认加载hibernate.cfg.xml文件.configure();//以Configuration创建SessionFactorySessionFactory sf = conf.buildSessionFactory();//创建SessionSession sess = sf.openSession();//开始事务Transaction tx = sess.beginTransaction();//创建消息实例News n = new News();//设置消息标题和消息内容n.setTitle("疯狂Java联盟成立了");n.setContent("疯狂Java联盟成立了,"+ "网站地址http://www.crazyit.org");//保存消息sess.save(n);//提交事务tx.commit();//关闭Sessionsess.close();sf.close();}}
为了使用Hibernate进行持久化操作,通常有如下操作步骤。

1.开发持久化类,有POJO加映射文件组成。

2.获取Configuration

3.获取SessionFactory(Hibernate的SessionFactory是数据库编译后的内存镜像,通常一个应用对应一个SessionFactory对象)

4.获取Session

5.用面向对象的方式操作数据库

6.关闭事务,关闭Session

随PO与Session的关联关系,PO可有如下三种状态。

瞬态:PO实例从未与Session关联过,该PO实例处于瞬态状态。

持久化:PO实例与Session关联起来,且该实例对应到数据库记录,则该实例处于持久化状态。

脱管:PO实例曾经与Session关联过,但因为Session的关闭等原因,PO实例脱离了Session的管理,这种状态被称为脱管状态。







0 0
原创粉丝点击