Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何返回GetMapping路径id的对象_Java_Spring Boot_Controller_Repository_Get Mapping - Fatal编程技术网

Java 如何返回GetMapping路径id的对象

Java 如何返回GetMapping路径id的对象,java,spring-boot,controller,repository,get-mapping,Java,Spring Boot,Controller,Repository,Get Mapping,控制器代码: @GetMapping(“/show/{id}”) 公共响应findId(@PathVariable Student)引发异常{ 学生showId=studentService.findId(学生); 返回新的响应属性(showId,HttpStatus.OK); } 需要返回路径id 存储库代码: public Student findId(Student-Student)引发异常{ 连接=空; 语句st=null; ResultSet rec=null; PreparedSt

控制器代码:

@GetMapping(“/show/{id}”)
公共响应findId(@PathVariable Student)引发异常{
学生showId=studentService.findId(学生);
返回新的响应属性(showId,HttpStatus.OK);
}
需要返回路径
id

存储库代码:

public Student findId(Student-Student)引发异常{
连接=空;
语句st=null;
ResultSet rec=null;
PreparedStatement pre=null;
List userlist=new ArrayList();
试一试{
Class.forName(“com.mysql.cj.jdbc.Driver”);
connect=DriverManager.getConnection(“jdbc:mysql://localhost/mydatabase“+”?user=root&password=root&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC”);
connect.setAutoCommit(false);
st=connect.createStatement();
String sql=“SELECT*FROM studentid=?”的学生;
pre=connect.prepareStatement(sql);
pre.setLong(1,student.getId());
pre.executeQuery();
}捕获(例外e){
if(connect!=null){
试一试{
圣克洛斯();
connect.rollback();
}捕获(SQLException e2){
e2.printStackTrace();
}
}
}最后{
如果(st!=null){
试一试{
圣克洛斯();
connect.setAutoCommit(true);
}捕获(SQLE异常){
e、 printStackTrace();
}
}
}
试一试{
connect.close();
}捕获(SQLE异常){
e、 printStackTrace();
}
留学生;
}
对于控制器,如果只需要id(@PathVariable Long id)更轻

Student response = studentService.findById(id);
return ResponseEntity.ok(response);
对于控制器,如果只需要id(@PathVariable Long id)更轻

Student response = studentService.findById(id);
return ResponseEntity.ok(response);

您是否通过传递实际的学生id来执行
SELECT*FROM student WHERE studentid=?
?是的,我是通过类型longy从数据库中获取id您没有形成学生对象。您是否通过传递实际的学生id来执行
SELECT*FROM student WHERE studentid=?
,我是按类型从数据库中获取id的,龙游没有形成学生对象。