Hibernate的基本使用

1. Hibernate包的下载(目前最新为hibernate4.3)

    官方下载地址:http://www.hibernate.org/downloads

    Hibernate完整版结构如下:documentation(帮助文档,API),lib(核心类库),project(全部源码),基本类库如下:

    lib/required/dom4j-1.6.1.jar    //XML配置和映射解释器

    lib/required/hibernate-commons-annotations-4.0.4.Final.jar   //常见的反射代码用于支持注解处理

    lib/required/hibernate-core-4.3.0.Final.jar   //Hibernate的核心模块

    lib/optional/ehcache/hibernate-ehcache-4.3.0.Final.jar //缓存工具.在没有提供其他缓存工具时,这个缓存工具是必不可少的

    lib/jpa/hibernate-entitymanager-4.3.0.Final.jar //Hibernate的核心库

    lib/required/hibernate-jpa-2.1-api-1.0.0.Final.jar //用来定义java持久性

    lib/required/javassist-3.18.1-GA.jar //代码生成工具Hibernate用它在运行时扩展java类和实现,同cglib包

    lib/required/jboss-logging-3.1.3.GA.jar //Jboss的日志框架

    由于4.3中Hibernate的事务处理相对以前版本的改动较大,并且在一般项目都会用spring的事务来代替hibernate。故本实例采用以前的版本来测试hibernate的基本使用。版本为Hibernate3.6,hibernate的包本身比较少,故在使用的时候建议导入lib下而所有的包。另外别忘了导数据库的驱动包。

2. 配置Hibernate配置文件(src/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/aaa</property>
        <!-- 指定连接数据库的用户名 -->
        <property name="connection.username">root</property>
        <!-- 指定连接数据库的密码 -->
        <property name="connection.password">root</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_period">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>
        <!-- 显示Hibernate持久化操作所生成的SQL -->
        <property name="show_sql">true</property>
        <!-- 将SQL脚本进行格式化后再输出 -->
        <property name="hibernate.format_sql">true</property>
        <!-- 罗列所有的映射文件 -->
        <mapping resource="test/hbm/News.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

3. 定义Model(src/test/model/News.java)

package test.model;

public class News{
    private Integer id;
    private String title;
    private String content;
    public void setId(Integer id){
        this.id = id;
    }
    public Integer getId(){
        return this.id;
    }
    public void setTitle(String title){
        this.title = title;
    }
    public String getTitle(){
        return this.title;
    }
    public void setContent(String content){
        this.content = content;
    }
    public String getContent(){
        return this.content;
    }
}

4. 配置XML映射文件(src/test/hbm/hibernate.cfg.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="test.model">
    <!-- 每个class元素对应一个持久化对象 -->
    <class name="News" table="news_table">
        <!-- id元素定义持久化类的标识属性,不加column属性时列名默认和model属性相同-->
        <id name="id" column="id">
            <!-- 指定主键生成策略,identity为自增长-->
            <generator class="identity"/>
        </id>
        <!-- property元素定义常规属性 -->
        <property name="title" column="news_title"/>
        <property name="content" column="news_content"/>
    </class>
</hibernate-mapping>

5. 测试类

package test;

import org.hibernate.*;
import org.hibernate.cfg.*;
import test.model.News;

public class NewsManager{
    public static void main(String[] args)throws Exception{
        //实例化Configuration,此处自动加载hibernate.cfg.xml文件
        Configuration conf = new Configuration().configure();
        //以Configuration创建SessionFactory
        SessionFactory sf = conf.buildSessionFactory();
        //创建Session
        Session sess = sf.openSession();
        //开始事务
        Transaction tx = sess.beginTransaction();
        //创建一条消息
        News n = new News();
        n.setTitle("222");
        n.setContent("222");
        //保存消息
        sess.save(n);
        //提交事务
        tx.commit();
        //关闭Session
        sess.close();
        sf.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值