-1

我是 Spring 的新用户,我想用 Hibernate-PostGreSQL 和 Spring Boot 开发一个 RestFull 服务。我尝试使用 Spring 的文档来学习,但是部署一个简单的服务有很多问题。我不使用 XML 文件属性,而是使用 Java 类。

这是我的不同文件:

持久性JPAConfig.java:

package com.spring.configuration;

import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

@Configuration
public class PersistenceJPAConfig {
  @Bean
  public DataSource dataSource() {
    DriverManagerDataSource driver = new DriverManagerDataSource();
    driver.setDriverClassName("org.postgresql.Driver");
    driver.setUrl("jdbc:postgresql://localhost:5432/test");
    driver.setUsername("test");
    driver.setPassword("test");
    return driver;
  }

  @Bean
  public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setDatabase(Database.POSTGRESQL);
    vendorAdapter.setGenerateDdl(true);

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan(getClass().getPackage().getName());
    factory.setDataSource(dataSource());

    return factory;
  }

  @Bean
  @Autowired
  public JpaTransactionManager transactionManager() {
    JpaTransactionManager txManager = new JpaTransactionManager();
    txManager.setEntityManagerFactory(entityManagerFactory().getObject());

    return txManager;
  }
}

我有一个经典模型,这里是存储库:

package com.spring.persistence.repositories;

import com.spring.persistence.model.ApplicationUser;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
@Qualifier(value = "applicationUserRepository")
public interface ApplicationUserRepository extends JpaRepository<ApplicationUser,Long>{

}

一个简单的服务:

package com.spring.persistence.service;

import com.spring.persistence.model.ApplicationUser;
import com.spring.persistence.repositories.ApplicationUserRepository;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class ApplicationUserService {

    private final ApplicationUserRepository applicationUserRepository;

    @Autowired
    public ApplicationUserService(ApplicationUserRepository applicationUserRepository) {
      this.applicationUserRepository = applicationUserRepository;
    }


    public ApplicationUser createUser(String username, String type, String country)
    {
        ApplicationUser user = new ApplicationUser(username,type,country);
        return applicationUserRepository.saveAndFlush(user);
    }
    public List<ApplicationUser> getAllUser()
    {
      return applicationUserRepository.findAll();
    }
    public ApplicationUser getUser(Long id)
    {
        ApplicationUser user = null;
        if(id != null)
        {
            user = applicationUserRepository.findOne(id);
        }
        return user;
    }
    public boolean deleteUser(Long id)
    {
        if(id != null)
        {
            try{
                applicationUserRepository.delete(id);
                return true;
            }
            catch(IllegalArgumentException ex)
            {
                ex.printStackTrace();
                return false;
            }
        }
        else
        {
            System.out.println("Id is null");
            return false;
        }   
    }
}

最后是 WebController :

@RestController
@RequestMapping(value = "/applicationuser")
public class ApplicationUserController {

    @Autowired
    private ApplicationUserService applicationUserService;

    @RequestMapping(value="/",method = RequestMethod.GET)
    @ResponseBody
    public ApplicationUser index()
    {
        return applicationUserService.createUser("test", "test", "test");

    }

}

可能缺少很多东西(注释,初始化程序,代码),但我在这里学习,任何建议都可以帮助我。

感谢您的回答

4

1 回答 1

-1

春季数据休息

该项目将使您能够以更少的样板代码实现您的目标。

遵循使用 REST 访问 JPA 数据指南,该指南演示了如何以最少的配置配置 Spring Boot + Spring Data REST。

一旦你有了基本的了解,你就可以添加更多的功能来满足你的业务需求。

Spring Data REST 文档中提供了详细信息

于 2016-07-28T15:02:10.163 回答