Advertisement
Guest User

Untitled

a guest
Jan 8th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. package com.spring.aop.config;
  2.  
  3. import org.apache.commons.dbcp2.BasicDataSource;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  7. import org.springframework.orm.jpa.JpaTransactionManager;
  8. import org.springframework.orm.jpa.JpaVendorAdapter;
  9. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  10. import org.springframework.orm.jpa.vendor.Database;
  11. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  12. import org.springframework.transaction.PlatformTransactionManager;
  13. import org.springframework.transaction.annotation.EnableTransactionManagement;
  14.  
  15. import javax.persistence.EntityManagerFactory;
  16. import javax.sql.DataSource;
  17. import java.util.Properties;
  18.  
  19. @Configuration
  20. @EnableTransactionManagement
  21. @EnableJpaRepositories(basePackages = "com.spring.aop.dao")
  22. public class PersistenceConfig {
  23.  
  24. @Bean
  25. public LocalContainerEntityManagerFactoryBean entityManagerFactory(JpaVendorAdapter adapter) {
  26. LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
  27.  
  28. emf.setPersistenceUnitName("spring-jpa");
  29. emf.setDataSource(getDataSource());
  30. emf.setJpaProperties(additionalProperties());
  31. emf.setJpaVendorAdapter(adapter);
  32. emf.setPackagesToScan("com.spring.aop.domain");
  33.  
  34. return emf;
  35. }
  36.  
  37. @Bean
  38. public JpaVendorAdapter createAdapter() {
  39. HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
  40. adapter.setDatabase(Database.MYSQL);
  41. adapter.setGenerateDdl(true);
  42. adapter.setShowSql(true);
  43. return adapter;
  44. }
  45.  
  46. @Bean
  47. public DataSource getDataSource() {
  48. BasicDataSource ds = new BasicDataSource();
  49. ds.setUrl("jdbc:mysql://localhost:3306/shop_spring?useSSL=false&serverTimezone=UTC");
  50. ds.setUsername("hbstudent");
  51. ds.setPassword("hbstudent");
  52. ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
  53. ds.setInitialSize(5);
  54. return ds;
  55. }
  56.  
  57. @Bean
  58. public PlatformTransactionManager transactionManager(
  59. EntityManagerFactory emf){
  60. JpaTransactionManager transactionManager = new JpaTransactionManager();
  61. transactionManager.setEntityManagerFactory(emf);
  62.  
  63. return transactionManager;
  64. }
  65.  
  66. Properties additionalProperties() {
  67. Properties properties = new Properties();
  68. properties.setProperty("hibernate.hbm2ddl.auto", "update");
  69. properties.setProperty(
  70. "hibernate.dialect", "org.hibernate.dialect.MySQL8Dialect");
  71. properties.setProperty("hibernate.dialect.storage_engine", "innodb");
  72.  
  73. return properties;
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement