本文给出一个简单的Hibernate4.3.5入门实例,配置方式采用XML文件方式(这种方式已经不是主流了,目前越来越多采用Annotation方式映射POJO实体)

代码结构如下图所示:主要用到hibernate-release-4.3.5.Final\lib\required下的jar

在这里只展示出主要的代码

1、HibernateUtil.java

 package com.wangp.hibernate.helper;

 import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
catch (Throwable ex) {
System.err.println("buildSessionFactory() error:" + ex);
throw new ExceptionInInitializerError(ex);
}
} public static SessionFactory getSessionFactory() {
return sessionFactory;
} }

2、测试类

 import org.hibernate.HibernateException;
import org.hibernate.Session; import com.wangp.hibernate.helper.HibernateUtil;
import com.wangp.hibernate.model.Hibernate; public class HibernateTest {
public static void main(String[] args) {
try {
//实例化POJO类
Hibernate h = new Hibernate();
h.setDriverName("oracle.jdbc.OracleDriver");
h.setId("1");
h.setUrl("jdbc:oracle:thin:@localhost:1521@orcl");
//获取Session
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
//curd操作
session.save(h);
session.getTransaction().commit();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}

从开发的角度简单的谈谈 hibernate的优缺点

优点:

1、通过hibernate来建立DB与实体之间的联系,对于简单的表操作不需要再去拼写SQL语句,减少了重复的工作量。

2、hibernate去除了app与DB之间的直接耦合关系,使得app的移植性得到增强

缺点:

1、对于SQL新手来讲,在没有接触、处理过复杂的SQL的情况下使用hibernate开发app,即使能够在控制台打出sql,开发调试依然还是比较艰难的事情。

Hibernate4.3.5入门HelloWorld的更多相关文章

  1. Hibernate4.x之入门篇

    Hibernate作为一个优秀的持久化框架.ORM框架.在日常的Java开发中常常用到.本文主要通过一个简单的例子来介绍下Hibernate4.x的入门知识. 新建一个Java项目,并加入Hibern ...

  2. RabbitMQ学习总结 第二篇:快速入门HelloWorld

    目录 RabbitMQ学习总结 第一篇:理论篇 RabbitMQ学习总结 第二篇:快速入门HelloWorld RabbitMQ学习总结 第三篇:工作队列Work Queue RabbitMQ学习总结 ...

  3. Wix学习整理(1)——快速入门HelloWorld

    原文:Wix学习整理(1)--快速入门HelloWorld 1 Wix简介 Wix是Windows Installer XML的简称,其通过类XML文件格式来指定了用于创建Windows Instal ...

  4. CodeIgniter入门——HelloWorld

    原文:CodeIgniter入门--HelloWorld CodeIgniter(CI)是一套给PHP网站开发者使用的应用程序开发框架和工具包. 初次接触,来一个HelloWorld~~~ ^_^ 准 ...

  5. Flask入门HelloWorld

    Flask入门HelloWorld Flask官网:http://flask.pocoo.org/ Flask中文翻译:http://dormousehole.readthedocs.io/en/la ...

  6. Windows Eclipse Scala的入门HelloWorld

    [学习笔记] Windows Eclipse Scala的入门HelloWorld 有关带scala版本的eclipse4.7的下载, 你可以直接去: http://scala-ide.org/dow ...

  7. Springboot2.x入门——helloWorld

    Springboot2.x入门--helloWorld 一.简介 1.1 Springboot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的 ...

  8. idea创建Scala入门HelloWorld

    Scala开发环境的搭建 首先到Scala官网下载Scala网址为 https://www.scala-lang.org/download/ 找到下图所示位置:选择相对应的版本的Scala进行下载,这 ...

  9. Node.js开发入门—HelloWorld再分析

    在Node.js开发入门(1)我们用http模块实现了一个简单的HelloWorld站点,这次我们再来细致分析下代码.了解很多其它的细节. 先看看http版本号的HelloWorld代码: 代码就是这 ...

随机推荐

  1. angularJS中directive父子组件的数据交互

    angularJS中directive父子组件的数据交互 1. 使用共享 scope 的时候,可以直接从父 scope 中共享属性.使用隔离 scope 的时候,无法从父 scope 中共享属性.在 ...

  2. linux的find命令详解

    find命令是用来在给定的目录下查找符合给定条件的文件 find [OPTIONS] [查找起始路径] [查找条件] [处理动作]   一.OPTIONS参数 -P.-L.-H:控制软连接的对待方式, ...

  3. 003——VUE操作元素属性

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. LeetCode OJ:Binary Tree Maximum Path Sum(二叉树最大路径和)

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

  5. dataGridView的使用经验

    1.dataGridView是dataGrid的替代品,包含了dataGrid的全部功能. 2.为dataGridView赋值,一般将其数据设置为一个DataTabel.例子如下: DataTable ...

  6. 4.CRT远程连接的使用

    目录: 1.为什么需要远程连接? 2.一般的远程连接工具有哪些? 3.远程连接的原理? 4.远程连接的软件的功能和使用相关技巧? 1.为什么选择远程连接? 因为在实际工作中,机房一般都不可能在办公室, ...

  7. 利用git bash和git gui向git远程仓库提交文件

    1.首先在该文件夹下git init 2.然后在github下面创建一个新仓库去存储你的代码 3.然后利用add添加远程仓库 4.然后点击stage changed 5.最后点击长传 参考链接:htt ...

  8. 十、python沉淀之路--高阶函数初识

    一.高阶函数:分两种:一种是返回值中包含函数体:另一种是把一个函数体当作了参数传给了另一个函数 1.返回值中包含函数体 例1. def test(): print('这是一个测试') return t ...

  9. Spring按名称自动装配--byName

    在Spring中,“按名称自动装配”是指,如果一个bean的名称与其他bean属性的名称是一样的,那么将自动装配它. 例如,如果“customer” bean公开一个“address”属性,Sprin ...

  10. ubantu 安装tree命令

    前提:必须安装好VMware tools 在linux系统中找不到tree这个命令时,需要安装,如ubuntu用下面的命令就可以安装tree这个命令工具,其他linux系统类似. 1 sudo apt ...