代码如下所示:
package com.localapp.hibernate; import static org.junit.Assert.fail; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class JUnitSysUserDao { //定义局部变量 Session session = null; Transaction transaction = null; SysUserDAO sysUserDAO = null; SysUser sysUser = null; private static SessionFactory sf = null; @BeforeClass public static void beforeClass() { //防止出现Junit的“静默”BUG的方法一 try { sf = new AnnotationConfiguration().configure().buildSessionFactory(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @AfterClass public static void tearDownAfterClass() throws Exception { } @Before public void setUp() throws Exception { sysUserDAO = new SysUserDAO(); session = sysUserDAO.getSession(); } @After public void tearDown() throws Exception { session.close(); sysUserDAO = null; } @Test public void testSave() { //fail("Not yet implemented"); //sysUser.setUserId(3); SysUser sysUser= new SysUser(); sysUser.setUserName("aaa"); // Configuration cfg=new Configuration(); // SessionFactory sf = cfg.configure().buildSessionFactory(); Session session = sf.openSession(); session.beginTransaction();//执行操作 session.save(sysUser); session.getTransaction().commit();//提交 session.close(); } @Test public void testFindById() { // transaction = this.session.beginTransaction(); // sysUser = sysUserDAO.findById(2); // //transaction.commit(); // System.out.println("sysUser="+sysUser.getUserName()+": "+sysUser.getUserId()); // Assert.assertNotNull(sysUser); SysUser sysUser= new SysUser(); Session session = sf.openSession(); session.beginTransaction();//执行操作 sysUser=sysUserDAO.findById(6); System.out.println("sysUser="+sysUser.getUserName()+": "+sysUser.getUserId()); session.getTransaction().commit();//提交 session.close(); } // public final void testFindById() { //测试查询记录 // Dept 部门 = null; // // 部门 = this.部门访问对象.findById((long) 20); // Assert.assertNotNull(部门); // } @Test public final void testDelete() { // SysUser sysUser= new SysUser(); // sysUser.setUserName("测试"); // Session session = sf.openSession(); // session.beginTransaction();//执行操作 // session.delete(sysUser); // session.getTransaction().commit();//提交 // session.close(); /*** * 以下是第二种测试 */ SysUser sysUser= new SysUser(); Session session = sf.openSession(); session.beginTransaction();//执行操作 sysUser=sysUserDAO.findById(6); session.delete(sysUser); session.getTransaction().commit();//提交 session.close(); } // public final void testDelete() { //测试删除记录 // Dept 部门 = new Dept(); // 部门.setDeptno((long) 90); // 部门.setDname("测试部门"); // 部门.setLoc("中国-佛山"); // // 事例 = this.会话.beginTransaction(); // this.部门访问对象.delete(部门); // 事例.commit(); // // 部门 = null; // 部门 = this.部门访问对象.findById((long) 90); // // Assert.assertNull(部门); // } }