javaweb框架之Hibernate3.6.0增删查该基本操作与Junit测试类

package cn.itcast.domain;

/**
 * 实体
 * @author Administrator
 *
 */
public class User {
	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + "]";
	}
}
package cn.itcast.utils;

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

public class HibernateUtils {
	private static SessionFactory sessionFactory;
	static {
		Configuration cfg = new Configuration();
		cfg.configure();
		sessionFactory = cfg.buildSessionFactory();
	}
	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	public static Session openSession() {
		return sessionFactory.openSession();
	}
	
	public static Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}
}

package cn.itcast.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.itcast.domain.User;
import cn.itcast.utils.HibernateUtils;

//增删改操作都是不需要有返回值的,出现错误就抛异常处理
public class UserDao {
	/**
	 * 保存
	 * 
	 * @param user
	 */
	public void save(User user) {
		Session session = HibernateUtils.openSession();	//打开Session
		Transaction tx = null;
		try {
		tx = session.beginTransaction();	//开始事物
		session.save(user);
		tx.commit();	//提交事物
		} catch(RuntimeException e) {
			tx.rollback();	//回滚事物
			throw e;
		} finally {
			session.close();	//关闭Session
		}
		
	}

	/**
	 * 更新
	 * 
	 * @param user
	 */
	public void update(User user) {
		Session session = HibernateUtils.openSession();	
		Transaction tx = null;
		try {
		tx = session.beginTransaction();	
		session.update(user);
		tx.commit();	
		} catch(RuntimeException e) {
			tx.rollback();	
			throw e;
		} finally {
			session.close();	
		}
		
	}

	/**
	 * 删除
	 * 
	 * @param user
	 */
	public void delete(int id) {
		Session session = HibernateUtils.openSession();	
		Transaction tx = null;
		try {
		tx = session.beginTransaction();	
		//做删除的时候要先获取对象再删除,用session.get方法获取
		User user = (User) session.get(User.class, id);
		session.delete(user);
		tx.commit();	
		} catch(RuntimeException e) {
			tx.rollback();
			throw e;
		} finally {
			session.close();	
		}
		
	}

	/**
	 * 根据id查找
	 * 
	 * @param user
	 * @return
	 */
	public User getById(int id) {
		Session session = HibernateUtils.openSession();	
		Transaction tx = null;
		try {
		tx = session.beginTransaction();	
		User user = (User) session.get(User.class,id);
		tx.commit();	
		return user;
		} catch(RuntimeException e) {
			tx.rollback();
			throw e;
		} finally {
			session.close();	
		}
	}

	/**
	 * 查询所有
	 * 
	 * @return
	 */
	public List findAll() {
		Session session = HibernateUtils.openSession();	
		Transaction tx = null;
		try {
		tx = session.beginTransaction();	
		List list = session.createQuery("FROM User").list();
		tx.commit();	
		return list;
		} catch(RuntimeException e) {
			tx.rollback();
			throw e;
		} finally {
			session.close();	
		}
	}

	/**
	 * 分页查询
	 * 
	 * @param user
	 * @return 一页的数据列表+总页数
	 */
	public QueryResult findAll(int firstResult, int maxResults) {
		Session session = HibernateUtils.openSession();	
		Transaction tx = null;
		try {
		tx = session.beginTransaction();
		//查询指定条目
		Query query = session.createQuery("FROM User");
		query.setFirstResult(firstResult);
		query.setMaxResults(maxResults);
		List list = query.list();
		Long count = (Long)session.createQuery("SELECT COUNT(*) FROM User").uniqueResult();
		tx.commit();	
		return new QueryResult(count.intValue(), list);
		} catch(RuntimeException e) {
			tx.rollback();
			throw e;
		} finally {
			session.close();	
		}
	}
}

package cn.itcast.dao;

import java.util.List;

import cn.itcast.domain.User;

public class QueryResult {
	private int count;
	private 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;
	}

	public QueryResult(int count, List list) {
		super();
		this.count = count;
		this.list = list;
	}

	public QueryResult() {
		super();
	}
}

package cn.itcast.dao;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.Test;

import cn.itcast.domain.User;

public class UserDaoTest {

	private UserDao userDao = new UserDao();
	@Test
	public void testSave() {
		User user = new User();
		user.setName("张三");
		userDao.save(user);
	}
	
	@Test
	public void testGetById() {
		User user = userDao.getById(5);
		System.out.println(user);
	}


	@Test
	public void testUpdate() {
		User user = userDao.getById(5);
		user.setName("李四");
		userDao.update(user);
	}

	@Test
	public void testDelete() {
		userDao.delete(5);

	}

	
	@Test
	public void testSave_24() {
		for(int i=1;i<=24;i++) {
			User user = new User();
			user.setName("test_" + i);
			userDao.save(user);
		}
		
	}
	
	
	@Test
	public void testFindAll() {
		List list = userDao.findAll();
		for(User user : list) {
			System.out.println(user);
		}
	}

	@Test
	public void testFindAllUser() {
		QueryResult qr = userDao.findAll(0,10);
		System.out.println(qr.getCount());
		for(User user : qr.getList()) {
			System.out.println(user);
		}
	}

}




	
		
		org.hibernate.dialect.SQLServer2008Dialect
		jdbc:sqlserver://localhost:1433;DatabaseName=hibernate
		com.microsoft.sqlserver.jdbc.SQLServerDriver
		sa
		root
		
		
		true
		
		
		
	





	
		
            
		
		
	



你可能感兴趣的:(自己用)