Hibernate实现简单的增删改查(CRUD)
1、拷贝hibernate一里面的项目,改名为hibernate03。
2、新建包org.zttc.itat.util,在其下新建
HibernateUtil.java
package org.zttc.itat.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtil {
private final static SessionFactory Factory =buidSessionFactory();
private static SessionFactory buidSessionFactory() {
Configuration cfg =new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(cfg.getProperties())
.buildServiceRegistry();
SessionFactory factory =cfg.buildSessionFactory(serviceRegistry);
return factory;
}
public static SessionFactory getSessionFactory(){
return Factory;
}
public static Session openSession(){
return Factory.openSession();
}
public static void close(Session session){
if(session!=null)
session.close();
}
}
3、在包org.zttc.itat.test下新建Junit测试类
TestCRUD.java
package org.zttc.itat.test;
import java.text.SimpleDateFormat;
import java.util.List;
import org.hibernate.Session;
import org.junit.Test;
import org.zttc.itat.model.User;
import org.zttc.itat.util.HibernateUtil;
public class TestCRUD {
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@Test
public void testAdd() {
Session session = null;
try {
session = HibernateUtil.openSession();
session.beginTransaction();
User u = new User();
u.setBorn(sdf.parse("1977-11-22"));
u.setNickname("张老七");
u.setPassword("123");
u.setUsername("zhanglaowu");
session.save(u);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
if(session!=null) session.getTransaction().rollback();
} finally {
HibernateUtil.close(session);
}
}
@Test
public void testLoad() {
Session session = null;
try {
session = HibernateUtil.openSession();
User u = (User)session.load(User.class,1);
System.out.println(u);
} catch (Exception e) {
e.printStackTrace();
} finally {
HibernateUtil.close(session);
}
}
@Test
public void testUpdate() {
Session session = null;
try {
session = HibernateUtil.openSession();
session.beginTransaction();
User u = (User)session.load(User.class, 1);
u.setNickname("张小四");
session.update(u);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
if(session!=null) session.getTransaction().rollback();
} finally {
HibernateUtil.close(session);
}
}
@Test
public void testDelete01() {
Session session = null;
try {
session = HibernateUtil.openSession();
session.beginTransaction();
User u = (User)session.load(User.class,7);
session.delete(u);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
if(session!=null) session.getTransaction().rollback();
} finally {
HibernateUtil.close(session);
}
}
@Test
public void testDelete02() {
Session session = null;
try {
session = HibernateUtil.openSession();
session.beginTransaction();
User u = new User();
u.setId(2);
session.delete(u);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
if(session!=null) session.getTransaction().rollback();
} finally {
HibernateUtil.close(session);
}
}
@Test
public void testList() {
Session session = null;
try {
session = HibernateUtil.openSession();
List<User> users = session.createQuery("from User").list();
for(User u:users) {
System.out.println(u);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
HibernateUtil.close(session);
}
}
@Test
public void testPager() {
//按条件查询。查询从第一个id开始的总共2条数据。可以实现分页。
Session session = null;
try {
session = HibernateUtil.openSession();
List<User> users = session.createQuery("from User")
.setFirstResult(0)
.setMaxResults(2).list();
for(User u:users) {
System.out.println(u);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
HibernateUtil.close(session);
}
}
}
4、在User.java中添加toString()方法。
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + ", nickname=" + nickname + ", born=" + born + "]";
}