簡體   English   中英

無法使用 spring 引導將數據保存到數據庫

[英]Not able to save data to the database using spring boot

當我運行測試 class public void testCreate()時,測試運行沒有錯誤,但我無法將任何數據保存到數據庫。

I have created a Product.java model class, and then using created a ProductRepository.java extending CrudRepository.java to interact with the MySQL DB.

我的 Spring 啟動版本是 2.2.0.RELEASE 下面是我的課程:

產品.java

package com.hibernate.productData.entities;



import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.Table;



@Entity

@Table

public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String name;

    @Column(name="description")
    private String desc;

    private Double price;

    public int getId() {

    return id;

}

public void setId(int id) {this.id = id;}

public String getName() {return name;}

public void setName(String name) {this.name = name;}

public String getDesc() {return desc;}

public void setDesc(String desc) {this.desc = desc;}

public Double getPrice() {return price;}

public void setPrice(Double price) {this.price = price;}

}

ProductRepository.java

package com.hibernate.productData.repository;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.hibernate.productData.entities.Product;

@Repository
public class ProductRepository implements CrudRepository<Product, Integer>
{
@Override
public <S extends Product> S save(S entity) {
// TODO Auto-generated method stub
return null;
}



@Override

public <S extends Product> Iterable<S> saveAll(Iterable<S> entities) {

// TODO Auto-generated method stub

return null;

}



@Override

public Optional<Product> findById(Integer id) {

// TODO Auto-generated method stub

return null;

}



@Override

public boolean existsById(Integer id) {

// TODO Auto-generated method stub

return false;

}



@Override

public Iterable<Product> findAll() {

// TODO Auto-generated method stub

return null;

}



@Override

public Iterable<Product> findAllById(Iterable<Integer> ids) {

// TODO Auto-generated method stub

return null;

}



@Override

public long count() {

// TODO Auto-generated method stub

return 0;

}



@Override

public void deleteById(Integer id) {

// TODO Auto-generated method stub

}



@Override

public void delete(Product entity) {

// TODO Auto-generated method stub

}



@Override

public void deleteAll(Iterable<? extends Product> entities) {

// TODO Auto-generated method stub

}



@Override

public void deleteAll() {

// TODO Auto-generated method stub

}



}

產品數據ApplicationTests.java



package com.hibernate.productData;



import org.junit.jupiter.api.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 com.hibernate.productData.entities.Product;

import com.hibernate.productData.repository.ProductRepository;



@RunWith(SpringRunner.class)

@SpringBootTest

class ProductDataApplicationTests {



@Autowired

ProductRepository repos;

@Test

void contextLoads() {

}

@Test

public void testCreate()

{

Product p = new Product();

p.setId(1);

p.setName("harry potter");

p.setDesc("Awesome");

p.setPrice(100d);

    repos.save(p);

}

}

應用程序屬性

spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=Pblock@10

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hibernate.productData</groupId>
    <artifactId>productData</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>productData</name>
    <description>Hibernate project</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

測試運行沒有錯誤,但我無法將任何數據保存到數據庫。

我認為這可能是因為您覆蓋了 CrudRepository 的保存方法。 我在任何地方都沒有看到這樣做。 嘗試將 ProductRepository 的實現替換為

package com.hibernate.productData.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.hibernate.productData.entities.Product;

@Repository
public interface ProductRepository extends CrudRepository<Product, Integer>
{
}

我認為正在發生的是你的保存方法的實現被調用,它什么都不做。

您的值未保存在數據庫中的原因是您已經實現了CrudRepository ,如果這是您想要做的,那么您需要為overridden的方法提供您的實現,例如save()saveAll()等。

如果你想使用default implementation ,你可以擴展CrudRepository而不是擴展它。

package com.hibernate.productData.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.hibernate.productData.entities.Product;

@Repository
public interface ProductRepository extends CrudRepository<Product, Integer>
{
}

我希望這可以幫助您解決這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM