【发布时间】:2018-05-09 06:23:52
【问题描述】:
如何使用 Hibernate for Maven 项目连接 H2 数据库?
【问题讨论】:
标签: spring hibernate maven spring-mvc h2
如何使用 Hibernate for Maven 项目连接 H2 数据库?
【问题讨论】:
标签: spring hibernate maven spring-mvc h2
您可以使用 Spring-boot 初始化程序。只需访问此页面 - https://start.spring.io/
对于简单的 Spring WebMVC,选择下一个工具:Web、H2、JPA,我会推荐 DevTools。
在应用程序中,您需要像这样创建 JPA 存储库接口:
包 ru.arvsoft.server.core.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.arvsoft.server.core.domain.Author;
public interface AuthorRepository extends JpaRepository<Author, Long> {
}
并在某些类中使用它:
@Service
public class AuthorService {
private final AuthorRepository repository;
private final AuthorMapper mapper;
@Autowired
public AuthorService(AuthorRepository repository, AuthorMapper mapper) {
this.repository = repository;
this.mapper = mapper;
}
public Author getById(Long id) {
return repository.findOne(id);
}
public List<AuthorShortDTO> getAuthors() {
return repository.findAll().stream().map(mapper::mapToShortDTO).collect(Collectors.toList());
}
...
【讨论】: