享受痛苦就是走向成功的第一步。

一、创建一个项目(lib里面是需要的夹包小奶瓶要导包)

    

二、书写大配置文件

大配置文件必须放置在项目根目录(专业classpath下):界定:就是src

1名称: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"> <hibernate-configuration> <session-factory> <!-- Database connection settings 数据库连接设置-->
<!-- 驱动类 -->
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost::orcl</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property> <!-- SQL dialect (sql的方言)-->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- Echo all executed SQL to stdout 在控制台打印后台的sql语句-->
<property name="show_sql">true</property>
<!-- 格式化显示sql -->
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup 序列化-->
<property name="hbm2ddl.auto">update</property> <mapping resource="hibernate.hbm.xml" /> </session-factory> </hibernate-configuration>

三、创建小配置

1、名称:hibernate.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.entity">
<class name="Student" table="STUDENT">
<id name="sid" column="SID">
<!-- 主键生成策略:native: native:如果后台是Oracle 后台是MySQL,自动应用自增 -->
<generator class="native" />
</id>
<property name="name" type="string" column="NAME" />
<property name="age" />
</class> </hibernate-mapping>

四、测试类

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import cn.happy.entity.Student; public class Test { /**
* @param args
*/
public static void main(String[] args) {
//addAll();//添加信息
//getAll();//查看信息
//deleteAll();//删除信息
UpdateAll();//修改信息
}
//添加信息
public static void addAll(){
Student stu = new Student();
//stu.setSid(2);
stu.setName("聆听");
stu.setAge();
// 1.1读取大配置文件,获取要连接的数据库信息
Configuration cfg = new Configuration().configure();
// 1.2创建SessionFactory
SessionFactory factory = cfg.buildSessionFactory(); // 1.3加工session
Session session = factory.openSession(); Transaction tx = session.beginTransaction(); // 02.Hibernate 帮我保存 session.save(stu); tx.commit();
System.out.println("save ok!"); }
//查看全部信息
public static void getAll(){
//读取配置文件
Configuration conf=new Configuration().configure();
//创建SessionFactory
SessionFactory sf=conf.buildSessionFactory();
//打开session
Session se=sf.openSession();
//加载数据
Student dept=(Student)se.get(Student.class, new Integer( ));
System.out.println(dept.getName());//输入数据
System.err.println(dept.getAge());
//关闭会话
if(se!=null){
se.close();
} }
//删除信息
public static void deleteAll(){
//读取数据文件
Configuration conf=new Configuration().configure();
//创建SessionFactory
SessionFactory se=conf.buildSessionFactory();
//打开session
Session session=se.openSession();
//开始一个事务
Transaction tx=session.beginTransaction();
//获取部门的对象
Student stu=(Student)session.get(Student.class, new Integer());
//删除对象(持久化操作)
session.delete(stu);
//提交事务
tx.commit();
System.out.println("删除成功");
//回滚事务
tx.rollback();
System.out.println("删除回滚");
//关闭session
if(session!=null){
session.close();
}
}
public static void UpdateAll(){
//读取数据文件
Configuration conf=new Configuration().configure();
//创建SessionFactory
SessionFactory se=conf.buildSessionFactory();
//打开session
Session session=se.openSession();
//开始一个事务
Transaction tx=session.beginTransaction();
//获取部门的对象
Student stu=(Student)session.get(Student.class, new Integer());
//修改信息
stu.setName("女王");
//提交事务
tx.commit();
/*http://blog.csdn.net/woxueliuyun/article/details/3930335*/
System.out.println("修改成功");
//回滚事务
tx.rollback();
System.out.println("修改回滚");
//关闭session
if(session!=null){
session.close();
}
}
}

Hibernate入门案例配置以及增、删、改、查看的更多相关文章

  1. Hibernate入门案例及增删改查

    一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...

  2. Hibernate入门案例 增删改

    一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...

  3. C# ADO.NET (sql语句连接方式)(增,删,改)

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  4. 好用的SQL TVP~~独家赠送[增-删-改-查]的例子

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化.  本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...

  5. iOS FMDB的使用(增,删,改,查,sqlite存取图片)

    iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...

  6. iOS sqlite3 的基本使用(增 删 改 查)

    iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...

  7. Hibernate入门3.配置映射文件深入

    Hibernate入门3.配置映射文件深入 2013.11.27 前言: 之前的两节是在Java项目中如何使用hibernate,并且通过一个简单地项目实践,期间有很多的错误,一般都是因为配置包的问题 ...

  8. ADO.NET 增 删 改 查

    ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...

  9. MVC EF 增 删 改 查

    using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...

随机推荐

  1. js对象私有变量公有变量问题

    0 js对象私有变量公有变量问题5 小弟初学JS面向对象编程 现有一问题 请教各位大虾: Person=function (){ //私有变量定义 var name; vae age; var Ale ...

  2. JavaScript的陷阱

    这本来是翻译Estelle Weyl的<15 JavaScript Gotchas>,里面介绍的都是在JavaScript编程实践中平时容易出错或需要注意的地方,并提供避开这些陷阱的方法, ...

  3. 再读GFS论文

    http://loopjump.com/gfs_paper_note/ 再读GFS的一些笔记.主要涉及GFS架构.Chunk大小选择的一些折中考量.元数据管理及锁.写数据流程.GFS一致性模型的理解. ...

  4. ASP.NET 5中的那些K

    ASP.NET 5最大的变化是什么?首当其冲的就是多了很多K,K表示的是ASP.NET vNext的项目代号“Project K”,但很容易让人想到一个汉字——“坑”,希望K不要成为“坑”的缩写. K ...

  5. 数据可视化(7)--D3基础

    一直想写写D3,觉得D3真心比较强大,基本上你能想出来的图表都能绘制出来,只不过使用起来比前几个要稍麻烦一点. 正好最近读完了<数据可视化实战>,将关于D3的知识梳理了一遍,写这篇博客记录 ...

  6. [OpenCV] Samples 04: contours2

    要先变为二值图像:cvThreshold 提取轮廓:cvFindContours #include "opencv2/imgproc/imgproc.hpp" #include & ...

  7. Android基于mAppWidget实现手绘地图(九)–如何处理地图对象的touch事件

    为了响应touch事件,需要设置OnMapTouchListener 示例: private void initMapEventsListener() { mapWidget.setOnMapTouc ...

  8. 理解 angular2 基础概念和结构 ----angular2系列(二)

    前言: angular2官方将框架按以下结构划分: Module Component Template Metadata Data Binding Directive Service Dependen ...

  9. Hadoop阅读笔记(三)——深入MapReduce排序和单表连接

    继上篇了解了使用MapReduce计算平均数以及去重后,我们再来一探MapReduce在排序以及单表关联上的处理方法.在MapReduce系列的第一篇就有说过,MapReduce不仅是一种分布式的计算 ...

  10. EasyUI treegrid 获取编辑状态中某字段的值 [getEditor方法获取不到editor]

    如题,在treegrid里,按照api里getEditor方法的说明, getEditoroptionsGet the specified editor, the options contains t ...