2. 实例代码

新建一个java工程,假设取名为HibernateHelloWorld。在src下新那一个package,可取名为com.sun.hibernate.model

2.1 类代码

新建一个简单的类,放在com.sun.hibernate.model包下。内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.sun.hibernate.model;
 
public class Student {
    private int id;
    private String name;
    private int age;
 
    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;
    }
 
}

2.2 配置hibernate配置文件

hibernate\projec\etc中的hiberante.cfg.xml可以作为hibernate的配置文档,或者可使用hibernate\documentation\manual\en-US\html_single\index.html作为模板。在src文件夹下新建一个文件,并命名为hibernate.cfg.xml。(不可命名为其他文件名)最基础的配置文件可参考如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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">
     
<hibernate-configuration>
    <session-factory>
     
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost/hibernate</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>
         
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
         
    <!-- Enable Hibernate's automatic session context management -->
    <!--<property name="current_session_context_class">thread</property>-->
         
    <!-- Drop and re-create the database schema on startup -->
    <!-- <property name="hbm2ddl.auto">create</property> -->
         
    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
     
    <mapping resource="com/sun/hibernate/model/Student.hbm.xml"/>
         
    </session-factory>
</hibernate-configuration>

mapping resource处的值可根据包名和类名做修改。若类名为Student,则此处的类配置文件必为:Student.hbm.xml。

2.3 类mapping文件

新建一个文件,命名为Student.hbm.xml,放在com.sun.hibernate.model包下。内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping package="com.sun.hibernate.model">
    <class name="Student">
        <id name="id"></id>
        <property name="name"></property>
        <property name="age"></property>
    </class>
</hibernate-mapping>

注意文件开始处的配置,此处与hibernate.cfg.xml不一样。如果配置的与hiberante.cfg.xml一样,运行时会提示错误:“文档根元素 "hibernate-mapping" 必须匹配 DOCTYPE 根 "hibernate-configuration"  ”

2.4 StudentTest测试类

新增Student.java的junit测试类StudentTest.java,放在com.sun.hibernate.model包下代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.sun.hibernate.model;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class StudentTest {
 
    public static void main(String[] args){
        Student s = new Student();
        s.setId(1);
        s.setName("s1");
        s.setAge(1);
         
        Configuration cfg = new Configuration();
        SessionFactory sf = cfg.configure().buildSessionFactory();
         
        Session session = sf.openSession();
        session.beginTransaction();
        session.save(s);
        session.getTransaction().commit();
        session.close();
        sf.close();    
    }
}

2.5. 运行结果

运行StudentTest.java这个类,虽然提示输入成功。去数据库查询后,可发现数据已存储到student数据表中。虽然myeclipse会提示buildSessionFactory()这个函数被deprecated,但实际上程序还是可以运行成功的。

bibnernate(2)的更多相关文章

随机推荐

  1. CheckBox设置Enabled为False后,无法修改ForeColor

    设置CheckBox的AutoCheck为False,Enabled为True,即可解决.

  2. maven更新远程仓库速度太慢解决方法

    1.maven在更新下载jar包的时候,因为jar包默认是从国外服务器上下载的,所以速度特别慢 2.通过设置镜像的方法加快jar包下载 3.在maven安装目录下,/config/settings.x ...

  3. 深入理解JavaScript系列

    转自http://www.cnblogs.com/TomXu/archive/2011/12/15/2288411.html 深入理解JavaScript系列(1):编写高质量JavaScript代码 ...

  4. python基础知识4——collection类——计数器,有序字典,默认字典,可命名元组,双向队列

    1.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. ps:具备字典的所有功能 + 自己的功能  Counter 我们从中挑选一些相对常用的方法来举例: 在上面的例子 ...

  5. diamond专题(一)– 简介和快速使用

     (转自 http://blog.csdn.net/zh_winer/article/details/50395024) 一.概况 diamond是淘宝内部使用的一个管理持久配置的系统,它的特点是简单 ...

  6. C# 实现一个可取消的多线程操作 示例

    private void button1_Click(object sender, EventArgs e) { //定义一个为可取消资源标志 CancellationTokenSource cts ...

  7. DSP学习中遇到的几个问题(初级)

    1..c和.asm 文件分别为C语言和汇编语言的源文件. 2..cmd是存储器分配说明文件,主要功能是指定工程中的各段分配到那段存储器中,比如有片内RAM(起始地址,大小)和SDRAM等.这些要根据平 ...

  8. poj1753改

    #include<iostream> char data[16]; int a[16]; int d[5]={0,-4,1,4,-1}; char b[16]; int flag; int ...

  9. Android用ImageView显示本地和网上的图片

    ImageView是Android程序中经常用到的组件,它将一个图片显示到屏幕上. 在UI xml定义一个ImageView如下: public void onCreate(Bundle savedI ...

  10. [OC笔记] protocol之我的见解

    OC中的protocol就是和JAVA中interface差不多的东西,但是又不是完全一样的.这个protocol常用来实现委托,也就是自己不实现,当事件产生的时候去回调委托者. 让委托者去执行响应的 ...