如何使用 Spring Data JPA 对两列求和?
How to sum up two columns using Spring Data JPA?
我想在我的数据库项目上总结两列,我正在使用 Spring Boot - Java,但我不知道我应该做什么
在数据库中我有那些列
id
、user
、value_income
、outcome_value
、total
和 income_property
我想做的基本上是从 value_income
和 outcome_value
从数据库中检索数据并在后端汇总
我的用户存储库:
package com.saturnssolutions.TakeMoney.repositories;
import com.saturnssolutions.TakeMoney.models.UserModel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.math.BigDecimal;
import java.util.List;
public interface UserRepository extends JpaRepository<UserModel, Integer> {
@Query("SELECT UserModel.id, (UserModel.valueIncome + UserModel.outcomeValue) as AddedValues FROM money")
List<UserModel> findAllByTotal;
}
ps:我记得ps 收到错误
我的用户控制器:
package com.saturnssolutions.TakeMoney.controllers;
import com.saturnssolutions.TakeMoney.models.UserModel;
import com.saturnssolutions.TakeMoney.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.NoSuchElementException;
@RestController
@RequestMapping("/api/v1")
public class UserController {
@Autowired
UserService userService;
@CrossOrigin
@GetMapping("/users")
public List<UserModel> listAllUsers() {
return userService.listAllUsers();
}
@CrossOrigin
@GetMapping("/users/{id}")
public ResponseEntity<UserModel> users(@PathVariable Integer id) {
try {
UserModel user = userService.getUser(id);
return new ResponseEntity<UserModel>(user, HttpStatus.OK);
} catch (NoSuchElementException e) {
return new ResponseEntity<UserModel>(HttpStatus.NOT_FOUND);
}
}
@CrossOrigin
@PostMapping("/users")
public void users(@RequestBody UserModel user) {
userService.saveUser(user);
}
@CrossOrigin
@PutMapping("/users/{id}")
public ResponseEntity<?> update(@RequestBody UserModel user, @PathVariable Integer id) {
try {
UserModel existUser = userService.getUser(id);
user.setId(id);
userService.saveUser(user);
return new ResponseEntity<>(HttpStatus.OK);
} catch (NoSuchElementException e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@CrossOrigin
@DeleteMapping("/users/{id}")
public void delete(@PathVariable Integer id) {
userService.deleteUser(id);
}
@CrossOrigin
@GetMapping("/users/total")
public Float getTotal(){
return userService.getTotal();
}
}
我的服务:
package com.saturnssolutions.TakeMoney.services;
import com.saturnssolutions.TakeMoney.models.UserModel;
import com.saturnssolutions.TakeMoney.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.List;
@Service
@Transactional
public class UserService {
@Autowired
private UserRepository userRepository;
public List<UserModel> listAllUsers() {
return userRepository.findAll();
}
public void saveUser(UserModel user) {
userRepository.save(user);
}
public UserModel getUser (Integer id) {
return userRepository.findById(id).get();
}
public void deleteUser (Integer id) {
userRepository.deleteById(id);
}
public List<UserModel> getTotal() {
return userRepository.findAllByTotal();
}
}
和我的模型:
package com.saturnssolutions.TakeMoney.models;
import javax.persistence.*;
import java.math.BigDecimal;
@Entity
@Table(name = "money")
public class UserModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String user;
private Float valueIncome;
private Float outcomeValue;
private Float total;
private Boolean incomeProperty;
public Boolean getIncomeProperty() {
return incomeProperty;
}
public void setIncomeProperty(Boolean incomeProperty) {
this.incomeProperty = incomeProperty;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public Float getValueIncome() {
return valueIncome;
}
public void setValueIncome(Float valueIncome) {
this.valueIncome = valueIncome;
}
public Float getOutcomeValue() {
return outcomeValue;
}
public void setOutcomeValue(Float outcomeValue) {
this.outcomeValue = outcomeValue;
}
public Float getTotal() {
return total;
}
public Float setTotal(Float total) {
this.total = total;
return total;
}
public UserModel() {
}
public UserModel(Integer id, String user, Float valueIncome, Float outcomeValue, Float total) {
this.id = id;
this.user = user;
this.valueIncome = valueIncome;
this.outcomeValue = outcomeValue;
this.total = total;
}
public Integer getId() {
return id;
}
public Integer setId(Integer id) {
return this.id = id;
}
}
你的存储库的这个版本应该可以工作:
public interface UserRepository extends JpaRepository<UserModel, Integer> {
@Query("SELECT id, valueIncome + outcomeValue AS AddedValues FROM money", nativeQuery=true)
List<Object[]> findAllByTotal;
}
请注意,在 JPA 查询中引用 UserModel
将不起作用,因为 JPA 需要 table 名称。此外,不需要别名,因为您只是 select 针对单个 table/entity.
此外,您的查询必须作为本机查询执行,因为它 returns 计算列。您还可以仅使用 select 语句中的两个字段来定义自定义 POJO。使用前一个 List<Object[]>
选项,您的代码应该类似于:
List<Object[]> vals = userRepository.findAllByTotal();
for (Object[] row : vals) {
int id = (Integer)row[0];
double addedValues = (Double)row[1];
}
您可能需要尝试在上面的 for 循环中进行强制转换才能使其正常工作。
我想在我的数据库项目上总结两列,我正在使用 Spring Boot - Java,但我不知道我应该做什么
在数据库中我有那些列
id
、user
、value_income
、outcome_value
、total
和 income_property
我想做的基本上是从 value_income
和 outcome_value
从数据库中检索数据并在后端汇总
我的用户存储库:
package com.saturnssolutions.TakeMoney.repositories;
import com.saturnssolutions.TakeMoney.models.UserModel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.math.BigDecimal;
import java.util.List;
public interface UserRepository extends JpaRepository<UserModel, Integer> {
@Query("SELECT UserModel.id, (UserModel.valueIncome + UserModel.outcomeValue) as AddedValues FROM money")
List<UserModel> findAllByTotal;
}
ps:我记得ps 收到错误
我的用户控制器:
package com.saturnssolutions.TakeMoney.controllers;
import com.saturnssolutions.TakeMoney.models.UserModel;
import com.saturnssolutions.TakeMoney.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.NoSuchElementException;
@RestController
@RequestMapping("/api/v1")
public class UserController {
@Autowired
UserService userService;
@CrossOrigin
@GetMapping("/users")
public List<UserModel> listAllUsers() {
return userService.listAllUsers();
}
@CrossOrigin
@GetMapping("/users/{id}")
public ResponseEntity<UserModel> users(@PathVariable Integer id) {
try {
UserModel user = userService.getUser(id);
return new ResponseEntity<UserModel>(user, HttpStatus.OK);
} catch (NoSuchElementException e) {
return new ResponseEntity<UserModel>(HttpStatus.NOT_FOUND);
}
}
@CrossOrigin
@PostMapping("/users")
public void users(@RequestBody UserModel user) {
userService.saveUser(user);
}
@CrossOrigin
@PutMapping("/users/{id}")
public ResponseEntity<?> update(@RequestBody UserModel user, @PathVariable Integer id) {
try {
UserModel existUser = userService.getUser(id);
user.setId(id);
userService.saveUser(user);
return new ResponseEntity<>(HttpStatus.OK);
} catch (NoSuchElementException e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@CrossOrigin
@DeleteMapping("/users/{id}")
public void delete(@PathVariable Integer id) {
userService.deleteUser(id);
}
@CrossOrigin
@GetMapping("/users/total")
public Float getTotal(){
return userService.getTotal();
}
}
我的服务:
package com.saturnssolutions.TakeMoney.services;
import com.saturnssolutions.TakeMoney.models.UserModel;
import com.saturnssolutions.TakeMoney.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.List;
@Service
@Transactional
public class UserService {
@Autowired
private UserRepository userRepository;
public List<UserModel> listAllUsers() {
return userRepository.findAll();
}
public void saveUser(UserModel user) {
userRepository.save(user);
}
public UserModel getUser (Integer id) {
return userRepository.findById(id).get();
}
public void deleteUser (Integer id) {
userRepository.deleteById(id);
}
public List<UserModel> getTotal() {
return userRepository.findAllByTotal();
}
}
和我的模型:
package com.saturnssolutions.TakeMoney.models;
import javax.persistence.*;
import java.math.BigDecimal;
@Entity
@Table(name = "money")
public class UserModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String user;
private Float valueIncome;
private Float outcomeValue;
private Float total;
private Boolean incomeProperty;
public Boolean getIncomeProperty() {
return incomeProperty;
}
public void setIncomeProperty(Boolean incomeProperty) {
this.incomeProperty = incomeProperty;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public Float getValueIncome() {
return valueIncome;
}
public void setValueIncome(Float valueIncome) {
this.valueIncome = valueIncome;
}
public Float getOutcomeValue() {
return outcomeValue;
}
public void setOutcomeValue(Float outcomeValue) {
this.outcomeValue = outcomeValue;
}
public Float getTotal() {
return total;
}
public Float setTotal(Float total) {
this.total = total;
return total;
}
public UserModel() {
}
public UserModel(Integer id, String user, Float valueIncome, Float outcomeValue, Float total) {
this.id = id;
this.user = user;
this.valueIncome = valueIncome;
this.outcomeValue = outcomeValue;
this.total = total;
}
public Integer getId() {
return id;
}
public Integer setId(Integer id) {
return this.id = id;
}
}
你的存储库的这个版本应该可以工作:
public interface UserRepository extends JpaRepository<UserModel, Integer> {
@Query("SELECT id, valueIncome + outcomeValue AS AddedValues FROM money", nativeQuery=true)
List<Object[]> findAllByTotal;
}
请注意,在 JPA 查询中引用 UserModel
将不起作用,因为 JPA 需要 table 名称。此外,不需要别名,因为您只是 select 针对单个 table/entity.
此外,您的查询必须作为本机查询执行,因为它 returns 计算列。您还可以仅使用 select 语句中的两个字段来定义自定义 POJO。使用前一个 List<Object[]>
选项,您的代码应该类似于:
List<Object[]> vals = userRepository.findAllByTotal();
for (Object[] row : vals) {
int id = (Integer)row[0];
double addedValues = (Double)row[1];
}
您可能需要尝试在上面的 for 循环中进行强制转换才能使其正常工作。