Java 在crudepository中打印FindAll()中的记录
我正在我的存储库类中扩展crudepository。我想使用findAll方法打印表中的记录。到目前为止,我已经编写了一个测试类,我可以看到结果查询是正确的。如何打印表中的单个记录 以下是我的代码片段: 存储库类Java 在crudepository中打印FindAll()中的记录,java,entitymanager,hibernate-entitymanager,Java,Entitymanager,Hibernate Entitymanager,我正在我的存储库类中扩展crudepository。我想使用findAll方法打印表中的记录。到目前为止,我已经编写了一个测试类,我可以看到结果查询是正确的。如何打印表中的单个记录 以下是我的代码片段: 存储库类 public interface RepositoryAda extends CrudRepository{ } 服务等级 @Service public class Service{ @Autowired private RepositoryAda repository;
public interface RepositoryAda extends CrudRepository{
}
服务等级
@Service
public class Service{
@Autowired private RepositoryAda repository;
@Transactional
public List selectRecords(){
return (List) repository.findAll();
}
}
测试用例:
@Test
public void getAllRecords() {
service.selectRecords();
}
如何将单个记录从表打印到控制台?我更喜欢在使用存储库界面时使用。您可以用一行将findAll()
Iterable
转换为列表
public RecordRepository extends CrudRepository<Record, Long> {}
public class RecordServiceImple implements RecordService {
RecordRepository recordRepository;
public List<Record> selectRecord() {
return Lists.newArrayList(recordRepository.findAll()); // Guava library
// or just simply cast it.
// return (List<Record>)recordRepository.findAll();
}
}
只需使用将记录
类中的toString()
或您的类名改写为表格格式,也就是说将它们打印到表格中是什么意思?是否只想以表格格式将记录打印到控制台?或者您不知道如何遍历Iterable
?我需要将它们打印到控制台。列表来自哪个导入?基于此行:return Lists.newArrayList(recordRepository.findAll());这是番石榴图书馆的。查看链接有没有其他不使用Guava库的方法来执行此操作?是的,只需将其转换为return(List)recordRepository.findAll()代码>
for (Record record : records) {
System.out.println(record);
}