Springboot:休眠配置

时间:2018-07-08 09:40:50

标签: java spring hibernate spring-boot

我已经添加了application.properties文件,如下所示:

  

Spring DATASOURCE(数据源自动配置和数据源属性)spring.datasource.url =   jdbc:mysql:// localhost:3306 / test?useSSL = false   spring.datasource.username =根spring.datasource.password =根

     

休眠属性    SQL方言使Hibernate为选定的数据库生成更好的SQL spring.jpa.properties.hibernate.dialect =   org.hibernate.dialect.MySQL5InnoDBDialect

     

休眠ddl auto(创建,创建,删除,验证,更新)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需要一个Bean   键入找不到的“ org.hibernate.SessionFactory”。

     

操作:

     

考虑在其中定义类型为“ 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>

我也添加了 PEP 484

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;
    }
}

1 个答案:

答案 0 :(得分:0)

Spring boot不会自动配置SessionFactory,而是配置EntityManagerFactory,您可以直接使用它,也可以通过以下方式从中获取SessionFactory

SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
相关问题