【问题标题】:In Eclipse how can we can connect H2 database using Hibernate for Maven project在 Eclipse 中,我们如何使用 Hibernate for Maven 项目连接 H2 数据库
【发布时间】:2018-05-09 06:23:52
【问题描述】:

如何使用 Hibernate for Maven 项目连接 H2 数据库?

【问题讨论】:

    标签: spring hibernate maven spring-mvc h2


    【解决方案1】:

    您可以使用 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());
        }
    
    ...
    

    【讨论】:

      猜你喜欢
      • 2016-12-24
      • 1970-01-01
      • 2021-05-14
      • 2014-10-24
      • 2018-07-30
      • 2016-11-26
      • 2017-12-31
      • 2012-05-03
      • 2020-01-31
      相关资源
      最近更新 更多