Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.spring.aop.config;
- import org.apache.commons.dbcp2.BasicDataSource;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
- import org.springframework.orm.jpa.JpaTransactionManager;
- import org.springframework.orm.jpa.JpaVendorAdapter;
- import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
- import org.springframework.orm.jpa.vendor.Database;
- import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
- import org.springframework.transaction.PlatformTransactionManager;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- import javax.persistence.EntityManagerFactory;
- import javax.sql.DataSource;
- import java.util.Properties;
- @Configuration
- @EnableTransactionManagement
- @EnableJpaRepositories(basePackages = "com.spring.aop.dao")
- public class PersistenceConfig {
- @Bean
- public LocalContainerEntityManagerFactoryBean entityManagerFactory(JpaVendorAdapter adapter) {
- LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
- emf.setPersistenceUnitName("spring-jpa");
- emf.setDataSource(getDataSource());
- emf.setJpaProperties(additionalProperties());
- emf.setJpaVendorAdapter(adapter);
- emf.setPackagesToScan("com.spring.aop.domain");
- return emf;
- }
- @Bean
- public JpaVendorAdapter createAdapter() {
- HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
- adapter.setDatabase(Database.MYSQL);
- adapter.setGenerateDdl(true);
- adapter.setShowSql(true);
- return adapter;
- }
- @Bean
- public DataSource getDataSource() {
- BasicDataSource ds = new BasicDataSource();
- ds.setUrl("jdbc:mysql://localhost:3306/shop_spring?useSSL=false&serverTimezone=UTC");
- ds.setUsername("hbstudent");
- ds.setPassword("hbstudent");
- ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
- ds.setInitialSize(5);
- return ds;
- }
- @Bean
- public PlatformTransactionManager transactionManager(
- EntityManagerFactory emf){
- JpaTransactionManager transactionManager = new JpaTransactionManager();
- transactionManager.setEntityManagerFactory(emf);
- return transactionManager;
- }
- Properties additionalProperties() {
- Properties properties = new Properties();
- properties.setProperty("hibernate.hbm2ddl.auto", "update");
- properties.setProperty(
- "hibernate.dialect", "org.hibernate.dialect.MySQL8Dialect");
- properties.setProperty("hibernate.dialect.storage_engine", "innodb");
- return properties;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement