zoukankan      html  css  js  c++  java
  • 基于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);
    
    
    
        }
    }
  • 相关阅读:
    Exchange这东东…
    下午解决了一个问题
    PDC每日视频
    Delphi.net的IDE和C#Builder是相同的
    开始把准备把Exchange的一些基本操作和设置与SharePoint结合起来
    这两天忙得焦头烂额
    一个在.net下进行用户模拟的类
    SharePoint的相关链接
    今天才知有一个CollectionBase类,惭愧
    【博客堂杯征文】从服务员到程序员
  • 原文地址:https://www.cnblogs.com/shouyaya/p/13111266.html
Copyright © 2011-2022 走看看