gpt4 book ai didi

java - 扩展现有的 spring jpa 存储库功能

转载 作者:太空宇宙 更新时间:2023-11-04 10:34:42 25 4
gpt4 key购买 nike

我正在使用 Spring 与 mysql 数据库通信。我有一个实体类和一个扩展 CrudRepository 的接口(interface)。一切都很好 - 读/写等。

我想“扩展”findAll 方法。我想在返回之前操作从 findAll 接收到的数据。

用户类别:

@Entity
@Table(name = "user")
public class User
{
private String name;
private String age;
private String type;

getters/setters
}

仓库:

@Repository
public interface UserRepo extends CrudRepository<User, Long> {
List<User> findAll();
Map<String, String> afterManipulatedFindAllData();
}

我希望 afterManipulatedFindAllData() 能够按照我的喜好操作 findAll 数据。

这可能吗?

已添加查看@BoristheSpider 链接后:

interface UserRepository {
Map<String, String> afterManipulatedFindAllData();
}

class UserRepositoryImpl implements UserRepository{

public Map<String, String> afterManipulatedFindAllData() {
////how this method receive the 'findAll' data?//////
}
}

public interface UserRepo extends CrudRepository<User, Long>, UserRepository
{
List<User> findAll();
Map<String, String> afterManipulatedFindAllData();
}

非常感谢,视频

最佳答案

您始终可以通过为其指定 JPA 查询来在存储库中定义新方法:

@Component
public interface UsersRepository extends JpaRepository<User, UUID> {

public List<User> findByEntityStatus(EntityStatus status);

@Query("from User user left outer join fetch user.areas where user.code = :code")
public User findByCode(@Param("code") String code);
}

也许这对你有用?

关于java - 扩展现有的 spring jpa 存储库功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49608516/

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