简体   繁体   中英

Spring MVC with Spring Data JPA

The following tutorial ( Spring Data JPA Tutorial ) explains how to get started with an application that uses Spring Data JPA to store and retrieve data in a relational database.

I'm currently having some trouble getting this to work together with SpringMVC. I have a very simple MVC app and I'm trying to integrate Spring Data JPA into it.

The above tutorial is unfortunately not very helpful in my case since it doesn't explain how to use Spring Data together with MVC.

The tutorial explains how to set up an Application class where all the necessary beans are configured and then it has a simple main method that uses one of the example repositories.

My question is: How do I configure my MVC app in the same way as in the tutorial so that I can start using Spring Data in MVC?

Here's the code of the Application class just for reference:

@Configuration
@EnableJpaRepositories
public class Application {

    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder().setType(H2).build();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
        LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
        lef.setDataSource(dataSource);
        lef.setJpaVendorAdapter(jpaVendorAdapter);
        lef.setPackagesToScan("hello");
        return lef;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setShowSql(false);
        hibernateJpaVendorAdapter.setGenerateDdl(true);
        hibernateJpaVendorAdapter.setDatabase(Database.H2);
        return hibernateJpaVendorAdapter;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager();
    }


    public static void main(String[] args) {
        AbstractApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
        CustomerRepository repository = context.getBean(CustomerRepository.class);

        // save a couple of customers
        repository.save(new Customer("Jack", "Bauer"));
        repository.save(new Customer("Chloe", "O'Brian"));
        repository.save(new Customer("Kim", "Bauer"));
        repository.save(new Customer("David", "Palmer"));
        repository.save(new Customer("Michelle", "Dessler"));

        // fetch all customers
        Iterable<Customer> customers = repository.findAll();
        System.out.println("Customers found with findAll():");
        System.out.println("-------------------------------");
        for (Customer customer : customers) {
            System.out.println(customer);
        }
        System.out.println();

        // fetch an individual customer by ID
        Customer customer = repository.findOne(1L);
        System.out.println("Customer found with findOne(1L):");
        System.out.println("--------------------------------");
        System.out.println(customer);
        System.out.println();

        // fetch customers by last name
        List<Customer> bauers = repository.findByLastName("Bauer");
        System.out.println("Customer found with findByLastName('Bauer'):");
        System.out.println("--------------------------------------------");
        for (Customer bauer : bauers) {
            System.out.println(bauer);
        }

        context.close();
    }

}

Personally, I started with Spring by downloading applications on github.com, with built-in config files :
download for example petclinic project (select Download .zip)
Then, if you use Spring Tool Suite : File > Import > Maven > Existing Maven Projects > Browse > ...
So that you don't have to worry about the .xml config files, they are installed at the same time.

I'm doing the same thing that you are trying to do. Start with the small starter tutorials and then go from there. This is how my main method looks like after merging mvc and data:

public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(Application.class,args);
PostRepository repository = context.getBean(PostRepository.class);
}

(my repository here is called PostRepository because I'm creating a blog app and I store "Post" objects in my database)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM