参考:
https://blog.csdn.net/weixin_44341110/article/details/115208375
https://blog.csdn.net/qq_40929531/article/details/103956332
我是sessionFactory 报错
No bean named 'sessionFactory' available
原因是我在springmvc中添加了springboot项目,
但是我的yml中并没有配置关于hibernate的相关配置(主要配置Transaction,不然无法增删改查,下面也会写到配置中)。
解决方案:
主要看jpa这一块:
server: port: 9020 spring: application: name: spring-boot-test datasource: url: jdbc:mysql://192.168.1.250:3306/kintech_camundaSpringBoot?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&useOldAliasMetadataBehavior=true driver-class-name: com.mysql.cj.jdbc.Driver username: abc password: abc jpa: generate-ddl: false hibernate: ddl-auto: none naming: physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl database: mysql show-sql: false properties: hibernate: dialect: org.hibernate.dialect.MySQL5Dialect current_session_context_class: org.springframework.orm.hibernate5.SpringSessionContext session: store-type: none
添加配置文件:
package com.kintech.webCamunda.conf; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.orm.hibernate5.HibernateTransactionManager; import org.springframework.orm.hibernate5.LocalSessionFactoryBean; import java.util.Properties; @Configuration public class HibernateConfig { @Autowired private Environment environment; /* * 配置SessionFactory会话工厂注入到spring容器 * */ @Bean LocalSessionFactoryBean sessionFactory(){ LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean(); localSessionFactoryBean.setDataSource(getDataSource()); localSessionFactoryBean.setPackagesToScan("com.kintech"); localSessionFactoryBean.setHibernateProperties(hibernateProperties()); return localSessionFactoryBean; } /* * 配置数据源 * */ @Bean DriverManagerDataSource getDataSource(){ DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(environment.getProperty("spring.datasource.driver-class-name")); dataSource.setUsername(environment.getProperty("spring.datasource.username")); dataSource.setPassword(environment.getProperty("spring.datasource.password")); dataSource.setUrl(environment.getProperty("spring.datasource.url")); return dataSource; } /* *设置hibernate配置属性 * */ private Properties hibernateProperties(){ Properties properties = new Properties(); properties.setProperty("hibernate.naming-strategy",environment.getProperty("spring.jpa.hibernate.naming.physical-strategy")); properties.setProperty("hibernate.dialect",environment.getProperty("spring.jpa.properties.hibernate.dialect")); properties.setProperty("hibernate.current_session_context_class",environment.getProperty("spring.jpa.properties.hibernate.current_session_context_class")); properties.setProperty("hibernate.show_sql",environment.getProperty("spring.jpa.show-sql")); properties.setProperty("hibernate.hbm2ddl.auto",environment.getProperty("spring.jpa.hibernate.ddl-auto")); return properties; } @Bean("HibernateTransactionManager") public HibernateTransactionManager getHibernateTransactionManager(@Qualifier("getDataSource") DriverManagerDataSource dataSource, @Qualifier("sessionFactory") LocalSessionFactoryBean sessionFactory) { HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager(); /*localSessionFactoryBean.setMappingResources("");*///配置hibernate的xml实体映射文件 hibernateTransactionManager.setDataSource(dataSource); //下面注意:你都让spring容器去管理类了,当然不能自己new了 //要用spring的getBean方法去创建实例 这样spring才能帮你注入 hibernateTransactionManager.setSessionFactory(sessionFactory.getObject());//这里要注意,sessionFactory必须是Spring注入的不然获取到的值为null return hibernateTransactionManager; } }
标签:available,named,hibernate,spring,xxx,springframework,environment,org,properties From: https://www.cnblogs.com/hanjun0612/p/16889925.html