这种不带参数查询的时候,它会创建一个payformation对象,而这个对象Id默认为0;而在创建的对象它的id就有值了,然后进行查询
WHERE
1=1
ID=0
这样查询出来的就不是全部的,只是查ID=0的。并且如果是按参数查询不按id查询,他也会对ID进行设置,比如:
WHERE
1=1
AND custmerId = 12341278L
AND ID = 0L
依然会带着id=0,上面说的是按customerId进行查询,同样对id也进行了设置,这就会出现问题。
解决办法,统一在Mapper中加一句判断语句。
<if test="@Ognl@isNotEmpty(metalId) and metalId != 0"> AND ID =#{id} </if>
package org.haze.bpsms.balance;
import org.haze.base.util.UniqueIdUtil;
import org.haze.bpsms.base.BaseSpringJUnitTest;
import org.haze.bpsms.balance.model.PaymentInformation;
import org.haze.bpsms.balance.service.PaymentInformationService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
public class PaymentInformationServiceTest extends BaseSpringJUnitTest {
@Resource
private PaymentInformationService paymentInformationService;
private static Long id1;
@Before
public void setUp(){
try {
id1 = UniqueIdUtil.genId();
}catch (Exception e){
e.printStackTrace();
}
}
@After
public void des(){
}
@Test
public void testInsert(){
try {
PaymentInformation paymentInformation = new PaymentInformation();
paymentInformation.setPaymentId(id1);
paymentInformation.setDepartmentId(6876L);
paymentInformation.setCustomerId(1533200726011L);
paymentInformation.setUserId(13234L);
paymentInformation.setDocDate(new Date());
paymentInformation.setKind("收入");
paymentInformation.setExpenses(32145.34);
paymentInformation.setStatusKey("wouldApproval");
paymentInformation.setDocumentCode("1530684656644");
paymentInformationService.insert(paymentInformation);
/*使用参数查询,并检验是否insert正确*/
paymentInformation.setDocDate(null);
List<PaymentInformation> list = paymentInformationService.select(paymentInformation);
Assert.notNull(list);
Assert.isTrue(list.size() >= 1 );
} catch (Exception e) {
/*运行出错则验证失败*/
Assert.isTrue(false);
}
}
@Test
public void testSelect() {
try {
Assert.notNull(paymentInformationService);
/*不带参数查询*/
PaymentInformation paymentInformation = new PaymentInformation();
paymentInformation.setPaymentId(null);
List<PaymentInformation> list = paymentInformationService.select(paymentInformation);
Assert.notNull(list);
Assert.isTrue(list.size() > 0);
/*带参数查询,可以带多个参数,测试mapper中的select */
paymentInformation.setPaymentId(1533364181668L);
list = paymentInformationService.select(paymentInformation);
Assert.isTrue(list.size() >= 1 );
} catch (Exception e) {
// e.printStackTrace();
Assert.isTrue(false);
}
}
@Test
public void testUpdate(){
try {
PaymentInformation paymentInformation = new PaymentInformation();
paymentInformation.setPaymentId(1533361470888L);
paymentInformation.setDepartmentId(6876L);
paymentInformation.setCustomerId(1533200726011L);
paymentInformation.setUserId(13234L);
paymentInformation.setDocDate(new Date());
paymentInformation.setKind("收入");
paymentInformation.setExpenses(32145.34);
paymentInformation.setStatusKey("wouldApproval");
paymentInformation.setDocumentCode("1530684656644");
paymentInformationService.update(paymentInformation);
/*使用参数查询,并检验是否update正确*/
paymentInformation.setDocDate(null);
List<PaymentInformation> list = paymentInformationService.select(paymentInformation);
Assert.notNull(list);
Assert.isTrue(list.size() >= 1 );
} catch (Exception e) {
/*运行出错则验证失败*/
Assert.isTrue(false);
}
}
@Test
public void testSelectOne(){
try {
/* 测试 使用id作为参数的查询 ,仅返回最多一个结果*/
PaymentInformation paymentInformation = paymentInformationService.selectOne(1533361470888L);
Assert.isTrue(paymentInformation.getDocumentCode().equalsIgnoreCase("1530684656644"));
} catch (Exception e) {
Assert.isTrue(false);
// e.printStackTrace();
}
}
@Test
public void testDelete(){
try {
/*使用单个id参数测试删除*/
String ids = "1533361494175";
paymentInformationService.delById(ids);
/*使用多个id参数测试删除,中间不能包含空格*/
ids = "1533355561092,1533360107486";
paymentInformationService.delById(ids);
} catch (Exception e) {
e.printStackTrace();
Assert.isTrue(false);
}
}
}