myeclipse创建hibernate工程
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工程的更多相关文章
- MyEclipse创建Maven工程
先要在MyEclipse中对Maven进行设置:
- java使用Myeclipse创建Hibernate项目碰到的诸多问题总结
这两天一直在搞Myeclipse创建Hibernate的1对多映射. 由于缺乏经验,可算是把我坑惨了.控制台是不停地报错啊~~~~我差点就崩溃了. 1.看的是慕课网的Hibernate一对多映射教程, ...
- 通过myEclipse创建hibernate的实体类
今天有个新项目中需要使用到hibernate,刚好数据库表已经创建完毕,就顺便来总结一下通过myEclipse创建hibernate的实体类. 1..在myEclipse中选择MyEclipse Da ...
- 使用Eclipse创建Hibernate工程
创建一个java project项目,加入hibernate的jar包和数据库驱动包,并引入到项目.
- MyEclipse中创建maven工程
转载:http://blog.sina.com.cn/s/blog_4f925fc30102epdv.html 先要在MyEclipse中对Maven进行设置: 到此Maven对MyEclip ...
- 【转载】Myeclipse如何自动创建hibernate
Myeclipse如何自动创建hibernate:http://jingyan.baidu.com/article/456c463b99f4370a583144a8.html An internal ...
- 使用Myeclipse为数据表创建hibernate实体对象
hibernate是orm框架的一种,orm即Object Relational Mapping,对象映射关系,其主要作用是将数据库(mysql,mssql,oracle)的对象转换为具体编程语言(如 ...
- Hibernate工程的手动创建
1.打开MyEclipse软件,新建Java项目,如HibernateReview: 2.导入Hibernate所需的jar包: 右键build path选择configurate build pat ...
- Myeclipse中创建Maven工程的时候没有 webapp-javaee6
1. http://mvnrepository.com/artifact/org.codehaus.mojo.archetypes/webapp-javaee6/1.5 中有描述
随机推荐
- GPSCamera隐私声明
GPSCamera获取了以下敏感隐私权限用于照片水印展示: 1:修改或删除您的SD卡中的内容 2:访问确切位置信息(使用 GPS 和网络进行定位) 3:访问大致位置信息(使用网络进行定位) 4:拍摄照 ...
- 如何在本地搭建一个Android应用crashing跟踪系统-ACRA
https://github.com/bboyfeiyu/android-tech-frontier/tree/master/others/%E5%A6%82%E4%BD%95%E5%9C%A8%E6 ...
- 普通socket与netty服务端交互
Socket socket = new Socket(host, port);OutputStream out = socket.getOutputStream();ByteBuffer header ...
- Linux 保护文件 不给修改
chatter +i file 文件不能删除,不能更改,不能移动 chatter -i file 恢复 lsattr file 查看 ----i--------e-- file 修改会提示: f ...
- Asp.net 子域共享cookie
最近项目遇到要共享cookie的问题,本来后台保存session用的是Redis来保存数据的.所以只需要2个站点发的ASP.NET_SessionId是相同的就可以,并且它的Domain 是父级域名. ...
- 使用Python登录Github网站
在下面的代码中, 展示了使用Python脚本登录Github的方法. 如果需要登录别的网站,那么请使用Chrome的Inspect的功能寻找到目标的object,对代码进行替换. 代码先登录了gith ...
- SpringMVC项目配置欢迎页面为index.html
一.问题 在web.xml中添加如下配置无效 <welcome-file-list> <welcome-file>index.html</welcome-file> ...
- Fix Corrupt Blocks on HDFS
来自:http://centoshowtos.org/hadoop/fix-corrupt-blocks-on-hdfs/ How do I know if my hadoop hdfs filesy ...
- Bullet物理引擎的安装与使用
图形赋予游戏一种视觉的吸引力,但是能够让游戏的世界鲜活起来的还应该是内部的物理引擎.物理引擎是游戏引擎中的子模块,是一种软件组件,可仿真物理系统.它根据牛顿力学定律,计算游戏中物体的合理的物理位置,并 ...
- 对actuator的管理端点进行ip白名单限制(springBoot添加filter)
在我们的SpringCloud应用中,我们会引入actuator来进行管理和监控我们的应用 常见的有:http://www.cnblogs.com/yangzhilong/p/8378152.html ...