Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. --APPLICATION PROPERTIES--
  2.  
  3. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  4. spring.datasource.url=jdbc:mysql://localhost:3306/bookstore
  5. spring.datasource.username=root
  6. spring.datasource.password=sean
  7. #Hibernate Configuration
  8. hibernate.dialect=org.hibernate.dialect.H2Dialect
  9. hibernate.hbm2ddl.auto=create-drop
  10. hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
  11. hibernate.show_sql=false
  12. hibernate.format_sql=true
  13.  
  14.  
  15.  
  16.  
  17. --CONFIG--
  18.  
  19. package com.sean.books;
  20.  
  21. import org.apache.tomcat.jdbc.pool.DataSource;
  22. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  23. import org.springframework.boot.orm.jpa.EntityScan;
  24. import org.springframework.context.annotation.Bean;
  25. import org.springframework.context.annotation.Configuration;
  26. import org.springframework.core.env.Environment;
  27. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  28. import org.springframework.orm.jpa.JpaTransactionManager;
  29. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  30. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  31. import org.springframework.transaction.annotation.EnableTransactionManagement;
  32.  
  33. import javax.persistence.EntityManagerFactory;
  34. import java.util.Properties;
  35.  
  36. /**
  37. * Created by Sean on 12/02/2016.
  38. */
  39.  
  40. @Configuration
  41. @EnableAutoConfiguration
  42. @EntityScan("com.sean.books")
  43. @EnableJpaRepositories(basePackages = {"com.sean.books"})
  44. @EnableTransactionManagement
  45. public class BookRepoConfig {
  46.  
  47. @Bean(destroyMethod = "close")
  48. DataSource dataSource(Environment env) {
  49. DataSource dataSourceConfig = new DataSource();
  50. dataSourceConfig.setDriverClassName(env.getRequiredProperty("spring.datasource.driver-class-name"));
  51. dataSourceConfig.setUrl(env.getRequiredProperty("spring.datasource.url"));
  52. dataSourceConfig.setUsername(env.getRequiredProperty("spring.datasource.username"));
  53. dataSourceConfig.setPassword(env.getRequiredProperty("spring.datasource.password"));
  54.  
  55. return new DataSource(dataSourceConfig);
  56. }
  57.  
  58. @Bean
  59. LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
  60. Environment env) {
  61. LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
  62. entityManagerFactoryBean.setDataSource(dataSource);
  63. entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
  64. entityManagerFactoryBean.setPackagesToScan("com.sean.books");
  65.  
  66. Properties jpaProperties = new Properties();
  67.  
  68. //Configures the used database dialect. This allows Hibernate to create SQL
  69. //that is optimized for the used database.
  70. jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
  71.  
  72. //Specifies the action that is invoked to the database when the Hibernate
  73. //SessionFactory is created or closed.
  74. jpaProperties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto")
  75. );
  76.  
  77. //Configures the naming strategy that is used when Hibernate creates
  78. //new database objects and schema elements
  79. jpaProperties.put("hibernate.ejb.naming_strategy", env.getRequiredProperty("hibernate.ejb.naming_strategy")
  80. );
  81.  
  82. //If the value of this property is true, Hibernate writes all SQL
  83. //statements to the console.
  84. jpaProperties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql")
  85. );
  86.  
  87. //If the value of this property is true, Hibernate will format the SQL
  88. //that is written to the console.
  89. jpaProperties.put("hibernate.format_sql",
  90. env.getRequiredProperty("hibernate.format_sql")
  91. );
  92.  
  93. entityManagerFactoryBean.setJpaProperties(jpaProperties);
  94.  
  95. return entityManagerFactoryBean;
  96. }
  97.  
  98. @Bean
  99. JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
  100. JpaTransactionManager transactionManager = new JpaTransactionManager();
  101. transactionManager.setEntityManagerFactory(entityManagerFactory);
  102. return transactionManager;
  103. }
  104. }
  105.  
  106.  
  107. --REPO--
  108.  
  109. package com.sean.books;
  110.  
  111. import org.hibernate.annotations.NamedNativeQueries;
  112. import org.hibernate.annotations.NamedNativeQuery;
  113. import org.springframework.data.domain.Page;
  114. import org.springframework.data.domain.Pageable;
  115. import org.springframework.data.jpa.repository.Query;
  116. import org.springframework.data.repository.CrudRepository;
  117. import org.springframework.data.repository.Repository;
  118. import org.springframework.data.repository.query.Param;
  119.  
  120. import java.util.List;
  121.  
  122.  
  123. /**
  124. * Created by Sean on 12/02/2016.
  125. */
  126. public interface BookRepository extends Repository<Book, Long> {
  127.  
  128. @Query(value = "SELECT * FROM books", nativeQuery = true)
  129. List<Book> findAllBooks();
  130.  
  131. @Query(value = "SELECT * FROM cart", nativeQuery = true)
  132. List<Book> findAllCartItems();
  133.  
  134. Book save(Book book);
  135.  
  136. void delete(Book deleted);
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement