首先,简单介绍一下spring boot的配置和连接数据库获取数据(在Log in with Atlassian account 案例基础上进行修改)。
spingboot目录结构:
SpingMVCConfig.java:
(由于从Intellij IDEA中复制的代码在这里粘贴,结构会乱,所以这里用代码截图)
Product.java:
ApiController.java:
package com.example.odata.controller;
import com.example.odata.dao.HeroesDao;
import com.example.odata.entity.Hero;
import com.example.odata.entity.Product;
import com.example.odata.repository.ProductsRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.beans.PropertyDescriptor;
import java.util.*;
@RestController
@RequestMapping("/api")
public class ApiController
{
@Autowired
private ProductsRepository prodectsRepository;
@GetMapping(value = "/Products",
produces = "application/json; charset=UTF-8")
public List<Product> queryAllFromDB()
{
return prodectsRepository.findAll();
}
@GetMapping(value = "/Products/{id}",
produces = "application/json; charset=UTF-8")
public Optional<Product> queryOneFromDB(@PathVariable("id") Integer id)
{
return prodectsRepository.findById(id);
}
@PostMapping("/Products")
public boolean insertToDB(@RequestBody Product data)
{
prodectsRepository.save(data);
return true;
}
@PutMapping("/Products")
public boolean updateToDB(@RequestBody Product data)
{
prodectsRepository.save(data);
return true;
}
@PatchMapping("/Products/{id}")
public boolean updatePartToDB(@PathVariable("id") Integer id,
@RequestBody Product data)
{
if (prodectsRepository.existsById(id))
{
Product oldData = prodectsRepository.getById(id);
data.setProductid(id);
BeanUtils.copyProperties(data, oldData, getNullPropertyNames(data));
prodectsRepository.save(oldData);
return true;
}
return false;
}
@DeleteMapping("/Products/{id}")
pub