我的控制器类是
package com.SystemFinal.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.SystemFinal.entity.Account;
import com.SystemFinal.repository.AccountRepository;
import com.SystemFinal.service.AccountService;
@RestController
@RequestMapping("api/acco")
@RequestMapping(value = "treansfer{acctID}/{destAcctID}/{amount}",method=RequestMethod.PUT, produces= {MimeTypeUtils.APPLICATION_JSON_VALUE},consumes= {MimeTypeUtils.APPLICATION_JSON_VALUE}, headers ="Accept=Application/json")
public ResponseEntity<Void> transferAmount(@PathVariable int acctID, @PathVariable int destAcctID, @PathVariable int amount){
try {
accountRepository.saveBalanceById(acctID,destAcctID, amount);
return new ResponseEntity<Void>(HttpStatus.OK);
}catch(Exception e) {
return new ResponseEntity<Void>(HttpStatus.BAD_REQUEST);
}
}
}
- 我的实体类是
package com.SystemFinal.entity;
import java.io.Serializable;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name="acco")
public class Account implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="account_number")
private int account_number;
@Column(name= "balance")
private int balance;
@Column(name="p_status")
private String p_status;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAccount_number() {
return account_number;
}
public void setAccount_number(int account_number) {
this.account_number = account_number;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
public String getP_status() {
return p_status;
}
public void setP_status(String p_status) {
this.p_status = p_status;
}
}
- 存储库类**
package com.SystemFinal.repository;
import java.lang.annotation.Repeatable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.SystemFinal.entity.Account;
@Repository("AccountRepository")
public interface AccountRepository extends CrudRepository<Account, Integer>,JpaRepository<Account, Integer>{
@Query(value="select balance from acco where id=?1",nativeQuery = true)
public int getBalanceById(int acc);
@Transactional
@Modifying(clearAutomatically = true)
@Query(value="update acco set balance=balance+?3 where id=?1,set balance=balance-?3 where id=?2",nativeQuery = true)
public void saveBalanceById(int id,int raccId ,int amount);
@Transactional
@Modifying(clearAutomatically = true)
@Query(value="update acco set balance=balance-?2 where id=?1",nativeQuery = true)
public void withdrawById(int id,int balance);
}
- 服务等级**
package com.SystemFinal.service;
import org.springframework.stereotype.Service;
import com.SystemFinal.entity.Account;
@Service
public interface AccountService {
public Iterable<Account> findAllAccount();
public Account findById(int id);
public void createAccount(Account acc);
public void updateAccount(Account acc);
public void deleteAccount(int id);
}
- 业务实现类**
import org.springframework.stereotype.Service;
import com.SystemFinal.entity.Account;
import com.SystemFinal.repository.AccountRepository;
@Service
public class AccountServiceImpl implements AccountService{
@Autowired
AccountRepository accountRepository;
@Override
public Iterable<Account> findAllAccount() {
return accountRepository.findAll();
}
@Override
public Account findById(int id) {
return accountRepository.findById(id).get();
}
@Override
public void createAccount(Account acc) {
accountRepository.save(acc);
}
@Override
public void updateAccount(Account acc) {
accountRepository.save(acc);
}
@Override
public void deleteAccount(int id) {
accountRepository.deleteById(id);
}
}
- 应用程序.属性**
server.port=8083
spring.datasource.url=jdbc:mysql://localhost:3306/bankdb
spring.datasource.username=root
spring.datasource.password=
server.servlet.context-path=/api
spring.jpa.hibernate.ddl-auto=update
- 聚合物. 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>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.SystemFinal</groupId>
<artifactId>SystemFinal</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SystemFinal</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>19</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我有一个名为acco的表,我尝试使用下面的查询更新其中的一列
@Transactional
@Modifying(clearAutomatically = true)
@Query(value="update acco set balance=balance+?3 where id=?1,set balance=balance-?3 where id=?2",nativeQuery = true)
public void saveBalanceById(int id,int raccId ,int amount);
我测试的时候发现
"时间戳":"2023 - 01 - 22T11:03:24.467 + 00:00","状态":404,"错误":"未找到","路径":"/api/api/acco/treansfer/1/2/20000 "
我是春靴新手,请帮帮我。
1条答案
按热度按时间jv4diomz1#
检查是否调用了正确的MapMap为:
api/acco/treansfer{acctID}/{destAcctID}/{amount}
网址是: