简单记录Hibernate3.3.2如何快速配置环境

一、下载hibernate-distribution-3.3.2.GA-dist.zip文件,建立User libraries.

打开windows->preferences->JAVA->Build Path->User libraries。新建一个名为Hibernate的文件夹。然后添加hibernate-distribution-3.3.2.GA-dist.zip下的jar文件

第一个包在解压缩之后的文件夹下,剩下的JAR包除了倒数三个(用来配置DDL语句输出)和annotation包以外在lib->required文件夹下。

二、配置hibernate.cfg.xml配置文件(数据库为SqlServer2008 EXPRESS)

<?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.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://localhost:1433; DatabaseName=hibernate</property>
<property name="connection.username">aa</property>
<property name="connection.password">aa</property> <!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</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 -->
<property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property> </session-factory> </hibernate-configuration>

 三)使用JAVABEAN的hbm.xml配置:

1、建立Student类

package com.huohuo.model;

public class Student {
public int id;
public String name;
public int age;
public String grade; 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;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
} }

   2、配置Student.hbm.xml(注意该文件和Student类放在同一个文件夹下)

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.huohuo.model"> <class name="Student" table="student">
<id name="id" >
</id>
<property name="age"/>
<property name="name"/>
<property name="grade"/> </class> </hibernate-mapping>

     3、在hibernate.cfg.xml加入这样一句话

<!-- 使用Student.hbm.xml  -->
<mapping resource="com/huohuo/model/Student.hbm.xml"/>

配置完这三步以后,写一个测试类StudentTest.class

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; import com.huohuo.model.Student; public class StudentTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub Student s1 = new Student();
s1.setId(10);
s1.setName("huohuo2");
s1.setAge(23);
s1.setGrade("二年级"); Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(s1); session.getTransaction().commit();
session.close();
sf.close();
} }

  之后执行测试类,就会成功。以上几步是hibernate中自己写的类手动配置的步骤。

四)使用hibernate annotation配置方法(配置Annotaion文件)

加入两个包:

写一个Teacher类

package com.huohuo.model;

import javax.persistence.Entity;
import javax.persistence.Id; @Entity
public class Teacher {
public int id;
public String name;
public String title; @Id
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 String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
} }

  @是annotation的语法,@id表示主码

写一个测试类TeacherTest.class

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration; import com.huohuo.model.Teacher; public class TeacherTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub Teacher t = new Teacher();
t.setId(3);
t.setName("huohuo");
t.setTitle("低级教师"); Configuration cfg = new AnnotationConfiguration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
sf.close();
} }

 在hibernate.cfg.xml中加入两行代码:

<!-- 使用hibernate_annotation自动配置 -->
<mapping class="com.huohuo.model.Teacher"/>

  然后就可以直接运行了,省去了Teacher.hbm.xml映射的配置,方便很多。

Hibernate3.3.2 手动配置annotation环境的更多相关文章

  1. c++篇 cad.grx 入门,手动配置编译环境

    安装vs2010+sp1补丁; 安装浩辰2018(64位版本); 下载浩辰Grx开发的SDK,注意对应版本年份., 解压到E盘目录下, E:\grxsdk 在他们的官方用户群下载,搜sdk, 找到gr ...

  2. Ubuntu环境下手动配置Java环境

    /×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...

  3. RedHat6.2 x86手动配置LNMP环境

    因为公司要求用RedHat配,顺便让我练习一下Linux里面的操作什么的. 折腾来折腾去终于搞好了,其实也没那么难嘛.但是也要记录一下. 首先,是在服务器里面用VMware搭建的RedHat6.2 x ...

  4. 手动配置wnmp环境

    wamp 是什么? windows,nginx,mysql,php(当然也可以是PYTHON等) 只所以使用nginx,是因为我等下要配置ZendGuardLoader ZendGuardLoader ...

  5. 手动配置wamp环境(1)--apache安装与基本操作

    Apache服务器简介: Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一. 安装apac ...

  6. CentOS-8.3.2011-x86_64 配置网络环境的几个方案以及问题处理方法

    1. 在安装前的环境配置中配置网络 可以通过 NETWORK & HOST NAME 进行网络配置, 推介通过这里便捷设置. 如果在安装的 CentOS 之前的配置选项中没有进行用户和网络的配 ...

  7. Visual Studio IDE环境下利用模板创建和手动配置CUDA项目教程

    目前版本的cuda是很方便的,它的一个安装里面包括了Toolkit`SDK`document`Nsight等等,而不用你自己去挨个安装,这样也避免了版本的不同步问题. 1 cuda5.5的下载地址,官 ...

  8. 问题 |无法找到Python路径,需手动配置环境变量

    问题: 在命令行cmd输入Python,如果出现以下无法识别命令行的报错,说明在系统环境变量中无法找到对应之前安装的Python的路径,则需手动配置一下 怎么配置? 1.打开我的电脑——右键——属性— ...

  9. Linux环境下手动配置sbt

    一.下载sbt安装包 从sbt官网下载地址:http://www.scala-sbt.org/download.html下载安装包,以sbt-0.13.13.tgz为例. 二.安装 1.将下载的二进制 ...

随机推荐

  1. CMMI4级实践中的5个经典问题及解答

    这五个问题相当经典而且比较深,需要做过CMMI4.5级的朋友才能看懂这些问题.这5个问题是一位正在实践CMMI4级的朋友提出来的,而解答则是我的个人见解. 五个疑问是:   A.流程,子流程部分不明白 ...

  2. ArrayList

    各种原因,前两年做C语言去了,现在重新做JAVA, 感觉自己基础很不扎实,要好好学习啦, 先从简单的开始~ 以下内容基于jdk1.7.0_79源码: 什么是ArrayList 可以简单的认为是一个动态 ...

  3. Learning The Bash Shell读书笔记(整理)

    最近搞了一本书 Learning Bash Shell,发现有人已经写了阅读笔记,我就在这边整理一下 来自blog:http://blog.sina.com.cn/n4mine Learning Th ...

  4. maven-shade-plugin

    <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> ...

  5. H264解码学习-2015.04.16

    今天看了不少,却感觉收获寥寥. 1.H264相关知识 因为RTP协议发过来的数据已经经过了H264编码,所以这边需要解码.补充一下H264的相关知识. 与以往的视频压缩标准相比,H.264 视频压缩标 ...

  6. 走进云背后:微软Azure web 项目通过web service部署web site

    探索云那不为人知的故事(一):Web Services部署web site 前奏:Windows Azure是微软基于云计算的操作系统,现在更名为“Microsoft Azure”,和Azure Se ...

  7. linux的“自动化”

    h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...

  8. my_strlen()

    int my_strlen(const char* S){ int i=0; while('\0'!=*(S+i)){ i++; } return i; }

  9. shell脚本切割tomcat的日志文件

    鉴于在调试logback和log4j的文件切割一直无法成功,随性用shell写个脚本用来切割tomcat下的日志文件(大家如果有在logback或log4j使用文件切割成功的话,可以留下使用方式,先谢 ...

  10. 一、Android学习第一天——环境搭建(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 一. Android学习第一天——环境搭建 Android 开发环境的搭建 ...