Hibernate3.3.2 手动配置annotation环境
简单记录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环境的更多相关文章
- c++篇 cad.grx 入门,手动配置编译环境
安装vs2010+sp1补丁; 安装浩辰2018(64位版本); 下载浩辰Grx开发的SDK,注意对应版本年份., 解压到E盘目录下, E:\grxsdk 在他们的官方用户群下载,搜sdk, 找到gr ...
- Ubuntu环境下手动配置Java环境
/×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...
- RedHat6.2 x86手动配置LNMP环境
因为公司要求用RedHat配,顺便让我练习一下Linux里面的操作什么的. 折腾来折腾去终于搞好了,其实也没那么难嘛.但是也要记录一下. 首先,是在服务器里面用VMware搭建的RedHat6.2 x ...
- 手动配置wnmp环境
wamp 是什么? windows,nginx,mysql,php(当然也可以是PYTHON等) 只所以使用nginx,是因为我等下要配置ZendGuardLoader ZendGuardLoader ...
- 手动配置wamp环境(1)--apache安装与基本操作
Apache服务器简介: Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一. 安装apac ...
- CentOS-8.3.2011-x86_64 配置网络环境的几个方案以及问题处理方法
1. 在安装前的环境配置中配置网络 可以通过 NETWORK & HOST NAME 进行网络配置, 推介通过这里便捷设置. 如果在安装的 CentOS 之前的配置选项中没有进行用户和网络的配 ...
- Visual Studio IDE环境下利用模板创建和手动配置CUDA项目教程
目前版本的cuda是很方便的,它的一个安装里面包括了Toolkit`SDK`document`Nsight等等,而不用你自己去挨个安装,这样也避免了版本的不同步问题. 1 cuda5.5的下载地址,官 ...
- 问题 |无法找到Python路径,需手动配置环境变量
问题: 在命令行cmd输入Python,如果出现以下无法识别命令行的报错,说明在系统环境变量中无法找到对应之前安装的Python的路径,则需手动配置一下 怎么配置? 1.打开我的电脑——右键——属性— ...
- Linux环境下手动配置sbt
一.下载sbt安装包 从sbt官网下载地址:http://www.scala-sbt.org/download.html下载安装包,以sbt-0.13.13.tgz为例. 二.安装 1.将下载的二进制 ...
随机推荐
- 体验最火的敏捷——SCRUM(厦门,2014.1.4)
1.概述SCRUM是当前最火的一种敏捷开发方法,有用户故事.冲刺.燃尽图等很多很酷的玩法,有牛B的产品负责人.SCRUM Master,有超强的自组织团队.本沙龙将为您展现当前最火最酷的敏捷开发方法! ...
- Spring为某个属性注入值或为某个方法的返回值
项目中用到需要初始化一些数据,Spring提供了filed的值注入和method的返回值注入. 一.Field值的注入 filed值注入需要使用org.springframework.beans.fa ...
- JavaSpring
http://www.cnblogs.com/suoning/p/5656403.html 1.序列化 JSON.stringify(obj) 序列化 JSON.parse(str) ...
- Cross-Origin Resource Sharing协议介绍
传统的Ajax请求只能获取在同一个域名下面的资源,但是HTML5打破了这个限制,允许Ajax发起跨域的请求.浏览器是可以发起跨域请求的,比如你可以外链一个外域的图片或者脚本.但是Javascript脚 ...
- 记一次ORACLE的UNDO表空间爆满分析过程
这篇文章是记录一次ORACLE数据库UNDO表空间爆满的分析过程,主要整理.梳理了同事分析的思路.具体过程如下所示: 早上收到一数据库服务器的UNDO表空间的告警邮件,最早一封是7:55发出的(监控作 ...
- Oracle行内链接不会引起USER_TABLES中CHAIN_CNT值变化
前几天和群里网友讨论一个关于行内链接(intra-block chaining)的问题,问题非常有意思,恰好今天有空,顺便整理了一下这些知识点. 问题描述:下面SQL,创建一个超过255列的表(实际为 ...
- 文件件监听器,android系统拍照功能调用后删除系统生成的照片
先说说要实现的功能: android调用系统拍照功能实时 预览 删除 上传 保存 (用户不能再本地文件夹中看到拍的照片) 再说说遇到的问题: 1.调用系统拍照在系统自带的拍照文件夹中生成一张随机命名图 ...
- CSS:布局的三个关键属性:float、position、display
最近在出差,就我一个在这里.客户要做几个页面,涉及到了页面的布局问题,没办法自己得做了.然后就遇到了一些问题.页面不论怎么都不能按照设想的布局. 以前也没有做过网页布局方面的工作.上网上找类似的例子, ...
- SQL Server调优系列基础篇(并行运算总结)
前言 上三篇文章我们介绍了查看查询计划的方式,以及一些常用的连接运算符.联合运算符的优化技巧. 本篇我们分析SQL Server的并行运算,作为多核计算机盛行的今天,SQL Server也会适时调整自 ...
- Bootstrap模态框(modal)垂直居中
http://v3.bootcss.com/ 自己也试了改了几种方式也不容乐观,发现在窗口弹出之前是获取不到$(this).height()的值,本想着是用($(window).height()-$( ...