菜鸟学习Hibernate——简单的增、删、改、查操作
最后更新于:2022-04-01 09:33:48
上篇博客利用Hibernate搭建起一个简单的例子,把数据库的映射显示了出来在上一篇的博客基础上这篇博客讲述如何利用Hinbernate框架实现简单的数据库操作。
1、加入junit.jar
2、新建一个工具类HibernateUtils.Java把一些对session的公共操作封装起来
~~~
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
/*
*读取Hibernate.cfg.xml文件
*/
private static SessionFactory factory;
static {
try {
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
//建立SessionFactory
factory = cfg.buildSessionFactory();
}catch(Exception e) {
e.printStackTrace();
}
}
/*
*打开Session
*/
public static Session getSession() {
return factory.openSession();
}
/*
*关闭Session
*/
public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
public static SessionFactory getSessionFactory() {
return factory;
}
}
~~~
3、新建一个test类继承TestCase(Junit包里面的)然后下面就利用test类对User对象的增、删、改、查。
~~~
import java.util.Date;
import org.hibernate.Session;
import com.bjpowernode.hibernate.HibernateUtils;
import com.bjpowernode.hibernate.User;
import junit.framework.TestCase;
public class test extends TestCase {
/*
* 增加User
*/
public void testSave() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
User user = new User();
user.setName("李四");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
session.save(user);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 删除
*/
public void testDel(){
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
User user=(User)session.load(User.class, "4028989545a244640145a24466300001");
session.delete(user);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 修改
*/
public void testUpdate(){
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
User user=(User)session.load(User.class, "4028989545a243ca0145a243cbf80001");
user.setName("王五");
session.update(user);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 查询
*/
public void testLoad(){
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
User user=(User)session.load(User.class, "4028989545a243ca0145a243cbf80001");
System.out.println(user.getName());
System.out.println(user.getId());
System.out.println(user.getPassword());
System.out.println(user.getClass());
System.out.println(user.getCreateTime());
System.out.println(user.getExpireTime());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
~~~
4、效果图
增
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-22_56caddfa98c96.jpg)
删
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-22_56caddfaaa90c.jpg)
改
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-22_56caddfabe221.jpg)
查
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-22_56caddfad90ea.jpg)
总结:
利用Hibernate简单的增、删、改、查操作的应用是一个很基础的应用刚学Hibernate就应该先从简单的学起。