• 基于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);
    
    
    
        }
    }
  • 相关阅读:
    php编译错误:jpeglib.h not found.
    php编译错误:Please reinstall the libcurl distribution
    php编译错误:Cannot find OpenSSL's <evp.h>
    php shmop windows 信号量锁
    vsftpd配置
    putty ssh 证书登录及问题
    mysql被收购 用mariadb
    centos 7 php7 yum源
    3dmax 欢迎页卡住
    用python进行服务器的监控
  • 原文地址:https://www.cnblogs.com/shouyaya/p/13111266.html
Copyright © 2020-2023  润新知