【发布时间】:2017-05-08 09:06:43
【问题描述】:
我正在使用Spring Boot + JPA + Hibernate。我有实体/表用户。如何创建CrudRepositoty 一次插入用户列表?另外如何将结果作为某些查询的列表获取?
【问题讨论】:
标签: database hibernate spring-boot spring-data-jpa
我正在使用Spring Boot + JPA + Hibernate。我有实体/表用户。如何创建CrudRepositoty 一次插入用户列表?另外如何将结果作为某些查询的列表获取?
【问题讨论】:
标签: database hibernate spring-boot spring-data-jpa
制作一个实现 crudRepository 的接口。
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepo extends CrudRepository<User, Long> {
List<User> findAll();
}
这里 CrudRepository 为您的自定义函数隐式创建查询,例如上面代码中的 findAll()
服务类
@Service("userservice")
public class UserService {
@Autowired
UserRepo rep;
@Transactional
public ArrayList<User> findAll()
{
return (ArrayList<User>) rep.findAll();
}
}
以同样的方式你可以将你的对象保存为 rep.save(User object)
【讨论】: