IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> springboot的jpa配配置和测试方法 -> 正文阅读

[Java知识库]springboot的jpa配配置和测试方法

首先在yml里面配置数据

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://192.168.56.101:3306/orderfood?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT
    jpa:
      show-sql: true

一般来说是一张表一个接口,正常情况下
一张表对应一个接口
在这里插入图片描述
里面写数据库名字可以完成映射

主键和自增映射

在这里插入图片描述
在这里插入图片描述
写完接口之后直接去测试

url连接最好加上

jdbc:mysql://localhost:3306/test?serverTimezone=GMT&allowPublicKeyRetrieval=true&useSSL=false&characterEncoding=utf8;


规则就是先写一个一张表写一个模型
一张表要写一个模型

package com.example.wetchatorder.dateObject;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

//数据库默认是下划线写法对应驼峰写法
//你当然也可以选择用Table进行映射
//这个相当于数据库的模型了
@Entity //将数据库映射进来
public class ProductCategory {
    //类目Id
    @Id  //表示主键
    @GeneratedValue //表示自增长
    private Integer categoryId;
    private String categoryName;
    private Integer categoryType;

    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public Integer getCategoryType() {
        return categoryType;
    }

    public void setCategoryType(Integer categoryType) {
        this.categoryType = categoryType;
    }

    @Override
    public String toString() {
        return "ProductCategory{" +
                "categoryId=" + categoryId +
                ", categoryName='" + categoryName + '\'' +
                ", categoryType=" + categoryType +
                '}';
    }
}

直接根据模型去写接口
写完接口之后可以直接进行测试
在这里插入图片描述

package com.example.wetchatorder.repository;

import com.example.wetchatorder.dateObject.ProductCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ProductCategoryRespository extends JpaRepository<ProductCategory,Integer> {
                                                            //这里写的是数据模型和住建类型

}

直接进行测试

package com.example.wetchatorder.repository;

import com.example.wetchatorder.dateObject.ProductCategory;
import lombok.extern.slf4j.Slf4j;
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.Optional;

import static org.junit.jupiter.api.Assertions.*;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class ProductCategoryRespositoryTest {

    @Autowired //根据类注入属性
    private ProductCategoryRespository repository;

    @Test
    public void finOneTest(){
        //接口继承了jpaRepository,所以可以直接用方法进行测试
        Optional<ProductCategory> productCategory = repository.findById(1);
        ProductCategory productCategory1 = productCategory.get();
        System.out.println(productCategory1);
    }





}

先测试好能不能正确联通和交互。

package com.example.wetchatorder.repository;

import com.example.wetchatorder.dateObject.ProductCategory;
import lombok.extern.slf4j.Slf4j;
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 javax.transaction.Transactional;
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
@Slf4j
public class ProductCategoryRespositoryTest {

    @Autowired //根据类注入属性
    private ProductCategoryRespository repository;



    @Test
    @Transactional
    public void finOneTest(){
        //接口继承了jpaRepository,所以可以直接用方法进行测试
        Optional<ProductCategory> productCategory = repository.findById(1);
        //这个是查
        ProductCategory productCategory1 = productCategory.get();
        //把id为1的拿到封装为对象
        productCategory1.setCategoryType(15);//然后修改这个对象的值就相当于修改了数据库的值
        repository.save(productCategory1);
        System.out.println(productCategory1);
    }

    @Test
 //加在测试库用的,完全回滚,你所做的事情直接回滚,不会留下
    @Transactional
    public void saveTest(){

        ProductCategory productCategory1 = new ProductCategory();
        productCategory1.setCategoryId(2);
        productCategory1.setCategoryName("可爱的狗狗");
        productCategory1.setCategoryType(10);
        repository.save(productCategory1);//增删改查用save
        //save方法是之星增删改

    }

    //这两个方法实际上就是测试一下这个接口和数据库有没有进行联通

    @Test
    public void findByCategoryTypeIn(){
        List<Integer> testList = Arrays.asList(1,3,7);

        List<ProductCategory> result = repository.findByCategoryTypeIn(testList);
        for (ProductCategory p: result
             ) {
            System.out.println(p);

        }

    }






}
  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-08-08 11:07:41  更:2021-08-08 11:08:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/11 5:51:48-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码