Hibernate搭建教程

来源:互联网 发布:得力指纹考勤软件 编辑:程序博客网 时间:2024/05/23 10:50

工程目录结构图:

所需要的jar包下载地址:http://download.csdn.net/detail/xiaorenwushibieren/8745063

首先我们用注解创建一个持久化类(Hibernate 是通过ORM持久化映射到关系数据库当中去的)New  并且映射到数据库表中(要去数据库手动创一张表) 

持久化类的要求:

Hibernate采用低侵入式设计  因此Hibernate 操作的持久化对象其实就是一个普通的java对象

但是还是应该遵守如下规则

1.提供一个无参构造器  因为通常为了Hibernate在运行时生成代理 构造器访问控制权限修饰符至少是包可见的

2.提供一个标识属性:标识属性通常是用来映射数据库表的主要字段

虽然可以没有这个属性  但是这样会导致很多Hibernate功能没办法使用 再说了  Hibernate可以以空类型来作为标识属性的类型 所以尽量避免用基本数据类型

News.java

package com.yang.demomain;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;/** *  * 功能描述: * * @author  杨鸿雕(Yanghd) * * <p>修改历史:(修改人,修改时间,修改原因)</p> *///声明orm持久化类@Entity//映射表@Table(name="news")public class News {//用于指定该类的标识属性(唯一可以识别该对象的属性)@Id//持久化注解   PO=POJO+持久化注解@GeneratedValue(strategy=GenerationType.IDENTITY)private Integer id;private String title;private String content;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getContent() {return content;}public void setContent(String contents) {this.content = content;}}
然后我们必须在配置文件当中设置数据库连接池    方言(HQL)  映射 等等  代码如下:

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"><hibernate-configuration>    <session-factory>        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="hibernate.connection.password">root</property>        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>        <property name="hibernate.connection.username">root</property>        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property><property name="hbm2ddl.auto">update</property><property name="show_sql">true</property>                <mapping class="com.yang.demomain.News"/>    </session-factory></hibernate-configuration>

接着 我们必须


获取Configuration

获取SessionFactory

 用面向对象的方式操作数据库
 关闭事物  关闭session



记住


如果.configure();里面没有参数  他就会默认指定hibernate.cfg.xml  如果在这个方法里加入路径参数就会指定其他文件

package com.yang.demomain;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;public class NewsManager  {/* * PO持久化对象       * 开发持久化类  由POJO+持久化注解组成 * 获取Configuration * 获取SessionFactory * 用面向对象的方式操作数据库 * 关闭事物  关闭session *  * PO(持久化对象)的三种状态 * 瞬态:如果PO实例  从未与Session关联过  该POC处于瞬态 * 持久化:如果PO与Session 关联过   而且 该实例对应到数据库记录   该实例处于持久状态 * 脱管:如果PO实例与Session关联起来  且因为Session关闭的原因 PO脱离了Session的管理   这种状态叫脱管 *  *  */    public static void main(String[] args) throws Exception{    Configuration conf =new Configuration()    //默认指定hibernate文件    .configure();    ServiceRegistry serviceregistry=new ServiceRegistryBuilder()    .applySettings(conf.getProperties()).build();    //创建SessionFactory    SessionFactory sf=conf.buildSessionFactory(serviceregistry);    //创建 Session    Session sess=sf.openSession();            //开始事务    Transaction tx=sess.beginTransaction();    //创建消息对象         News n=new News();        //设置消息和标题内容    n.setTitle("i love java!");        n.setContent("www.baidu.com");        sess.save(n);    tx.commit();        sess.close();    sf.close();                            }}



0 0