第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
- 第5章 Hibernate的基本用法 5.2 Hibernate入门
- 第5章 HIbernate的基本用法 5.3Hibernate的体系结构
- 第5章Hibernate的基本用法 5.4深入Hibernate的配置文件
- 第5章 Hibernate的基本用法 5.1 ORM和Hibernate
- 第5章Hibernate的基本的用法5.5深入理解持久化对象
- 第5章Hibernate的基本用法 5.7映射组件属性
- 【Hibernate】(1)Hibernate的基本用法
- hibernate inverse 的基本用法
- Hibernate的一些基本用法
- hibernate入门的基本配置
- 第 2 章 Hibernate入门
- Hibernate Criteria 基本用法
- Hibernate基本用法:体系结构
- hibernate-基本用法1
- hibernate-基本用法2
- Hibernate的基本API的用法
- Hibernate之HQL的基本用法
- hibernate学习笔记 第 1 章 Hibernate入门
- 网址保存
- Asterisk[1]
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 2015061206 - python2的死期
- 堆排序
- 第5章 Hibernate的基本用法 5.2 Hibernate入门
- 对设计的感悟
- Asterisk-Java 教程(中文版) --FastAGI协议
- 9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
- 大数据高并发分布式架构发展
- SpringMVC多文件上传
- 安装IBM Data Studio Client
- MDK 文字乱码 解决办法
- 2015061207 - 踩在脚下