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