Android 创建一个RESTAPI,我需要手动查询,比如从condition=false的表中选择*。我应该在哪里写那个条件

Android 创建一个RESTAPI,我需要手动查询,比如从condition=false的表中选择*。我应该在哪里写那个条件,android,spring,rest,api,Android,Spring,Rest,Api,@查询(value=“select*from student where active=true”,nativeQuery=true) Iterable findactivestudents(字符串名) 我应该能够使用API将此查询用于@getmapping,您可以像这样在查询中传递参数 @Query(select p from Person p where p.forename = :forename and p.surname = :surname) User findByForenameA

@查询(value=“select*from student where active=true”,nativeQuery=true) Iterable findactivestudents(字符串名)


我应该能够使用API将此查询用于
@getmapping
,您可以像这样在查询中传递参数

@Query(select p from Person p where p.forename = :forename and p.surname = :surname)
User findByForenameAndSurname(@Param("surname") String lastname,
     @Param("forename") String firstname);

您可以像这样在查询中传递参数

@Query(select p from Person p where p.forename = :forename and p.surname = :surname)
User findByForenameAndSurname(@Param("surname") String lastname,
     @Param("forename") String firstname);

您应该将此查询添加到相应的存储库文件中

import java.util.Optional;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

@EnableTransactionManagement
@Repository
public interface StudentRepository extends CrudRepository<Student, Integer> {

@Query("SELECT * from Student where active = ?1",nativeQuery= true)
public Optional<List<Student>> findActiveStudents(boolean active);

}
import java.util.Optional;
导入org.springframework.data.jpa.repository.Modifying;
导入org.springframework.data.jpa.repository.Query;
导入org.springframework.data.repository.crudepository;
导入org.springframework.data.repository.query.Param;
导入org.springframework.stereotype.Repository;
导入org.springframework.transaction.annotation.EnableTransactionManagement;
导入org.springframework.transaction.annotation.Transactional;
@启用事务管理
@存储库
公共界面StudentRepository扩展了crudepository{
@查询(“从学生中选择*,其中活动=?1”,nativeQuery=true)
公共可选findActiveStudents(布尔活动);
}
这里Student是对应的POJO文件,Integer是它的主键的数据类型

您可以在控制器文件中自动连接此文件并调用此公共方法--

@Autowired
私立学校;私立学校;
List studentList=studentRepository.findActiveStudents(true.get();

您应该将此查询添加到相应的存储库文件中

import java.util.Optional;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

@EnableTransactionManagement
@Repository
public interface StudentRepository extends CrudRepository<Student, Integer> {

@Query("SELECT * from Student where active = ?1",nativeQuery= true)
public Optional<List<Student>> findActiveStudents(boolean active);

}
import java.util.Optional;
导入org.springframework.data.jpa.repository.Modifying;
导入org.springframework.data.jpa.repository.Query;
导入org.springframework.data.repository.crudepository;
导入org.springframework.data.repository.query.Param;
导入org.springframework.stereotype.Repository;
导入org.springframework.transaction.annotation.EnableTransactionManagement;
导入org.springframework.transaction.annotation.Transactional;
@启用事务管理
@存储库
公共界面StudentRepository扩展了crudepository{
@查询(“从学生中选择*,其中活动=?1”,nativeQuery=true)
公共可选findActiveStudents(布尔活动);
}
这里Student是对应的POJO文件,Integer是它的主键的数据类型

您可以在控制器文件中自动连接此文件并调用此公共方法--

@Autowired
私立学校;私立学校;
List studentList=studentRepository.findActiveStudents(true.get();