gpt4 book ai didi

java - Spring Boot高级查询

转载 作者:行者123 更新时间:2023-12-02 12:24:38 25 4
gpt4 key购买 nike

谁能解释一下如何在 crudrepository 中添加高级查询,例如在我的例子中,我想按用户的名字搜索用户。

下面是我的AppUsers

public class AppUsers {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int id;
@Column(name = "firstname")
public String firstName;
@Column(name = "lastname")
public String lastName;

public AppUsers() {
}

public AppUsers(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

}

这是 AppUsers 存储库:

package com.developer.SpringMySQL.models;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;


public interface AppUsersRepo extends PagingAndSortingRepository<AppUsers,Integer> {

}

这是 Controller :

@Controller
public class MainController {

private AppUsersRepo usersRepository;

@Autowired
AppUsersRepo appRepo;

@RequestMapping("/")
public ModelAndView doHome() {
ModelAndView mv = new ModelAndView("index");
mv.addObject("lists", appRepo.findAll());
return mv;
}
}

最佳答案

这是存储库方法的命名约定。在您的情况下,您需要在存储库接口(interface)中创建一个方法:

设置 findByFirstName(String pFirstName);

有关命名约定的更多信息,请参阅以下链接:

https://spring.io/guides/gs/accessing-data-mysql/

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/

关于java - Spring Boot高级查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45532440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com