Hibernate实现信息管理

Hibernate实现信息管理

增删改查

准备工作

环境:Eclipse;MySQL
导入Hibernate库(jar包 ;MySQL驱动包 ;log4j日志包。
Hibernate库下载网站: http://sourceforge.net/projects/hibernate/files/

1、增加

package com.cn;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Transaction ;
public class CustomerTest {
public static void main(String[] args) throws Exception {
Configuration config = new Configuration().configure(“hibernate.cfg.xml”) ;
SessionFactory sessionFactory =config.buildSessionFactory() ;
Session session =sessionFactory.openSession() ;
Transaction t = session.beginTransaction() ;
Customer c = new Customer();
c.setName(“号学生”);
c.setAge(21);
c.setCity(“哈尔滨”);
c.setSex(“男”);
session.save© ;
t.commit();
session.close();
sessionFactory.close();
}

2、删除

package com.cn;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class DeleteTest {

public static void main(String[] args) {
	// TODO 自动生成的方法存根
	Configuration config = new Configuration().configure("hibernate.cfg.xml") ;
	SessionFactory sessionFactory =config.buildSessionFactory() ;
	Session session =sessionFactory.openSession() ;
	Transaction t = session.beginTransaction() ;
	Customer u = (Customer)session.get(Customer.class,7);
	session.delete(u);
	t.commit(); 
	session.close();
	sessionFactory.close();
}

}

Hibernate:
select
customer0_.id as id1_0_0_,
customer0_.name as name2_0_0_,
customer0_.age as age3_0_0_,
customer0_.sex as sex4_0_0_,
customer0_.city as city5_0_0_
from
customer customer0_
where
customer0_.id=?
Hibernate:
delete
from
customer
where
id=?

3、修改

package com.cn;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Update {

public static void main(String[] args) {
	// TODO 自动生成的方法存根
	Configuration config = new Configuration().configure("hibernate.cfg.xml") ;
	SessionFactory sessionFactory =config.buildSessionFactory() ;
	Session session =sessionFactory.openSession() ;
	Transaction t = session.beginTransaction() ;
	Customer u = (Customer)session.get(Customer.class,4);
	System.out.println("id是=4的数据参数:"+u.getName());
	u.setName("小明");
	session.update(u);
	t.commit(); 
	session.close();
	sessionFactory.close();
}

}

4、查询

package com.cn;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class SelectTest {

public static void main(String[] args)throws Exception {
	Configuration config = new Configuration().configure("hibernate.cfg.xml") ;
	SessionFactory sessionFactory =config.buildSessionFactory() ;
	Session session =sessionFactory.openSession() ;
	Transaction t = session.beginTransaction() ;
	Customer u = (Customer)session.get(Customer.class,5);
	System.out.println("姓名="+u.getName());
	System.out.println("年龄="+u.getAge());
	System.out.println("姓别="+u.getSex());
	System.out.println("城市="+u.getCity());
	t.commit(); 
	session.close();
	sessionFactory.close();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值