用eclipselink来取代hibernate

         hibernate是我们常见的实现jpa的全orm框架,可是eclipselink才是完全符合j2ee规范的标准jpa实现。也是官方推荐的jpa实现。下面就来写一个替代hibernate,整合springboot的例子。

         先引入pom依赖,在jpa中排除掉hibernate的依赖,引入eclipselink的依赖。

         


            org.springframework.boot
            spring-boot-starter-data-jpa
            
            
                
                    org.hibernate
                    hibernate-core
                
                
                    org.hibernate
                    hibernate-entitymanager
                
            
        
        
            org.eclipse.persistence
            eclipselink
            2.5.0-RC1
            
                
                    org.eclipse.persistence
                    commonj.sdo
                
            
        

   然后要添加一个配置类JpaDataSourceConfiguration

  

@Configuration
@EntityScan(basePackages = "com.example.demo.entity")
@EnableJpaRepositories(basePackages = "com.example.demo.repository")
public class JpaDataSourceConfiguration extends JpaBaseConfiguration {

    protected JpaDataSourceConfiguration(DataSource dataSource, JpaProperties properties, ObjectProvider jtaTransactionManager, ObjectProvider transactionManagerCustomizers) {
        super(dataSource, properties, jtaTransactionManager, transactionManagerCustomizers);
    }


    @Override
    protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
        return new EclipseLinkJpaVendorAdapter();
    }

    @Override
    protected Map getVendorProperties(){
        Map map = new HashMap<>();
        map.put("eclipselink.weaving", "false");
        map.put("eclipselink.ddl-generation", "none");
        return map;
    }
}

  有了这两个之后,其余的就按照jpa的操作进行就行了,构建repository继承JpaRepository即可操作数据库了。    

你可能感兴趣的:(WEB综合)