1.配置

http://hibernate.org/orm/下载hibernate包然后解压

在eclipse中新建一个java project,如名为hibernate_test

再所建工程中新建一个文件夹libs

将\lib\required下的所有jar包,(另外数据库驱动的jar包,我使用mysql所以导入了mysql-connector-5.1.26-bin.jar,另外还有做测试用的junit包) 复制到libs文件夹下

将所有的jar包选中右键build path->add to path

再将\project\etc下的hibernate.cfg.xml和log4j.properties复制到src下

配置完成

2.

案例

<!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>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql:///test</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">959511586</property>

<mapping resource="com/kotar/entity/Student.hbm.xml"/>

</session-factory>

</hibernate-configuration>

1)在hibernate.cfg.xml中添加如下,配置数据库(红色部分是数据库中创建好的的schema)

2)定义持久化类

package com.kotar.entity;

import java.io.Serializable;

public class Student implements Serializable {

/**

*

*/

private static final long serialVersionUID = 1L;

private int id;

private String name;

private int age;

public Student() {

// TODO Auto-generated constructor stub

}

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;

}

}

配置对象关系映射文件(Student.hbm.xml)持久化类和数据库表的对应关系

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.kotar.entity.Student" table="stu_tab">

<id name="id" column="stu_id">

<generator class="native"></generator>

</id>

<property name="name" column="stu_name"></property>

<property name="age" column="stu_age"></property>

</class>

</hibernate-mapping>

3)在hibernate.cfg.xml中注册对象关系映射文件

<mapping resource="com/kotar/entity/Student.hbm.xml"/>

4)编写测试类,对数据库操作

package com.kotar.test;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.boot.registry.StandardServiceRegistry;

import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import org.hibernate.cfg.Configuration;

import org.hibernate.tool.hbm2ddl.SchemaExport;

import org.junit.Test;

import com.kotar.entity.Student;

public class StudentTest {

@Test

public void createTable() {

Configuration cfg=new Configuration().configure();

SchemaExport se=new SchemaExport(cfg);

se.create(true, true);

}

@Test

public void insert(){

Configuration cfg=new Configuration().configure();

StandardServiceRegistryBuilder ssrb=new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());

StandardServiceRegistry serviceRegistry= ssrb.build();

SessionFactory sessionFactory=cfg.buildSessionFactory(serviceRegistry);

//获取Session

Session session=sessionFactory.openSession();

//获取事务

org.hibernate.Transaction transaction=session.beginTransaction();

//输入数据

Student student=new Student();

student.setAge(17);

student.setName("kotar");

try {

session.save(student);

transaction.commit();

} catch (Exception e) {

try {

transaction.rollback();

} catch (IllegalStateException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}finally {

session.clear();

}

}

}

先后用junit运行测试方法createTable()和insert()

结果如下

配置和创建一个hibernate简单应用的更多相关文章

  1. 在VS中手工创建一个最简单的WPF程序

    如果不用VS的WPF项目模板,如何手工创建一个WPF程序呢?我们来模仿WPF模板,创建一个最简单的WPF程序. 第一步:文件——新建——项目——空项目,创建一个空项目. 第二步:添加引用,Presen ...

  2. MVVM之旅(1)创建一个最简单的MVVM程序

    这是MVVM之旅系列文章的第一篇,许多文章和书喜欢在开篇介绍某种技术的诞生背景和意义,但是我觉得对于程序员来说,一个能直接运行起来的程序或许能够更直观的让他们了解这种技术.在这篇文章里,我将带领大家一 ...

  3. 创建一个hibernate helloword

    1.下载hibernate包 http://hibernate.org/orm/ 点download下载最终版 安装hibernate插件hibernatetools 2.创建一个java工程 导入必 ...

  4. hibernate安装和配置和第一个Hibernate应用

    ssh的各个版本要搭配好,struts2.3一般搭配hibernate4.x,我下的是4.3. Hibernate4的改动较大只有spring3.1以上版本能够支持,Spring3.1取消了Hiber ...

  5. 如何用Unity创建一个的简单的HoloLens 3D程序

    注:本文提到的代码示例下载地址>How to create a Hello World 3D holographic app with Unity 之前我们有讲过一次如何在HoloLens中创建 ...

  6. SAP Cloud Platform integration上创建一个最简单的iFlow

    登录SAP CPI控制台,点击这个铅笔图标进入工作区域: 选择一个已经存在的content package: 在这个content package里创建一个新的iFlow: 默认生成的iFlow模型如 ...

  7. Framework7 - 入门教程(安装、配置、创建一个H5应用)

    1,Framework7介绍 (1)Framework7 是一个开源免费的框架.可以用来开发混合移动应用(原生和 HTML 混合)或者开发 iOS & Android 风格的 WEB APP. ...

  8. 创建一个最简单的SpringBoot应用

    已经来实习了一段时间了,从开始接触到SpringBoot框架到现在一直都感觉SpringBoot框架实在是为我们带来了巨大遍历之处,之前一直在用并没有总结一下,现在有空从零开始写点东西,也算是对基础的 ...

  9. 前端 vue-cli+Webpack 项目开发环境配置、创建一个vue-demo

    一.软件及命令: (1)下载node.js 最新的LTS 版本,下载 msi格式的(直接点击安装即可). (2)命令1:npm install cnpm -g 命令2:cnpm install web ...

随机推荐

  1. IIS 7.5最新解析漏洞

    IIS7.5解析漏洞 http://www.cnk0n9.com/fckeditor/editor/fckeditor.html, 上传图片,浏览,上传一个aspx的一句话木马,名字为:a.aspx. ...

  2. 设置默认python模块源

    可以在Linux编辑~/.pip/pip.conf或者在Windows下编辑%HOME%\pip\pip.ini,内容如下:[global]index-url = http://pypi.douban ...

  3. windows下安装MongoDB要注意的问题

    1.  errno:10061 由于目标计算机积极拒绝,无法连接. 解决方法:在mongoDB的bin目录下,打开命令行,输入: mongod --dbpath "c:\data\db&qu ...

  4. Java中sql语句的引号问题

    1..sql语句 在数据库中,当我们查询语句时,会使用类似的语句: Select * from userinfo where userid='1' or 1; Select * from userin ...

  5. interface

    接口的简单案例: 接口 就是一种规范 其目的主要是为了约束和解耦 public class Test { public static void main(String[] args){ Compute ...

  6. 最近在做外贸网站的时候,需要大量的字体来充实页面,就学习了怎么引用Google Fonts

    第一步,FQ进入谷歌官方字体网站:https://fonts.google.com  妥妥的. 第二步,点击你所选择字体演示块的右上角的加号,然后你所选择的字体会形成引用链接以及你所要写的css样式. ...

  7. blade and soul Personal Combos

    Personal Combos Since Blade and Soul is mainly based on skills, the game is more interesting after y ...

  8. 伪静态重写模块rewrite.dll及httpd.ini文件参考下载

    伪静态重写模块rewrite.dll及httpd.ini文件参考下载 http://www.ledaokj.com/download/rewrite.rar 服务器端开启伪静态,可以查看以下文章< ...

  9. js变量及其作用域

    Javascript和Java.C这些语言不同,它是一种无类型.弱检测的语言.它对变量的定义并不需要声明变量类型,我们只要通过赋值的形式,可以将各种类型的数据赋值给同一个变量   一.js变量的类型及 ...

  10. python基础教程-第三章-使用字符串

    本章将会介绍如何使用字符串何世华其他的值(如打印特殊格式的字符串),并简单了解下利用字符串的分割.联接.搜索等方法能做些什么 3.1 基本字符串操作 所有标准的序列操作(索引.分片.乘法.判断成员资格 ...