我已经使用Thymeleaf和Hibernate用IntelliJ创建了Spring Boot Web应用程序。到目前为止,我可以创建所有的数据库连接,并且工作正常。据我所知,将Sessionfactory作为bean并在执行db操作的所有服务类中将其自动装配是一个好方法。
我有一个SpringMvcConfiguration作为配置文件,如下所示:
package eu.barz.familykurse.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.SessionLocaleResolver; import java.util.Locale; @Configuration public class SpringMvcConfiguration extends WebMvcConfigurerAdapter{ @Bean public LocaleResolver localeResolver(){ SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver(); sessionLocaleResolver.setDefaultLocale(Locale.GERMAN); return sessionLocaleResolver; } @Bean LocaleChangeInterceptor localeChangeInterceptor(){ LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); localeChangeInterceptor.setParamName("lang"); return localeChangeInterceptor; } @Override public void addInterceptors(InterceptorRegistry interceptorRegistry){ interceptorRegistry.addInterceptor(localeChangeInterceptor()); } }
问题:我已经尝试了很多,但是找不到为SessionFactory声明bean的解决方案。
任何提示将非常有帮助。我应该在这里声明一个sessionfactory和数据源,还是必须在application.properties或仅在hibernate.cfg.xml中声明(当前看起来像这样):
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.url">jdbc:mysql://localhost:3306/family_kurse</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">username</property> <property name="connection.password">secret</property> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.current_session_context_class">thread</property> <property name="show_sql">true</property> <mapping class="eu.barz.familykurse.domain.Leader"/> <mapping class="eu.barz.familykurse.domain.Course"/> <!-- DB schema will be updated if needed --> <!-- <property name="hbm2ddl.auto">update</property> --> </session-factory> </hibernate-configuration>
干杯
解:
我需要如下所述添加豆类
我必须添加
org.springframework spring-orm 4.3.10.RELEASE到我的pom.xml
@EnableAutoConfiguration(排除= {HibernateJpaAutoConfiguration.class})
由于您使用的是Spring Boot,因此应该对数据库配置使用不使用XML的配置。spring启动与Hibernate,您需要创建整合LocalSessionFactoryBean,DataSource,HibernateTransactionManager和PersistenceExceptionTranslationPostProcessor豆是这样的:
LocalSessionFactoryBean
DataSource
HibernateTransactionManager
PersistenceExceptionTranslationPostProcessor
@Configuration public class DatabaseConfig { @Bean public LocalSessionFactoryBean sessionFactory() { LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); sessionFactory.setPackagesToScan("com.example.model"); sessionFactory.setHibernateProperties(hibernateProperties()); return sessionFactory; } @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.postgresql.Driver"); dataSource.setUrl("jdbc:postgresql://localhost:5432/testdb"); dataSource.setUsername("root"); dataSource.setPassword("root"); return dataSource; } @Bean @Autowired public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) { HibernateTransactionManager txManager = new HibernateTransactionManager(); txManager.setSessionFactory(sessionFactory); return txManager; } @Bean public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { return new PersistenceExceptionTranslationPostProcessor(); } Properties hibernateProperties() { Properties properties = new Properties(); properties.setProperty("hibernate.ddl-auto", "update"); properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect"); return properties; } }
在上面的数据库配置中,我使用了postgreSQL数据库。
要获得sessionFactory自动装配SessionFactory接口的实例,如下所示:
SessionFactory
@Autowired SessionFactory sessionFactory;