我是Spring的新用户,我希望使用Hibernate-PostGreSQL和Spring Boot开发RestFull服务。我尝试学习Spring的文档,但是我部署一个简单的服务有很多问题。 我不使用XML文件属性,而是使用Java类。
以下是我的不同文件:
PersistanceJPAConfig.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");
}
}
可能缺少很多东西(注释,初始化程序,代码),但我在这里学习,任何建议都可以帮助我。
感谢您的回答
答案 0 :(得分:-1)
该项目将使您能够以极少的样板代码实现目标。
按照Accessing JPA Data with REST指南进行操作,该指南演示了如何使用绝对最少的配置配置Spring Boot + Spring Data REST。
一旦您有了基本的了解,就可以添加更多功能以满足您的业务需求。
中提供