获取Name和Price属性的空值,并使用Spring JPARepository保存到Mysql。

huangapple go评论64阅读模式
英文:

Getting null value of Name and Price Property and Saving to Mysql using Spring JPARepository

问题

我面临获取名称和价格属性的空值以及在Spring Boot JPA中使用JpaRepository接口保存到MySQL时的问题。它只保存了cid字段到MySQL数据库,对于名称和价格字段,保存了null值。我正在使用MySQL数据库。

POJO类

@Data
@Entity
@Table(name = "COURSE_DTLS")
public class Course {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer cid;
	private String name;
	private Double price;
}

Repository类

import java.io.Serializable;
import org.springframework.data.jpa.repository.JpaRepository;
import in.ashokit.binding.Course;

public interface CourseRepository extends JpaRepository<Course, Serializable> {

}

Service类

@Service
public class CourseServiceImpl implements CourseService {

	@Autowired
	private CourseRepository courseRepo;

	@Override
	public String upsert(Course course) {

		courseRepo.save(course); // upsert (insert / update based on PK)

		return "success";
	}

	@Override
	public Course getById(Integer cid) {
		Optional<Course> findById = courseRepo.findById(cid);

		if (findById.isPresent()) {
			return findById.get();
		}
		return null;
	}

	@Override
	public List<Course> getAllCourses() {
		return courseRepo.findAll();
	}

	@Override
	public String deleteById(Integer cid) {
		if (courseRepo.existsById(cid)) {
			courseRepo.deleteById(cid);
			return "Delete Success";
		} else {
			return "No Record Found";
		}
	}
}

Controller类

@RestController
public class CourseRestController {

	@Autowired
	private CourseService courseService;

	@PostMapping("/course")
	public ResponseEntity<String> createCourse(@RequestBody Course course) {
		String status = courseService.upsert(course);
		return new ResponseEntity<>(status, HttpStatus.CREATED);
	}

	@GetMapping("/course/{cid}")
	public ResponseEntity<Course> getCourse(@PathVariable Integer cid) {
		Course course = courseService.getById(cid);
		return new ResponseEntity<>(course, HttpStatus.OK);
	}

	@GetMapping("/courses")
	public ResponseEntity<List<Course>> getAllCourses() {
		List<Course> allCourses = courseService.getAllCourses();
		return new ResponseEntity<>(allCourses, HttpStatus.OK);
	}

	@PutMapping("/course")
	public ResponseEntity<String> updateCourse(@RequestBody Course course) {
		String status = courseService.upsert(course);
		return new ResponseEntity<>(status, HttpStatus.OK);
	}

	@DeleteMapping("/course/{cid}")
	public ResponseEntity<String> deleteCourse(@PathVariable Integer cid) {
		String status = courseService.deleteById(cid);
		return new ResponseEntity<>(status, HttpStatus.OK);
	}
}

获取Name和Price属性的空值,并使用Spring JPARepository保存到Mysql。

英文:

I am facing issue regarding getting null value of Name and Price Property and Saving to Mysql using Spring bootJPA using interface JpaRepository. It saved data only cid to mysql db for all other fields saving null for Name and price fields in MySql Db. I am using MySql database.

POJO Class


@Data
@Entity
@Table(name = &quot;COURSE_DTLS&quot;)
public class Course {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer cid;
	private String name;
	private Double price;
}

Repo Class

import java.io.Serializable;

import org.springframework.data.jpa.repository.JpaRepository;

import in.ashokit.binding.Course;

public interface CourseRepository extends JpaRepository&lt;Course, Serializable&gt; {

}

Service class

@Service
public class CourseServiceImpl implements CourseService {

	@Autowired
	private CourseRepository courseRepo;

	@Override
	public String upsert(Course course) {

		courseRepo.save(course); // upsert (insert / update based on PK)

		return &quot;success&quot;;
	}

	@Override
	public Course getById(Integer cid) {
		Optional&lt;Course&gt; findById = courseRepo.findById(cid);

		if (findById.isPresent()) {
			return findById.get();
		}
		return null;
	}

	@Override
	public List&lt;Course&gt; getAllCourses() {
		return courseRepo.findAll();
	}

	@Override
	public String deleteById(Integer cid) {
		if (courseRepo.existsById(cid)) {
			courseRepo.deleteById(cid);
			return &quot;Delete Success&quot;;
		} else {
			return &quot;No Record Found&quot;;
		}
	}

}

Controller class

@RestController
public class CourseRestController {

	@Autowired
	private CourseService courseService;

	@PostMapping(&quot;/course&quot;)
	public ResponseEntity&lt;String&gt; createCourse(@RequestBody Course course) {
		String status = courseService.upsert(course);
		return new ResponseEntity&lt;&gt;(status, HttpStatus.CREATED);
	}

	@GetMapping(&quot;/course/{cid}&quot;)
	public ResponseEntity&lt;Course&gt; getCourse(@PathVariable Integer cid) {
		Course course = courseService.getById(cid);
		return new ResponseEntity&lt;&gt;(course, HttpStatus.OK);
	}

	@GetMapping(&quot;/courses&quot;)
	public ResponseEntity&lt;List&lt;Course&gt;&gt; getAllCourses() {
		List&lt;Course&gt; allCourses = courseService.getAllCourses();
		return new ResponseEntity&lt;&gt;(allCourses, HttpStatus.OK);
	}

	@PutMapping(&quot;/course&quot;)
	public ResponseEntity&lt;String&gt; updateCourse(@RequestBody Course course) {
		String status = courseService.upsert(course);
		return new ResponseEntity&lt;&gt;(status, HttpStatus.OK);
	}

	@DeleteMapping(&quot;/course/{cid}&quot;)
	public ResponseEntity&lt;String&gt; deleteCourse(@PathVariable Integer cid) {
		String status = courseService.deleteById(cid);
		return new ResponseEntity&lt;&gt;(status, HttpStatus.OK);
	}

}

获取Name和Price属性的空值,并使用Spring JPARepository保存到Mysql。

答案1

得分: 1

你的代码库有误。

将这部分代码:

JpaRepository<Course, Serializable>

修改为:

JpaRepository<Course, Integer>

英文:

Your repository is wrong.

Change this:

JpaRepository&lt;Course, Serializable&gt;

Like this:

JpaRepository&lt;Course, Integer&gt;

答案2

得分: 1

JpaRepository 中的第二个值应该是实体类主键的类型,在您的情况下是 Integer。请按以下方式更改 Repo 类:

public interface CourseRepository extends JpaRepository<Course, Integer> {

}
英文:

The second value in JpaRepository should be the type of primary key of the entity class which in your case is Integer. Change Repo class as follows:

public interface CourseRepository extends JpaRepository&lt;Course, Integer&gt; {

}

huangapple
  • 本文由 发表于 2023年5月28日 11:14:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定