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);
    
    
    
        }
    }
  • 相关阅读:
    vue 优化hash持久化缓存
    用vue的抽象组件来做一个防止img标签url为空或url地址出错的验证
    读源码学会一些编程小技巧
    webpack编译后的代码如何在浏览器执行
    vue@cli3 public目录下的静态图片,如何使用在css类文件中(sass可行,纯css不行)
    vue@cli3 项目模板怎么使用public目录下的静态文件,找了好久都不对,郁闷!
    vscode如何配置ts的lint,如何配置才能让eslint和prettier不冲突一键格式化代码(vue开发使用)
    rollup 使用babel7版本的插件rollup-plugin-babel,rollup-plugin-babel使用报错解决办法。
    深入研究webpack之Tree Shaking相关属性sideEffects用处
    前端性能优化之http缓存
  • 原文地址:https://www.cnblogs.com/shouyaya/p/13111266.html
Copyright © 2011-2022 走看看