按条件查询的时候,Id默认为0 的情况

这种不带参数查询的时候,它会创建一个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);
        }


    }

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值