配置 jdbcConfig.properites

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/jpaTest
  3. jdbc.username=root
  4. jdbc.password=123456

编写配置类

  1. @Configuration
  2. @ComponentScan(value = "com.example")
  3. @PropertySource(value = "classpath:jdbcConfig.properties")
  4. @EnableTransactionManagement
  5. @EnableJpaRepositories(basePackages = "com.example.dao", transactionManagerRef = "transactionManager",
  6. entityManagerFactoryRef = "entityManagerFactory")
  7. public class SpringConfiguration {
  8. @Value("${jdbc.driver}")
  9. private String driver;
  10. @Value("${jdbc.url}")
  11. private String url;
  12. @Value("${jdbc.username}")
  13. private String username;
  14. @Value("${jdbc.password}")
  15. private String password;
  16. @Bean
  17. public DataSource dataSource(){
  18. ComboPooledDataSource dataSource = new ComboPooledDataSource();
  19. try{
  20. dataSource.setDriverClass(driver);
  21. dataSource.setJdbcUrl(url);
  22. dataSource.setUser(username);
  23. dataSource.setPassword(password);
  24. return dataSource;
  25. } catch (PropertyVetoException e) {
  26. throw new RuntimeException(e.getMessage());
  27. }
  28. }
  29. @Bean
  30. public PersistenceProvider persistenceProvider(){
  31. return new HibernatePersistenceProvider();
  32. }
  33. @Bean
  34. public JpaVendorAdapter jpaVendorAdapter(){
  35. HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
  36. jpaVendorAdapter.setShowSql(true);
  37. jpaVendorAdapter.setGenerateDdl(false);
  38. jpaVendorAdapter.setDatabase(Database.MYSQL);
  39. jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
  40. return jpaVendorAdapter;
  41. }
  42. @Bean
  43. public JpaDialect jpaDialect(){
  44. return new HibernateJpaDialect();
  45. }
  46. @Bean
  47. public LocalContainerEntityManagerFactoryBean entityManagerFactory(
  48. @Autowired DataSource dataSource,
  49. @Autowired PersistenceProvider persistenceProvider,
  50. @Autowired JpaVendorAdapter jpaVendorAdapter,
  51. @Autowired JpaDialect jpaDialect){
  52. LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
  53. factory.setDataSource(dataSource);
  54. factory.setPackagesToScan("com.example.domain");
  55. factory.setPersistenceProvider(persistenceProvider);
  56. factory.setJpaVendorAdapter(jpaVendorAdapter);
  57. factory.setJpaDialect(jpaDialect);
  58. return factory;
  59. }
  60. @Bean
  61. public PlatformTransactionManager transactionManager(@Autowired EntityManagerFactory entityManagerFactory){
  62. JpaTransactionManager txManager = new JpaTransactionManager();
  63. txManager.setEntityManagerFactory(entityManagerFactory);
  64. return txManager;
  65. }
  66. }

编写JPA 接口

  1. @Repository
  2. public interface ICustomerDao extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {
  3. }

编写测试类

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(classes = {SpringConfiguration.class}) //指定 Spring 容器的配置信息
  3. public class ICustomerDaoTest {
  4. @Autowired
  5. private ICustomerDao customerDao;
  6. .....
  7. }