步骤:
1.新建new-other
2.选择Junit Test Case
3.注意红线圈出部分
4.选择需要测试的方法
5.Finish后出现如下
示例代码:
package com.jrgc.dao; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import com.jrgc.entity.User; import com.jrgc.util.SessionUtil; public class UserDaoImp implements UserDao{ @SuppressWarnings("unchecked") @Override public List<User> userList() { Session session = SessionUtil.getSession(); /** * from User中的User是实体类(entity) */ String hql = "from User"; return session.createQuery(hql).list(); } @Override public void save(User u) { Session session = SessionUtil.getSession(); Transaction tran = session.beginTransaction(); try { session.save(u); tran.commit(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); tran.rollback(); }finally{ SessionUtil.closeSession(); } } }
时间: 2024-10-13 12:43:02