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();
}
}