ssh下JUnit测试及相关hql语句的一点记录

本文档记录了在SSH框架中使用JUnit进行单元测试的方法,包括@Test注解、事务处理和Spring配置。同时展示了HQL查询语句的多种写法,如inner join和left join,以及通过日期和ID获取数据的例子。
摘要由CSDN通过智能技术生成

import static org.junit.Assert.*;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

 

 //设置junit运行的类

@RunWith(SpringJUnit4ClassRunner.class)

//设置运行时的spring配置文件
@ContextConfiguration(locations={"/spring/sa/applicationContext.xml" })

public class MytookDaoImplTest {
 @Autowired//这个也是要加上去的
  @Test
 @Transactional
 public void testGetBy_id() {
  //fail("Not yet implemented");
  Leave leave=(Leave)staffLeaveDao.getBy_id(8);
  System.out.println(leave.getStaff().getName()+leave.getLeavecase());
 }

 @Test
//当有事务存在的时候,测试的时候也要再加上事务处理,不然就会出现找不到session

 @Transactional
 public void testGetBy_state() {
  List<Leave> leaves=staffLeaveDao.getBy_state(2);
  for (Iterator<Leave> iterator = leaves.iterator(); iterator.hasNext();) {
   Leave leave = (Leave) iterator.next();
   System.out.println(leave.getStaff().getName()+leave.getLeavecase());
  }
 
  }

///一种得到list的写法
 List<CertificateManager> resulist=this.getSession()
  .createQuery("select c from CertificateManager c inner join c.certificate cf where cf.cernum=:cernum")
  .setString("cernum", cernum).list();

//一种得到结果的方法

Certifi certi=()getHibernateTemplate()
//  .find("from Certifi c left join Certi cf where cf.cernum=:cernum", cernum);

//li另一种写法:

      certif=(Certif)new Configuration().configure()
//  .buildSessionFactory().getCurrentSession()
//  .createQuery("from tablename where :id=:id ")
//  .setString("tablename",tablename)
//  .setInteger("id",id);

 

//其他写法

 List<Cert> certificateManagers=(List<Cert>)this.getSession()
  .createQuery("from Cert c where c.registime ='"+date+"'").list();
 //getHibernateTemplate().find("queryString", values)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值