首先删除所有配置,Spring Boot会为你启动它。如果你确实需要a
SessionFactory而不是
EntityManagerFactoryadd HibernateJpaSessionFactoryBean。
确保你
application.properties的类路径中有一个,然后添加以下属性。
spring.datasource.driverClassName=org.postgresql.Driverspring.datasource.url=jdbc:postgresql://localhost:5432/teste?charSet=LATIN1spring.datasource.username=klebermospring.datasource.password=123spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialectspring.jpa.show-sql=falsespring.jpa.hibernate.ddl-auto=create
如果你确实需要访问,
SessionFactory并且基本上是针对同一数据源的,则可以执行以下操作(尽管对于XML而不是JavaConfig,也可以在此处进行说明)。
@Configuration public class HibernateConfig { @Bean public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) { HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean(); factory.setEntityManagerFactory(emf); return factory; }}这样,你就可以同时拥有
EntityManagerFactory和
SessionFactory。
假设你有一个带有main方法的类,
@EnableAutoConfiguration则不需要
@EnableTransactionManagement注释,因为
Spring Boot将为你启用该注释。
com.spring.app包中的基本应用程序类就足够了。
@Configuration@EnableAutoConfiguration@ComponentScanpublic class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); }} 这样的事情应该足以检测到你的所有类(包括实体和基于Spring Data的存储库)。
我还建议删除
commons-dbcp依赖关系,因为这将允许Spring Boot配置更快,更可靠的HikariCP实现。



