发布网友 发布时间:2022-04-08 02:09
共2个回答
懂视网 时间:2022-04-08 06:30
配置过程省略。
数据库连接的公共类HibernateUtils.java
package test.hibernate.dao; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils { private static SessionFactory sessionFactory; static { // Configuration configuration=new Configuration(); // configuration.configure();//读取默认的文件hibernate.cfg.xml // // configuration.configure("hibernate.cfg.xml");读取指定文件 // sessionFactory=configuration.buildSessionFactory(); sessionFactory = new Configuration()// .configure()// .buildSessionFactory(); } /* * 获得一个全局唯一的SessionFactory * * @return */ public static SessionFactory getSessionFactory() { return sessionFactory; } /* * 从全局SessionFactory中打开一个Session */ public static Session openSession() { return sessionFactory.openSession(); } public static void setSessionFactory(SessionFactory sessionFactory) { HibernateUtils.sessionFactory = sessionFactory; } }
QueryResult.java
package test.hibernate.dao; import java.util.List; public class QueryResult { private int count; private List list; public QueryResult(int count, List list) { this.count = count; this.list = list; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public List getList() { return list; } public void setList(List list) { this.list = list; } }业务实现类UserDao.java
package test.hibernate.dao; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; import test.hibernate.domain.User; /** * @author LinDL * */ public class UserDao { public void save(User user) { Session session = HibernateUtils.openSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); session.save(user); transaction.commit(); } catch (RuntimeException e) { // TODO: handle exception transaction.rollback(); throw e; } finally { } session.close(); } public void update(User user) { Session session = HibernateUtils.openSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); session.update(user); transaction.commit(); } catch (RuntimeException e) { // TODO: handle exception transaction.rollback(); throw e; } finally { } session.close(); } public void delete(int id) { Session session = HibernateUtils.openSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); User user = (User) session.get(User.class, id);// 要先获取到实体对象 session.delete(user);// 删除的是实体对象 transaction.commit(); } catch (RuntimeException e) { // TODO: handle exception transaction.rollback(); throw e; } finally { } session.close(); } /** * @param id * @return */ public User getById(int id) { Session session = HibernateUtils.openSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); User user = (User) session.get(User.class, id); transaction.commit(); return user; } catch (RuntimeException e) { // TODO: handle exception transaction.rollback(); throw e; } finally { session.close(); } } /** * @return */ public List<User> findAll() { Session session = HibernateUtils.openSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); // 使用HQL查询 // List<User> all = session.createQuery("from User").list(); // 使用面向对象的方式查询 Criteria criteria = session.createCriteria(User.class); // criteria.add(Restrictions.eq("id", 5));// 添加限制条件,查询id等于5的记录 // criteria.add(Restrictions.ge("id", 6));// // 查询id大于等于6的记录,lt表小于,le表小于等于 // criteria.addOrder(Order.asc("id"));// 添加排序条件 List<User> all = criteria.list(); transaction.commit(); return all; } catch (RuntimeException e) { // TODO: handle exception transaction.rollback(); throw e; } finally { session.close(); } } /** * @param firstResult * @param maxResult * @return */ @SuppressWarnings("unchecked") public QueryResult findAll(int firstResult, int maxResult) { Session session = HibernateUtils.openSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); // 使用HQL查询 // Query query = (Query) session.createQuery("from User"); // query.setFirstResult(firstResult); // query.setMaxResults(maxResult); // List<User> all = query.list(); List<User> all = session.createQuery("from User") .setFirstResult(firstResult)// .setMaxResults(maxResult)// .list(); // 查询总记录数 Long count = (Long) session .createQuery("select count(*) from User").uniqueResult(); transaction.commit(); return new QueryResult(count.intValue(), all); } catch (RuntimeException e) { // TODO: handle exception transaction.rollback(); throw e; } finally { session.close(); } } }测试类
package test.hibernate.dao; import static org.junit.Assert.*; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; import test.hibernate.domain.User; public class UserDaoTest { UserDao userDao = new UserDao(); @Test public void testSave() { User user = new User(); // user.setName("张三"); // userDao.save(user); for (int i = 1; i < 30; i++) { user.setId(i); user.setName("test" + i); userDao.save(user); } } @Test public void testUpdate() { User user = userDao.getById(2); user.setName("历史"); userDao.update(user); } @Test public void testDelete() { userDao.delete(2); } @Test public void testGetById() { User user = userDao.getById(1); System.out.println(user); } @Test public void testFindAll() { List<User> list = userDao.findAll(); for (User user : list) { System.out.println(user); } } @Test public void testFindAllIntInt() { // QueryResult queryResult = userDao.findAll(0, 10); // QueryResult queryResult=userDao.findAll(10, 10); QueryResult queryResult = userDao.findAll(20, 10); System.out.println("总记录数" + queryResult.getCount()); for (Object user : queryResult.getList()) { System.out.println(user); } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。转载请注明出处http://blog.csdn.net/lindonglian
Hibernate之实现数据库的基本操作
标签:
热心网友 时间:2022-04-08 03:38
Hibernate对于数据库表的操作