QuerydslPredicate和泛型类

Yur*_*ken 18 java generic-collections querydsl spring-data

我有很多控制器使用相同的方法,差异只是实体类.

我想创建通用的BaseController并且QuerydslPredicate注释有问题(不能设置root,这是通用的)

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);
   }
}
Run Code Online (Sandbox Code Playgroud)

Method extractTypeInfo from QuerydslPredicateArgumentResolver return T.但是需要Entity类.

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

@QuerydslPredicate(root = T.class)
Run Code Online (Sandbox Code Playgroud)

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

Mik*_*kov 3

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

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

弹簧底座

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

实体

@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;
}
Run Code Online (Sandbox Code Playgroud)

控制器

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

存储库

@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> {
}
Run Code Online (Sandbox Code Playgroud)

它尽可能地工作和抽象。尽管如此,你仍然需要产生额外的@Controller@Repository类别@Entity。尽管它们大部分都是空的存根。但我不认为你可以完全欺骗 @Querydsl 或 Spring Data 来正确地联合使用泛型。