springboot 获取hibernate 的 SessionFactory


注入bean

复制代码
package cn.xiaojf;

import cn.xiaojf.today.data.rdb.repository.RdbCommonRepositoryImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean;

@SpringBootApplication
@EnableJpaRepositories(repositoryBaseClass = RdbCommonRepositoryImpl.class)
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean public HibernateJpaSessionFactoryBean sessionFactory() { return new HibernateJpaSessionFactoryBean(); }
}
复制代码

application.properties 中配置

spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database=mysql
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

直接在代码中注入

@Autowired
private SessionFactory sessionFactory;


http://www.cnblogs.com/xiaojf/p/6613595.html


实践下来  service 层必须加事务注解,否则报错


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM