简单记录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)

  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5.  
  6. <hibernate-configuration>
  7.  
  8. <session-factory>
  9.  
  10. <!-- Database connection settings -->
  11. <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
  12. <property name="connection.url">jdbc:sqlserver://localhost:1433; DatabaseName=hibernate</property>
  13. <property name="connection.username">aa</property>
  14. <property name="connection.password">aa</property>
  15.  
  16. <!-- JDBC connection pool (use the built-in) -->
  17. <!-- <property name="connection.pool_size">1</property> -->
  18.  
  19. <!-- SQL dialect -->
  20. <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
  21.  
  22. <!-- Enable Hibernate's automatic session context management -->
  23. <!-- <property name="current_session_context_class">thread</property> -->
  24.  
  25. <!-- Disable the second-level cache -->
  26. <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
  27.  
  28. <!-- Echo all executed SQL to stdout -->
  29. <property name="show_sql">true</property>
  30.  
  31. <!-- Drop and re-create the database schema on startup -->
  32. <property name="hbm2ddl.auto">create</property>
  33.  
  34. </session-factory>
  35.  
  36. </hibernate-configuration>

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

1、建立Student类

  1. package com.huohuo.model;
  2.  
  3. public class Student {
  4. public int id;
  5. public String name;
  6. public int age;
  7. public String grade;
  8.  
  9. public int getId() {
  10. return id;
  11. }
  12. public void setId(int id) {
  13. this.id = id;
  14. }
  15. public String getName() {
  16. return name;
  17. }
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21. public int getAge() {
  22. return age;
  23. }
  24. public void setAge(int age) {
  25. this.age = age;
  26. }
  27. public String getGrade() {
  28. return grade;
  29. }
  30. public void setGrade(String grade) {
  31. this.grade = grade;
  32. }
  33.  
  34. }

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

  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5.  
  6. <hibernate-mapping package="com.huohuo.model">
  7.  
  8. <class name="Student" table="student">
  9. <id name="id" >
  10. </id>
  11. <property name="age"/>
  12. <property name="name"/>
  13. <property name="grade"/>
  14.  
  15. </class>
  16.  
  17. </hibernate-mapping>

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

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

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

  1. import org.hibernate.Session;
  2. import org.hibernate.SessionFactory;
  3. import org.hibernate.cfg.Configuration;
  4.  
  5. import com.huohuo.model.Student;
  6.  
  7. public class StudentTest {
  8.  
  9. /**
  10. * @param args
  11. */
  12. public static void main(String[] args) {
  13. // TODO Auto-generated method stub
  14.  
  15. Student s1 = new Student();
  16. s1.setId(10);
  17. s1.setName("huohuo2");
  18. s1.setAge(23);
  19. s1.setGrade("二年级");
  20.  
  21. Configuration cfg = new Configuration();
  22. SessionFactory sf = cfg.configure().buildSessionFactory();
  23. Session session = sf.openSession();
  24. session.beginTransaction();
  25. session.save(s1);
  26.  
  27. session.getTransaction().commit();
  28. session.close();
  29. sf.close();
  30. }
  31.  
  32. }

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

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

加入两个包:

写一个Teacher类

  1. package com.huohuo.model;
  2.  
  3. import javax.persistence.Entity;
  4. import javax.persistence.Id;
  5.  
  6. @Entity
  7. public class Teacher {
  8. public int id;
  9. public String name;
  10. public String title;
  11.  
  12. @Id
  13. public int getId() {
  14. return id;
  15. }
  16. public void setId(int id) {
  17. this.id = id;
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. public String getTitle() {
  26. return title;
  27. }
  28. public void setTitle(String title) {
  29. this.title = title;
  30. }
  31.  
  32. }

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

写一个测试类TeacherTest.class

  1. import org.hibernate.Session;
  2. import org.hibernate.SessionFactory;
  3. import org.hibernate.cfg.AnnotationConfiguration;
  4. import org.hibernate.cfg.Configuration;
  5.  
  6. import com.huohuo.model.Teacher;
  7.  
  8. public class TeacherTest {
  9.  
  10. /**
  11. * @param args
  12. */
  13. public static void main(String[] args) {
  14. // TODO Auto-generated method stub
  15.  
  16. Teacher t = new Teacher();
  17. t.setId(3);
  18. t.setName("huohuo");
  19. t.setTitle("低级教师");
  20.  
  21. Configuration cfg = new AnnotationConfiguration();
  22. SessionFactory sf = cfg.configure().buildSessionFactory();
  23. Session session = sf.openSession();
  24. session.beginTransaction();
  25. session.save(t);
  26. session.getTransaction().commit();
  27. session.close();
  28. sf.close();
  29. }
  30.  
  31. }

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

  1. <!-- 使用hibernate_annotation自动配置 -->
  2. <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. 体验最火的敏捷——SCRUM(厦门,2014.1.4)

    1.概述SCRUM是当前最火的一种敏捷开发方法,有用户故事.冲刺.燃尽图等很多很酷的玩法,有牛B的产品负责人.SCRUM Master,有超强的自组织团队.本沙龙将为您展现当前最火最酷的敏捷开发方法! ...

  2. Spring为某个属性注入值或为某个方法的返回值

    项目中用到需要初始化一些数据,Spring提供了filed的值注入和method的返回值注入. 一.Field值的注入 filed值注入需要使用org.springframework.beans.fa ...

  3. JavaSpring

    http://www.cnblogs.com/suoning/p/5656403.html   1.序列化 JSON.stringify(obj)   序列化 JSON.parse(str)     ...

  4. Cross-Origin Resource Sharing协议介绍

    传统的Ajax请求只能获取在同一个域名下面的资源,但是HTML5打破了这个限制,允许Ajax发起跨域的请求.浏览器是可以发起跨域请求的,比如你可以外链一个外域的图片或者脚本.但是Javascript脚 ...

  5. 记一次ORACLE的UNDO表空间爆满分析过程

    这篇文章是记录一次ORACLE数据库UNDO表空间爆满的分析过程,主要整理.梳理了同事分析的思路.具体过程如下所示: 早上收到一数据库服务器的UNDO表空间的告警邮件,最早一封是7:55发出的(监控作 ...

  6. Oracle行内链接不会引起USER_TABLES中CHAIN_CNT值变化

    前几天和群里网友讨论一个关于行内链接(intra-block chaining)的问题,问题非常有意思,恰好今天有空,顺便整理了一下这些知识点. 问题描述:下面SQL,创建一个超过255列的表(实际为 ...

  7. 文件件监听器,android系统拍照功能调用后删除系统生成的照片

    先说说要实现的功能: android调用系统拍照功能实时 预览 删除 上传 保存 (用户不能再本地文件夹中看到拍的照片) 再说说遇到的问题: 1.调用系统拍照在系统自带的拍照文件夹中生成一张随机命名图 ...

  8. CSS:布局的三个关键属性:float、position、display

    最近在出差,就我一个在这里.客户要做几个页面,涉及到了页面的布局问题,没办法自己得做了.然后就遇到了一些问题.页面不论怎么都不能按照设想的布局. 以前也没有做过网页布局方面的工作.上网上找类似的例子, ...

  9. SQL Server调优系列基础篇(并行运算总结)

    前言 上三篇文章我们介绍了查看查询计划的方式,以及一些常用的连接运算符.联合运算符的优化技巧. 本篇我们分析SQL Server的并行运算,作为多核计算机盛行的今天,SQL Server也会适时调整自 ...

  10. Bootstrap模态框(modal)垂直居中

    http://v3.bootcss.com/ 自己也试了改了几种方式也不容乐观,发现在窗口弹出之前是获取不到$(this).height()的值,本想着是用($(window).height()-$( ...