Hibernate环境搭建

本文详细介绍了如何搭建Hibernate开发环境,包括所需资源的下载及配置步骤,并通过一个具体的例子展示了Hibernate的基本使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先是准备环境搭建需要的几个资源:

  

1,   hibernate-distribution-3.3.2.GA   (hibernate核心包)

下载链接:http://download.youkuaiyun.com/detail/minger2008/4348601

2,   hibernate-annotations-3.4.0.GA   (annotations核心包)

下载链接:http://download.youkuaiyun.com/detail/hetao0921/3496263

3,   slf4j-1.5.8  (slf4j 实现库,虽然hibernate核心包中有slf4j的jar包,但是只是api包没有实现)

下载链接:http://download.youkuaiyun.com/detail/xujianqiang8659/2621774

4,   mysql-connector-java-3.1.14    (mysql 驱动)

下载链接:http://download.youkuaiyun.com/detail/wodwl/1520607

 

在Eclipse 里面创建一个Java Project 名字你随意好了,

然后在window->preference->User Libraries中new 一个新的库,名字随意,包含hibernate所需要的包,我这里叫做myhibernate

有以下这几个 ,然后add 这个 user library

然后右键工程Build Path->add External Archives 将 数据库的驱动装上 我这是使用的是mysql的数据库

我这里用的是上面链接下载的mysql-connector-java-3.1.14-bin.jar

Mysql准备

现在创建如下图的文件(按照目录格式)

Teacher.java

package com.test;

public class Teacher {
 private int id;
 private String name;
 private int age;
 
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }

Teacher.hbm.xml中的内容

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping package="com.test"><!-- 路径 -->
    <class name="Teacher">   <!-- 映射的类名 -->
        <id name ="id"/>     <!-- 映射的数据库primary key -->
        <!-- 下面都是映射我们的其他属性,如果你的数据库中的字段名相同则使用以下形式(必须和数据库完全一致)-->
        <property name="name"/>
        <property name="age"/>

    </class>
</hibernate-mapping>

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>
 
        <!-- Database connection settings 数据库的配置 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">admin</property>
 
        <!-- JDBC connection pool (use the built-in)  hibernate自带连接池,暂不使用 -->
        <!-- <property name="connection.pool_size">1</property> -->
 
        <!-- SQL dialect 数据库方言,这里我们才爱用MySQL-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
 
        <!-- Enable Hibernate's automatic session context management 新功能,暂不使用 -->
        <!-- <property name="current_session_context_class">thread</property> -->
 
        <!-- Disable the second-level cache  二级缓存,放置不管 -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
 
        <!-- Echo all executed SQL to stdout  设置show_sql为true表示让hibernate将生成sql语句在控制台打印出来 -->
        <property name="show_sql">true</property>
 
        <!-- Drop and re-create the database schema on startup 是否让hibernate自动为我们创建表 -->
        <!-- <property name="hbm2ddl.auto">update</property> -->
 
        <mapping resource="com/test/Teacher.hbm.xml"/> <!-- 这里是将需要mapping的文件进行再次声明 -->
 
    </session-factory>
 
</hibernate-configuration>

这些配置文件,可以去hibernate的demo中copy,通常都是copy改(这里上面的注释我也是从别的地方copy的)

 
}

看最后一句

<mapping resource="com/test/Teacher.hbm.xml"/> <!-- 这里是将需要mapping的文件进行再次声明 -->

映射文件的引入

MainTest.java

package org.hibernate.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.test.Teacher;

 

public class MainTest {
 public static void main(String[] args){
  Teacher teacher = new Teacher();
  
  teacher.setId(2);
  teacher.setName("swj_Test");
  teacher.setAge(25);
  
  Configuration cfg = new Configuration();
  SessionFactory sf = cfg.configure().buildSessionFactory();
  System.out.println("buildSessionFactory");
  Session ss = sf.openSession();
  System.out.println("openSession");
  ss.beginTransaction();
  System.out.println("beginTransaction");
  ss.save(teacher);
  System.out.println("save");
  ss.getTransaction().commit();
  System.out.println("getTransaction().commit()");
  ss.close();
  sf.close();
  
  
  System.out.println("end");
  
 }
}

右键工程run as app OK 可以运行了 看控制台的log

buildSessionFactory
openSession
beginTransaction
save
Hibernate: insert into Teacher (name, age, id) values (?, ?, ?)
getTransaction().commit()
end

OK 你懂得的

http://www.himigame.com/hibernate/783.html 借鉴了这了的大部分内容,但是对于资源的找去,和其他工具的使用,稍微改一点,算是原创吧,请原作者不要见怪,谢谢~!

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值