我已经将springboot连接到MySQL数据库。我想在HTTP请求中指定user_id
时显示用户名。例如http://8080/user/1
必须使用user_id
1显示用户名。
该表包含以下属性:
| Integer user_id; | String username; | String fathername; | String mothername;
我已经在Controller类中尝试过此代码,但是我似乎没有用
@RequestMapping("/{userid}")
@ResponseBody
public String getById(Integer userid) {
String name="";
try {
Optional<Persondetails> persondetails=persondetailsRepository.findById(personid);
name = String.valueOf(userdetails.getName());
}
catch (Exception ex) {
return "Name not found";
}
return "The Name of the user is : " + name;
}
我的存储库代码:
import java.util.List;
import java.util.Optional;
public interface UserdetailsRepository extends JpaRepository<Userdetails, Integer> {
public Optional<Userdetails> findById(Integer userid);
}
它说getName()
对于“可选”类型未定义
但是我已经在Userdetails类中定义了它
public class Userdetails {
@Id
@GeneratedValue
@Column(name="user_id")
private Integer userid;
@Column(name="name")
private String name;
@Column (name="fathers_name")
private String fathersname;
@Column(name="mothers_name")
private String mothersname;
public Userdetails() {
}
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getName() {
return name;
}
public void setname(String name) {
this.name = name;
}
public String getFathersname() {
return fathersname;
}
public void setFathersname(String fathersname) {
this.fathersname = fathersname;
}
public void setMothersname(String mothersname) {
this.mothersname = mothersname;
}
public String getMothersname() {
return mothersname;
}
}
答案 0 :(得分:0)
缺少方法类型GET,您可以通过两个选项进行操作:
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User findOne(@PathVariable("id") int id){
return userService.findById(id);
}
OR
@GetMapping("/{id}")
public String getString(@PathVariable("id") int id) {
return "Helloworld";
}
Spring boot与Mysql连接并获取数据。
application.properties
server.contextPath=/demo-user
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
* Controller.Java
@RequestMapping({"/users"})
public class UserController {
@Autowired
private UserService userService;
@GetMapping(path = {"/{id}"})
public User findOne(@PathVariable("id") int id){
return userService.findById(id);
}
}
UserService.java
public interface UserService {
User findById(int id);
}
UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository repository;
@Override
public User findById(int id) {
return repository.findOne(id);
}
}
UserRepository .java
public interface UserRepository extends Repository<User, Integer> {
User findOne(int id);
}
User.java
@Entity
@Table(name = "user")
public class User {
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
private String firstName;
@Column
private String lastName;
@Column
private String email;
//setter and getter
}
从浏览器或应用程序发出请求。