[英]Springboot : Hibernate Configuration
我添加了application.properties文件,如下所示:
Spring DATASOURCE(DataSourceAutoConfiguration&DataSourceProperties)spring.datasource.url = jdbc:mysql:// localhost:3306 / test?useSSL = false spring.datasource.username = root spring.datasource.password = root
Hibernate属性SQL方言使Hibernate为所选数据库生成更好的SQL spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
Hibernate DDL自动(创建,创建,删除,验证,更新)spring.jpa.hibernate.ddl-auto =创建
spring.jpa.properties.hibernate.current_session_context_class = org.springframework.orm.hibernate4.SpringSessionContext
对于dao层,类:
package com.repository;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Repository;
@Repository
public class DaoClass
{
@Autowired
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory()
{
return sessionFactory;
}
@Bean
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
}
现在在运行springboot应用程序时,我面临以下错误:
***************************申请无法开始
描述:
com.repository.DaoClass中的字段sessionFactory需要找不到类型为'org.hibernate.SessionFactory'的bean。
行动:
考虑在配置中定义类型为“ org.hibernate.SessionFactory”的bean。
POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>springboot</groupId> <artifactId>firstprogram</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <name>firstprogram Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.3.6.Final</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies> <build> <finalName>firstprogram</finalName> </build> </project>
package com.repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Component;
@Component
public class DaoClass
{
@PersistenceContext
private EntityManager entityManger;
public EntityManager getEntityManger()
{
return entityManger;
}
public void setEntityManger(EntityManager entityManger)
{
this.entityManger = entityManger;
}
}
Spring Boot不会自动配置SessionFactory
,而是配置EntityManagerFactory
,您可以直接使用它,也可以通过以下方式从中获取SessionFactory
:
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.