欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Hibernate环境搭建与配置方法(Hello world配置文件版)

程序员文章站 2024-03-09 08:02:34
本文实例讲述了hibernate环境搭建与配置方法。分享给大家供大家参考,具体如下: 1.下载hibernate jar包:hibernate-release-4.3.5...

本文实例讲述了hibernate环境搭建与配置方法。分享给大家供大家参考,具体如下:

1.下载hibernate jar包:hibernate-release-4.3.5.final,导入必要的jar包,路径为:hibernate-release-4.3.5.final\lib\required。

包含的jar包有10个。

2.建立新的java项目。

3.学习自己建立user library:

(a)项目右键——build path——configure build path——add library.
(b)选择user-library,在其中新建library,命名为hibernate。
(c)在library中加入hibernate所需要的jar包(路径为:hibernate-release-4.3.5.final\lib\required),hello world就够了,其他的还要加。

4.引入数据库的jdbc驱动。我用的mysql:mysql-connector-java-5.1.7-bin.jar

(a)创建数据库:

create database hibernate;

(b)切换数据库:

use hibernate;

(c)创建student表:

create table student(id int primary key,name varchar(20),age int);

5.建立hibernate的配置文件hibernate.cfg.xml,强烈建议在hibernate-release-4.3.5.final\documentation\manual\en-us\html_single路径下的帮助文档中copy。

地点:1.1.4. hibernate configuration。 内容修改后:

<?xml version='1.0' encoding='utf-8'?>
<!doctype hibernate-configuration public
  "-//hibernate/hibernate configuration dtd 3.0//en"
  "http://www.hibernate.org/dtd/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">xxx</property>
  <property name="connection.password">xxxx</property>
  <!-- jdbc connection pool (use the built-in) -->
  <!--
  <property name="connection.pool_size">1</property>
   -->
  <!-- sql dialect -->
  <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.internal.nocacheprovider</property>
  <!-- echo all executed sql to stdout -->
  <property name="show_sql">true</property>
  <!-- drop and re-create the database schema on startup -->
  <!--
  <property name="hbm2ddl.auto">update</property>
  -->
  <mapping resource="com/huxing/hibernate/model/student.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

建立student类:

public class student {
  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;
 }
}

建立student的映射文件:student.hbm.xml

<?xml version="1.0"?>
<!doctype hibernate-mapping public
  "-//hibernate/hibernate mapping dtd 3.0//en"
  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.huxing.hibernate.model">
 <class name="student" table="student">
  <id name="id" column="id">
  </id>
  <property name="name" type="string" column="name"/>
  <property name="age" type="int" column="age"/>
 </class>
</hibernate-mapping>

最后测试:

import org.hibernate.session;
import org.hibernate.sessionfactory;
import org.hibernate.cfg.configuration;
import com.huxing.hibernate.model.student;
public class studenttest {
 public static void main(string[] args) {
  student a = new student();
  a.setid(123);
  a.setage(32);
  a.setname("hello hibernate!");
  configuration cfg = new configuration();
  sessionfactory cf = cfg.configure().buildsessionfactory();
  session session = cf.opensession();
  session.begintransaction();
  session.save(a);
  session.gettransaction().commit();
  session.close();
  cf.close();
 }
}

希望本文所述对大家hibernate框架程序设计有所帮助。