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.总结 将配置文件省略,让类和配置文件浑然一体,让配置更加简单,十分方便!

posted @ 2016-06-28 09:12  任心荒芜  阅读(205)  评论(0编辑  收藏  举报