买家商品DAO

2018-11-19  本文已影响0人  谢谢水果

新建数据表映射过来的对象

main--dataobject--ProductInfo.java

package com.tkft.sell.dataobject;

import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.math.BigDecimal;

@Entity
@Data
public class ProductInfo {
    @Id
    private String productId;

    private String productName;

    private BigDecimal productPrice;

    private Integer productStock;

    private String productDescription;

    private String productIcon;

    private Integer productStatus;

    private Integer categoryType;
}

DAO层

main--repository--ProductInfoRepository.class

package com.tkft.sell.repository;

import com.tkft.sell.dataobject.ProductInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;

public interface ProductInfoRepository extends JpaRepository<ProductInfo, String> {
    List<ProductInfo> findByProductStatus(Integer productStatus);
}

测试

选中方法名 -- 右键 -- go to -- test
test--repository--ProductInfoRepositoryTest.class

package com.tkft.sell.repository;

import com.tkft.sell.dataobject.ProductInfo;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.math.BigDecimal;
import java.util.List;
import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductInfoRepositoryTest {

    @Autowired
    private ProductInfoRepository repository;

    @Test
    public void saveTest(){
        ProductInfo productInfo = new ProductInfo();
        productInfo.setProductId("123456");
        productInfo.setCategoryType(53);
        productInfo.setProductDescription("好吃好吃");
        productInfo.setProductName("苹果");
        productInfo.setProductStatus(0);
        productInfo.setProductIcon("www.apple.com");
        productInfo.setProductPrice(new BigDecimal(3.5));
        productInfo.setProductStock(100);

        ProductInfo result = repository.save(productInfo);
        Assert.assertNotNull(result);
    }
    @Test
    public void findByProductStatus() {
        List<ProductInfo> result = repository.findByProductStatus(0);
        Assert.assertNotEquals(0, result.size());
    }
}
上一篇 下一篇

猜你喜欢

热点阅读