• 基于springBoot使用JPA


    1.通过maven引入相关的jar包依赖

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>

    2.配置pojo(实体对象):

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity   //申明是个pojo对象
    @DynamicUpdate    //动态更新,
    @Data            //引入lombok包,可自动生成getter/setter/toString等方法
    public class ProductCategory {
        @Id         //设置主键
        @GeneratedValue(strategy = GenerationType.IDENTITY)     //自增属性,strategy表示自增策略
        private Integer categoryId;
        private String categoryName;
        private Integer categoryType;
    
        public ProductCategory() {
        }
    
        public ProductCategory(String categoryName,Integer categoryType){
            this.categoryName=categoryName;
            this.categoryType=categoryType;
        }
    }

    3.spring-data-jpa封装了基本的增删改查可直接使用:

    (Dao层)

    package com.yzy.sell.Repository;
    
    import com.yzy.sell.Entity.ProductCategory;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    import java.util.List;
    
    public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer> {
        List<ProductCategory> findByCategoryIdIn(List<Integer> categoryTypeList); //自定义的根据类目查询,方法需命名规范
    }

    测试:

    package com.yzy.sell.Repository;
    
    import com.yzy.sell.Entity.ProductCategory;
    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.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Optional;
    
    import static org.junit.jupiter.api.Assertions.*;
    
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ProductCategoryRepositoryTest {
    
        @Autowired
        private ProductCategoryRepository repository;
    
        @Test
        public void testFindOne(){
            ProductCategory productCategory = repository.findById(1).get();
            System.out.println(productCategory);
        }
        @Test
        public void testFindAll(){
            List<ProductCategory> all = repository.findAll();
            System.out.println(all);
            Assert.assertNotNull(all);
        }
        @Test
        public void testSave(){
            //新增
            ProductCategory productCategory=new ProductCategory("男女必买",10);
            ProductCategory result = repository.save(productCategory);
            Assert.assertEquals(productCategory,result);
            //修改
            ProductCategory category = repository.findById(1).get();
            category.setCategoryName("男人必买");
            repository.save(category);
        }
    
        @Test
        public void testFindByCategoryType(){
            List<ProductCategory> byCategoryIdIn = repository.findByCategoryIdIn(Arrays.asList(1, 2, 3, 10));
            System.out.println(byCategoryIdIn);
    
    
    
        }
    }
  • 相关阅读:
    数据结构——数据结构的起源和研究内容
    数据结构——学习数据结构的意义
    C++中动态内存申请的结果
    C++中函数异常规格的说明
    C++异常处理的深入理解
    NOIP 2012 Day2
    NOIP 2012 Day1
    NOIP 2011 Day2
    NOIP 2011 Day 1
    NOIP 2010
  • 原文地址:https://www.cnblogs.com/shouyaya/p/13111266.html
Copyright © 2020-2023  润新知