Java 无法使用spring boot将数据保存到数据库
当我运行测试类Java 无法使用spring boot将数据保存到数据库,java,hibernate,spring-boot,unit-testing,Java,Hibernate,Spring Boot,Unit Testing,当我运行测试类public void testCreate()时,测试运行时没有错误,但我无法将任何数据保存到数据库中 我创建了一个Product.java模型类,然后使用创建的ProductRepository.java扩展crudepository.java与MySQL数据库交互 我的Spring boot版本是2.2.0.0版本 以下是我的课程: Product.java package com.hibernate.productData.entities; import javax
public void testCreate()
时,测试运行时没有错误,但我无法将任何数据保存到数据库中
我创建了一个Product.java
模型类,然后使用创建的ProductRepository.java
扩展crudepository.java
与MySQL数据库交互
我的Spring boot版本是2.2.0.0版本
以下是我的课程:
Product.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;}
}
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
}
}
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);
}
}
ProductRepository.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;}
}
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
}
}
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>
4.0.0
org.springframework.boot
spring启动程序父级
2.2.0.1发布
com.hibernate.productData
产品数据
0.0.1-快照
产品数据
Hibernate项目
1.8
org.springframework.boot
spring引导启动器数据jpa
mysql
mysql连接器java
运行时
org.springframework.boot
弹簧起动试验
测试
org.junit.vintage
朱尼特老式发动机
org.springframework.boot
springbootmaven插件
测试正在运行,没有错误,但我无法将任何数据保存到数据库
我认为这可能是因为您已经覆盖了Crudepository的save方法。我在任何地方都没有见过这样做。尝试将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>
{
}
package com.hibernate.productData.repository;
导入org.springframework.data.repository.crudepository;
导入org.springframework.stereotype.Repository;
导入com.hibernate.productData.entities.Product;
@存储库
公共接口ProductRepository扩展了Crudepository
{
}
我认为正在发生的是,正在调用您的save方法的实现,而该实现没有任何作用。发布您的application.properties和pom.xml,以帮助其他人找到您的问题所在。添加..@chaojunchong当我删除这些方法时,ProductRepository.java给我一个错误,说“add umimplemented methods”提示我从中添加方法CrudRepository@AnantVikramSingh更新了我的答案