gpt4 book ai didi

java - QuerydslPredicate 和泛型类

转载 作者:太空狗 更新时间:2023-10-29 22:35:50 24 4
gpt4 key购买 nike

我有许多具有相同方法的 Controller ,区别仅在于实体类。

我想创建通用的 BaseController 并且在使用 QuerydslPredicate 注释时遇到问题(无法设置根,这是通用的)

class abstract BaseController<T extends BaseEntity> {
@Autowired
private Repository<T, Long> repository;

@RequestMapping(method = RequestMethod.GET)
public Page<T> findAll(@QuerydslPredicate Predicate predicate, Pageable pageable) {
return repository.findAll(predicate, pageable);
}
}

方法 extractTypeInfo 从 QuerydslPredicateArgumentResolver 返回 T。但需要实体类。

而且我无法将根值设置为 QuerydslPredicate(没有类)

@QuerydslPredicate(root = T.class)

关于如何实现这一目标的任何帮助?

最佳答案

我认为没有办法为注解提供通用类。它应该是编译时常量。

但是这个呢? (大量代码,完整示例)。

Spring 底座

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

实体

@MappedSuperclass
@Data
@NoArgsConstructor
@AllArgsConstructor
public abstract class BaseEntity {

@Id
@Column(name = "id")
private Integer id;

@Column(name = "created_at")
private Date createdAt;
}

@Entity
@Table(name = "user")
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class User extends BaseEntity {

@Column(name = "username")
private String userName;
}

@Entity
@Table(name = "another_user")
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class AnotherUser extends BaseEntity {

@Column(name = "another_username")
private String anotherUserName;
}

Controller

public abstract class BaseEntityController<T extends BaseEntity> {

private final BaseEntityRepository<T> repository;

@Autowired
protected BaseEntityController(BaseEntityRepository<T> repository) {
this.repository = repository;
}

@GetMapping(value = "/index")
public Page<T> index(Predicate predicate, Pageable pageable) {
return repository.findAll(predicate, pageable);
}
}

@RestController
@RequestMapping("/user")
public class UserController extends BaseEntityController<User> {

protected UserController(BaseEntityRepository<User> repository) {
super(repository);
}

@Override
public Page<User> index(@QuerydslPredicate(root = User.class) Predicate predicate, Pageable pageable) {
return super.index(predicate, pageable);
}
}

@RestController
@RequestMapping("/anotheruser")
public class AnotherUserController extends BaseEntityController<User> {

protected AnotherUserController(BaseEntityRepository<User> repository) {
super(repository);
}

@Override
public Page<User> index(@QuerydslPredicate(root = AnotherUser.class) Predicate predicate, Pageable pageable) {
return super.index(predicate, pageable);
}
}

存储库

@NoRepositoryBean
public interface BaseEntityRepository<T extends BaseEntity> extends CrudRepository<T, Integer>, QuerydslPredicateExecutor<T> {
}

@Repository
public interface UserEntityRepository extends BaseEntityRepository<User> {
}

@Repository
public interface AnotherUserEntityRepository extends BaseEntityRepository<AnotherUser> {
}

它尽可能地工作和抽象。不过,您仍然需要为每个 @Entity 生成额外的 @Controller@Repository 类。尽管它们大多是空 stub 。但我认为您无法完全欺骗 @Querydsl 或 Spring Data 以在联合中正确使用泛型。

关于java - QuerydslPredicate 和泛型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37481905/

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