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 中有描述
随机推荐
- [POI2010]GRA-The Minima Game
OJ题号:洛谷3507 思路: 如果选了$k_i$,那么你的对手就可以选上所有$\geq{k_i}$的数.那么他其中获得的分数也一定$\geq{k_i}$. 如果你选了$k_i$以及所有$\geq{k ...
- node+koa2 使用ejs模版
1.进入项目下,npm install -save ejs 2.app.js加入: const ejs = require('ejs'); app.use(views(__dirname + '/vi ...
- centos7 rabbitmq集群搭建+高可用
环境 [root@node1 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@node1 ~]# uname -r -.el ...
- phpstorm破解
由于JetBrains系列新版本注册激活发生了变化,所以原来的激活方式已经不能在使用. 只能用新的方式来破解了.此方式支持所有系列的新版版.包括IDEA15,PHPSTORM10,WEBSTORM11 ...
- vs配置TFS
- ecshop jquery 冲突
遇到冲突在脚本前面加上这句 $(function() { window.__Object_toJSONString = Object.prototype.toJSONString; delete Ob ...
- AngularJS中控制器继承
本篇关注AngularJS中的控制器继承,了解属性和方法是如何被继承的. 嵌套控制器中属性是如何被继承的? ==属性值是字符串 myApp.controller("ParentCtrl&qu ...
- 写一个针对IQueryable<T>的扩展方法支持动态排序
所谓的动态排序是指支持任意字段.任意升序降序的排序.我们希望在客户端按如下格式写: localhost:8000/api/items?sort=titlelocalhost:8000/api/item ...
- mui 总结
出框框 js内容 mui(".mui-popover").popover('toggle'); 点击“弹出框框”就会弹出这个有class="mui-pop ...
- 一篇文章让你学透Linux系统中的more命令
Linux 下有很多实用工具可以让你在终端界面查看文本文件.其中一个就是 more. more 跟我之前另一篇文章里写到的工具 —— less 很相似.它们之间的主要不同点在于 more 只允许你向前 ...