1.创建数据库:

from blog http://www.cnblogs.com/zhaocundang/p/9061959.html

使用navicat mysql IDE:

创建数据库 bookshop

创建表

CREATE TABLE books(id INT PRIMARY KEY auto_increment,Bookname VARCHAR(50),Bookprice VARCHAR(40));

ok 数据库部分整完。

打开myeclipse创建web工程:

finish

创建数据库连接db browser

如果没有的话,在other查找db browser

右键new

填信息

测试连接



finish

展开数据库,右键数据库表book,反向工程

finish

右键工程导入hibernate库

取消勾

整个工程:

创建个测试类:

插入数据

package test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import pojo.Book; public class mytest { static Configuration cfg = null;
static SessionFactory sessionFactory = null; public static void insert(){ cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory(); Configuration cfg = new Configuration();
cfg.configure(); SessionFactory sessionFactory = cfg.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Book book = new Book(); book.setBookname("语文");
book.setBookprice("18元"); //调用session的方法实现添加
session.save(book); // 第六步 提交事务
tx.commit(); // 第七步 关闭资源
session.close();
sessionFactory.close(); } public static void main(String[] args){ insert(); } }

更新数据

package test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import pojo.Book; public class update { static Configuration cfg = null;
static SessionFactory sessionFactory = null; public static void update(){ cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory(); Configuration cfg = new Configuration();
cfg.configure(); SessionFactory sessionFactory = cfg.buildSessionFactory(); Session session = sessionFactory.openSession();
Transaction tx= session.beginTransaction(); //修改第一条数据
Book book =(Book)session.get(Book.class, new Integer(1)); book.setBookname("语文");
book.setBookprice("20元"); //保存一下
session.save(book); // 第六步 提交事务
tx.commit(); // 第七步 关闭资源
session.close();
sessionFactory.close(); } public static void main(String[] args){ update(); } }

删除数据

package test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import pojo.Book; public class delete { static Configuration cfg = null;
static SessionFactory sessionFactory = null; public static void delete(){ cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory(); Configuration cfg = new Configuration();
cfg.configure(); SessionFactory sessionFactory = cfg.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); //删除第三条数据
Book book =(Book)session.get(Book.class, new Integer(3)); session.delete(book); // 第六步 提交事务
tx.commit(); // 第七步 关闭资源
session.close();
sessionFactory.close(); } public static void main(String[] args){ delete(); } }

查询数据

package test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import pojo.Book; public class select { static Configuration cfg = null;
static SessionFactory sessionFactory = null; public static void select(){ cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory(); Configuration cfg = new Configuration();
cfg.configure(); SessionFactory sessionFactory = cfg.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Book book =(Book)session.get(Book.class, new Integer(1));
System.out.println("书的ID是:"+book.getId()+"\n"+"书名是:"+book.getBookname()+"\n"+"价格是:"+book.getBookprice()); // 第六步 提交事务
tx.commit(); // 第七步 关闭资源
session.close();
sessionFactory.close(); } public static void main(String[] args){ select(); } }

myeclipse创建hibernate工程的更多相关文章

  1. MyEclipse创建Maven工程

    先要在MyEclipse中对Maven进行设置:

  2. java使用Myeclipse创建Hibernate项目碰到的诸多问题总结

    这两天一直在搞Myeclipse创建Hibernate的1对多映射. 由于缺乏经验,可算是把我坑惨了.控制台是不停地报错啊~~~~我差点就崩溃了. 1.看的是慕课网的Hibernate一对多映射教程, ...

  3. 通过myEclipse创建hibernate的实体类

    今天有个新项目中需要使用到hibernate,刚好数据库表已经创建完毕,就顺便来总结一下通过myEclipse创建hibernate的实体类. 1..在myEclipse中选择MyEclipse Da ...

  4. 使用Eclipse创建Hibernate工程

    创建一个java project项目,加入hibernate的jar包和数据库驱动包,并引入到项目.

  5. MyEclipse中创建maven工程

    转载:http://blog.sina.com.cn/s/blog_4f925fc30102epdv.html     先要在MyEclipse中对Maven进行设置: 到此Maven对MyEclip ...

  6. 【转载】Myeclipse如何自动创建hibernate

    Myeclipse如何自动创建hibernate:http://jingyan.baidu.com/article/456c463b99f4370a583144a8.html An internal ...

  7. 使用Myeclipse为数据表创建hibernate实体对象

    hibernate是orm框架的一种,orm即Object Relational Mapping,对象映射关系,其主要作用是将数据库(mysql,mssql,oracle)的对象转换为具体编程语言(如 ...

  8. Hibernate工程的手动创建

    1.打开MyEclipse软件,新建Java项目,如HibernateReview: 2.导入Hibernate所需的jar包: 右键build path选择configurate build pat ...

  9. Myeclipse中创建Maven工程的时候没有 webapp-javaee6

    1. http://mvnrepository.com/artifact/org.codehaus.mojo.archetypes/webapp-javaee6/1.5 中有描述

随机推荐

  1. Python3练习题系列(05)——设计和调试规则

    If 语句的常见规则 1. 每一个“if 语句”必须包含一个else: 2. 如果这个else 永远都不应该被执行到,因为它本身没有任何意义,那你必须在else 语句后面使用一个叫做die 的函数,让 ...

  2. (华中科大)江南雨烟 C++ STL 专栏

    本文转载来自,华中科技大学江南雨烟的C/C++专栏部分STL剖析文章,以作学习之用. [1]  [C++ STL学习之一]容器的共通能力和共通操作总结 [2]  [C++ STL学习之二]容器vect ...

  3. Delphi识别读取验证码

    unit OCR; interface uses Windows, SysUtils, Graphics, Classes, PNGImage, GIFImage, JPEG, Math, Asphy ...

  4. Unity3d之截图方法

    http://blog.csdn.net/highning0007/article/details/37991787 Unity3d之截图方法 分类: Unity3D2013-11-28 17:13  ...

  5. 如何自动播放光盘、解决win7电脑不能播放光盘

    如何设置光盘自动播放.允许光盘自动运行呢? 在使用电脑光驱播放光盘文件的时候,经常出现的一个问题是,光驱不能自动播放光盘,但是打开光盘的文件手动操作没有任何问题,这给使用造成了很多麻烦.那么,如何让光 ...

  6. 如何将revit模型背景设置为黑色

    Revit软件建模窗口默认的背景色为白色,在用惯了CAD的新用户转到Revit软件的时候,会对Revit白色的背景不太适应,跟AutoCAD一样,Revit提供自定义工作区背景颜色的功能--其实,你只 ...

  7. Tomcat热部署SpringMVC项目出错

    一.问题 项目照常跑,没有什么大的影响,但是在控制台却出现了错误,具体信息如下图所示 二.解决方法 原因分析:很多人已经说的很明白了,这大概是因为项目文件很多,在tomcat重启的时候,之前的tomc ...

  8. tmux 共享窗口大小

    http://www.cnblogs.com/bamanzi/p/tmux-share-windows-between-sessions.html

  9. phpBB3.2 自动检测浏览器语言

    这是根据HTTP request header里的Accept-Language信息来处理的. 首先看一下Accept-Language的格式 Accept-Language: <languag ...

  10. 使用Java合并图片、修改DPI

    项目中有时候需要对图片进行DPI.合并.拼接等的处理: package com.snow.web.a_test; import java.awt.Graphics; import java.awt.i ...