hibernate Annotation版本的helloworld

经过第一次的 hibernate  我发现每一个数据库表都对应了一个类,并且每一个类都要新建一个文件进行配置 很麻烦!  于是便出现了Annotation版本的hibernate。

具体如下:

1.同样的 先新建一个java project。

2.导入hibernate插件(选中项目单击鼠标右键-->my eclipse-->project  facets-->hibernate-->next-->新建一个包选中-->next-->去掉上面那个勾-->finsish)。

3.可以发现在src目录下有了一个包 还有一个类。

4.新建一个Teacher类  代码如下:

package com.cqvie;

import javax.persistence.Id;

import org.hibernate.annotations.Entity;

@javax.persistence.Entity
public class Teacher {
private int id;
//设置主键
@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;
}
private String name;
private String title;
}

5.配置hibernate.cfg.xml文件:

<?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">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

     <session-factory>

        <!-- Database connection settings 用到的驱动、数据库名、用户名、密码 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/text</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</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.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 class="com.cqvie.Teacher"/>
    </session-factory>
</hibernate-configuration>

6.将mysql驱动导入项目。

  1. 在项目中新建一个文件夹
  2. 将驱动放入文件夹
  3. 选中驱动鼠标右键 build Path -->add

7.在mysql数据库中新建一个表 名为teacher  (int id PRIMARY KEY,verchar(20) name,verchar(20) title)

8.在com.cqvie 包下新建一个测试类TeacherTest

package com.cqvie;

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

public class testTeacher {
    public static void main(String[] args) {
        Teacher t= new Teacher();
        t.setId(1);
        t.setName("s1");
        t.setTitle("教授");
        Configuration cfg= new Configuration();
        SessionFactory sf=cfg.configure().buildSessionFactory();
        Session session=sf.openSession();
        session.beginTransaction();
        session.save(t);
        session.getTransaction().commit();
        session.close();
    }
}

9.运行结果如下:

成功!

10.总结 将配置文件省略,让类和配置文件浑然一体,让配置更加简单,十分方便!

时间: 2024-12-24 14:28:07

hibernate Annotation版本的helloworld的相关文章

陶顺利 建立annotation版本的helloworld

1.在前一个项目的基础上新建一个实体类teacher,属性有id,name,title. import javax.persistence.Id;@Entitypublic class Teacher { private int id; private String name; private String title;  @Id public int getId() {  return id; } public void setId(int id) {  this.id = id; } 2.建

Annotation版本的HelloWorld

hiberante 的 annotation历史: 在hibernate3以后,开始支持Annotation; 先有hiberante再有JPA,有了JPA标准之后,hibernate写了Annotation来支持JPA:所以 hibernate的annotation是JPA标准之下的,一般都直接用JPA的annotation,hibernate的annotation只有在极少的情况下才使用. 1.创建teacher表,create table teacher(id int primary ke

Hibernate---第一个helloworld程序 (XML版本, annotation版本)

Hibernate作为JPA的一种实现,jpa的注解已经是hibernate的核心,hibernate只提供了一些补充,而不是两套注解.hibernate对jpa的支持够足量,在使用hibernate注解建议使用jpa XML版本过程: 新建工程, 导入hibernate包, 数据库包, 建立数据库表, 新建model类, 测试类, 新建src下hibernate.cfg.xml, model 类下 Student.hbm.xml annotation版本过程: 新建工程, 导入hibernat

hibernate annotation注解方式来处理映射关系

在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式以后,发现使用annotation的方式可以更简介,所以这里就简单记录下通过annotation来配置各种映射关系,在hibernate4以后已经将annotation的jar包集成进来了,如果使用hibernate3的版本就需要引入annotation的jar包. 一.单对象操作 @Entity

Hibernate Annotation笔记

(1)简介:在过去几年里,Hibernate不断发展,几乎成为Java数据库持久性的事实标准.它非常强大.灵活,而且具备了优异的性能.在本文中,我们将了解如何使用Java 5 注释来简化Hibernate代码,并使持久层的编码过程变得更为轻松. 传统上,Hibernate的配置依赖于外部 XML 文件:数据库映射被定义为一组 XML 映射文件,并且在启动时进行加载.    在最近发布的几个Hibernate版本中,出现了一种基于 Java 5 注释的更为巧妙的新方法.借助新的 Hibernate

Rhythmk 学习 Hibernate 08 - Hibernate annotation 关联关系注解

1.一对一 (One to One)    共三种情况:     1.1 主键共享    1.2 外键共享 1.3 中间表关联 1.1  code: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Entity public class article {     @Id     @GeneratedValue    public Integer getArticleId() {         return articleId;     }     .....  

Rhythmk 学习 Hibernate 07 - Hibernate annotation 实体注解

参考: http://docs.jboss.org/hibernate/annotations/3.4/reference/zh_cn/html_single/ 1.系统配置: 可以通过使用  mapping的 resource,于class 属性混合配置 <mapping resource="com/rhythmk/model/product.hbm.xml" /> <mapping class="com.rhythmk.model.User"&

Hibernate 验证版本不兼容问题

Hibernate验证版本不兼容 用5.0以上Hibernate 验证即可

Hibernate Annotation关系映射, 级联cascade属性

Hibernate Annotation关系映射, 级联cascade属性一. Hibernate Annotation关系映射 1.一对一外键关联映射(单向) 2.一对一外键关联映射(双向) 3.一对一主键关联映射(不重要)在这不演示 在实际中很少用,使用注解@PrimaryKeyJoinColumn 意思是说,我的主键去参考另外一张表中的主键,作为我的主键,但是在我测试使用 注解一对一主键关联映射,在生成表的时候,数据库中并没有生成关联,使用XML 映射可以生成.Annotation注解一对