|
| 1 | +package com.us.example.config; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import javax.persistence.EntityManagerFactory; |
| 7 | +import javax.sql.DataSource; |
| 8 | + |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.context.annotation.Bean; |
| 11 | +import org.springframework.context.annotation.ComponentScan; |
| 12 | +import org.springframework.context.annotation.Configuration; |
| 13 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 14 | +import org.springframework.orm.jpa.JpaTransactionManager; |
| 15 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 16 | +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
| 17 | +import org.springframework.transaction.PlatformTransactionManager; |
| 18 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 19 | + |
| 20 | +/** |
| 21 | + * Created by yangyibo on 17/1/13. |
| 22 | + */ |
| 23 | + |
| 24 | +@Configuration |
| 25 | +@EnableJpaRepositories("com.us.example.dao") |
| 26 | +@EnableTransactionManagement |
| 27 | +@ComponentScan |
| 28 | +public class JpaConfig { |
| 29 | + @Autowired |
| 30 | + private DataSource dataSource; |
| 31 | + |
| 32 | + @Bean |
| 33 | + public EntityManagerFactory entityManagerFactory() { |
| 34 | + HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); |
| 35 | + //vendorAdapter.setShowSql(true); |
| 36 | + //vendorAdapter.setGenerateDdl(true); |
| 37 | + |
| 38 | + LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); |
| 39 | + factory.setJpaVendorAdapter(vendorAdapter); |
| 40 | + factory.setPackagesToScan("com.us.example.bean"); |
| 41 | + factory.setDataSource(dataSource); |
| 42 | + |
| 43 | + |
| 44 | + Map<String, Object> jpaProperties = new HashMap<>(); |
| 45 | + jpaProperties.put("hibernate.ejb.naming_strategy","org.hibernate.cfg.ImprovedNamingStrategy"); |
| 46 | + jpaProperties.put("hibernate.jdbc.batch_size",50); |
| 47 | + //jpaProperties.put("hibernate.show_sql",true); |
| 48 | + |
| 49 | + factory.setJpaPropertyMap(jpaProperties); |
| 50 | + factory.afterPropertiesSet(); |
| 51 | + return factory.getObject(); |
| 52 | + } |
| 53 | + |
| 54 | + @Bean |
| 55 | + public PlatformTransactionManager transactionManager() { |
| 56 | + |
| 57 | + JpaTransactionManager txManager = new JpaTransactionManager(); |
| 58 | + txManager.setEntityManagerFactory(entityManagerFactory()); |
| 59 | + return txManager; |
| 60 | + } |
| 61 | +} |
0 commit comments