Spring boot具体get怎么写我只能举例说明

Spring boot how to write a specific get i can only describe with an example

The database I'm talking about 您好,我想创建一个 returns 的 GET 端点,例如每个项目的 route_short_name 只有 route_long_name = Autobuz。我的实体文件看起来像这样,我正在使用 Jpa 存储库,我不知道我应该做什么,我还有一个服务和一个控制器 class,我正在尝试实现所有这些。

package stpt.entities;

import javax.persistence.*;

@Entity
@Table(name = "routes")
public class Routes {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "route_id")
private String routeId;
@Column(name = "route_short_name")
private String routeShortName;
@Column(name = "route_long_name")
private String routeLongName;

public Routes() {
}

public String getRouteId() {
    return this.routeId;
}

public String getRouteShortName() {
    return this.routeShortName;
}

public String getRouteLongName() {
    return this.routeLongName;
}

public void setRouteId(String routeId) {
    this.routeId = routeId;
}

public void setRouteShortName(String routeShortName) {
    this.routeShortName = routeShortName;
}

public void setRouteLongName(String routeLongName) {
    this.routeLongName = routeLongName;
}

public boolean equals(final Object o) {
    if (o == this) return true;
    if (!(o instanceof Routes)) return false;
    final Routes other = (Routes) o;
    if (!other.canEqual((Object) this)) return false;
    final Object this$routeId = this.getRouteId();
    final Object other$routeId = other.getRouteId();
    if (this$routeId == null ? other$routeId != null : !this$routeId.equals(other$routeId)) return false;
    final Object this$routeShortName = this.getRouteShortName();
    final Object other$routeShortName = other.getRouteShortName();
    if (this$routeShortName == null ? other$routeShortName != null : !this$routeShortName.equals(other$routeShortName))
        return false;
    final Object this$routeLongName = this.getRouteLongName();
    final Object other$routeLongName = other.getRouteLongName();
    if (this$routeLongName == null ? other$routeLongName != null : !this$routeLongName.equals(other$routeLongName))
        return false;
    return true;
}

protected boolean canEqual(final Object other) {
    return other instanceof Routes;
}

public int hashCode() {
    final int PRIME = 59;
    int result = 1;
    final Object $routeId = this.getRouteId();
    result = result * PRIME + ($routeId == null ? 43 : $routeId.hashCode());
    final Object $routeShortName = this.getRouteShortName();
    result = result * PRIME + ($routeShortName == null ? 43 : $routeShortName.hashCode());
    final Object $routeLongName = this.getRouteLongName();
    result = result * PRIME + ($routeLongName == null ? 43 : $routeLongName.hashCode());
    return result;
}

public String toString() {
    return "Routes(routeId=" + this.getRouteId() + ", routeShortName=" + this.getRouteShortName() + ", routeLongName=" + this.getRouteLongName() + ")";
}
}

检查这个简单的例子。希望对你有帮助。

// creating controller to receive requests
@RestController
@RequestMapping("/route")
public class RouteController {
    
    @Autowired // it's not a best practice to call repository on controllers. this only to example
    private RouteRepository routeRepository;

    @GetMapping("/way1/{routeLongName}")
    public ResponseEntity<List<Routes>> getByLongName(@PathVariable("routeLongName") String routeLongName){
        return new ResponseEntity<List<Routes>>( routeRepository.listRoutesByLongName(routeLongName) ,  HttpStatus.OK );
    }
    
    @GetMapping("/way2") // this the best way for api design in your case
    public ResponseEntity<List<Routes>> getByLongNameOtherWay(Routes route){
        return new ResponseEntity<List<Routes>>( routeRepository.listRoutesByLongName(route.getRouteLongName()) ,  HttpStatus.OK );
    }
}

// creating repositoty/dao to read database
@Repository
public interface RouteRepository extends JpaRepository<Routes,String> {
   
   // using jpql to describe a select
   @Query("select obj from #{#entityName} obj where routeLongName = ?1 ")
   List<Routes> listRoutesByLongName(String routeLongName);

}

不要忘记在 pom.xml

中导入 spring 数据依赖
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

在 运行 之后调用请求 spring启动应用程序

 http://localhost:8080/route/way1/Autobuz
 
 http://localhost:8080/route/way2?routeLongName=Autobuz