我有一个User类实体:
@Entity
@Table(name = "USERS")
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@json-id")
public class User extends AbstractEntity {
@Id
@SequenceGenerator(name = "USERS_SEQ", sequenceName = "S_USER_ACCOUNT")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USERS_SEQ")
@Column(name = "USER_ID", precision = 22, scale = 0)
private Long userId;
@Column(name = "ID_TYPE")
private String idType;
@Column(name = "COUNTRY_CODE", length = 3, nullable = false)
@NotBlank
private String countryCode;
}
}
和类UserRepository从JpaRepository扩展
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
Page<User> findbyUser(User users, Pageable pageable);
}
当构建和编译源时,我有一条错误消息:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property findbyUser found for type User!
我想用实体中的任何字段搜索用户。你能说出你的想法吗?
答案 0 :(得分:1)
谢谢大家。我刚刚研究了这个答案的解决方案。效果很好。
Example<User> example = Example.of(user);
Page<User> pageResult = usersRepository.findAll(example, pageable);
答案 1 :(得分:0)
您可以使用if(req.url == '/') {
filename = '/public/index.html'
} else {
filename = path.basename(parsedURL.pathname);
}
注释按任何字段进行搜索,例如:
@Query
按countryCode搜索:
@Query("SELECT user FROM User user WHERE (:userId IS NULL OR user.userId = :userId) " +
"AND (:idType IS NULL OR user.idType = :idType) " +
"AND (:countryCode IS NULL OR user.countryCode = :countryCode) ")
Page<User> search(@Param("userId") Long userId, @Param("idType") String idType,
@Param("countryCode") String countryCode, Pageable pageable);
按类型和countryCode搜索:
userRepository.search(null, null, "US", new PageRequest(page, pageSize));
按ID搜索:
userRepository.search(null, "someType", "US", new PageRequest(page, pageSize));
答案 2 :(得分:0)
我建议你制作3种不同的方法:
userRepository.search(1L, null, null, new PageRequest(page, pageSize));
Page<User> findAllByUserId(Long userId, Pageable pageable);
Page<User> findAllByIdType(String idType, Pageable pageable);
在Page<User> findAllByCountryCode(String countryCode, Pageable pageable);
中,获取所有网页的内容并将其合并为一个@Service
请注意,这些方法可以返回用户列表,而Page<User>
可以返回@Controller
。(这可以使Page<User>
响应的构建更轻松,更容易)< / p>