当前位置: 首页 > 知识库问答 >
问题:

在SpringBoot+Spring data JPA+MySql项目中配置SessionFactory

侯善
2023-03-14

我一直在用SpringBoot+Spring Data JPA编写我的第一个项目。我使用MySql作为我的数据库提供程序。我对春靴和冬眠有点陌生。

我遵循了http://blog.netgloo.com/2014/10/27/using-mysql-in-spring-boot-via-spring-data-jpa-and-hibernate/提供的教程。

@Repository
public class ReadDaoImpl implements ReadDao{

    private static SessionFactory sessionFactory;

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Autowired
    public static void setSessionFactory(SessionFactory sessionFactory) {
        TenderReadDaoImpl.sessionFactory = sessionFactory;
    }

    @Override
    public List<Read> getAllReads() {

        System.out.println("DEBUG: Inside ReadDaoImpl.getAllReads");

        Session session = sessionFactory.openSession(); // I think that the issue comes from here
        Transaction tx = null;
        ..............................
        ..............................
    }
}
Session session = sessionFactory.openSession();
# ===============================
# = DATA SOURCE
# ===============================

# Set here configurations for the database connection

# Connection url for the database 
spring.datasource.url = jdbc:mysql://localhost:3306/readsdb?useSSL=false

# Username and password
spring.datasource.username = root
spring.datasource.password = password

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# ===============================
# = JPA / HIBERNATE
# ===============================

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager).

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto = create-drop

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

在我的项目中,没有任何配置文件。我需要添加一个来创建SessionFactorybean吗?我遇到了一个名为EntityManagerFactory的东西。对于这个基于Spring Jpa的项目,我需要通过它来配置SessionFactory吗?如果是,怎么做?

我现在完全没有解决办法。引导我完成这件事。如果能指出做这件事的详尽方法,对我会有很大帮助。

谢谢…!

共有1个答案

宗政元青
2023-03-14

以这种方式在@Configuration类中显式配置SessionFactory。从属性文件中删除配置。我使用Oracle,您可以相应地替换mysql属性:

@Configuration
public class myConfiguration {
 //datasource bean
    @Bean("dataSource")
    public DataSource  getDataSource() {        
        DataSourceBuilder b = DataSourceBuilder.create();
         b.url("jdbc:oracle:thin:@localhost:1521:xe");
         b.driverClassName("oracle.jdbc.driver.OracleDriver");
         b.username("system");
         b.password("system");
        return b.build();
    }


    //sessionfactory bean
    @Bean
    public SessionFactory getSessionFactory(DataSource dataSource) {

        LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);

        builder.addProperties(getHibernateProperties());
        builder.scanPackages("ORM.Model");

        return builder.buildSessionFactory();       
    }

    //Hibernate properties:
    private Properties getHibernateProperties() {       
        Properties properties = new Properties();

        properties.put("hibernate.dialect", "org.hibernate.dialect.Oracle9Dialect");        
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.format_sql", "true");     
        properties.put("hibernate.hbm2ddl.auto", "create");

        return properties;
    }

    //transaction manager bean
    @Bean
    public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
        return transactionManager;
    }
 类似资料:
  • 我只想不使用xml,所以我需要非xml替代这些设置。这是我的POM。

  • 我知道在DispatcherServlet之外使用请求范围bean需要一些配置,并且已经阅读了http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes-oth,但是还没有成功: 对于Servlet3.0+,这可以通过WebApplicationIni

  • 项目配置 项目配置 配置简介 CAT上项目名称:项目接入CAT后的唯一标识,来识别自己 事业部:项目所属事业部 产品线:项目所属产品线 负责人:项目负责人,该项不做为告警联系人 项目组邮件:项目负责人邮件,或项目组产品线邮件,多个邮箱由英文逗号分割,不要留有空格;作为发送告警邮件、微信的依据 项目组号码:项目负责人手机号;多个号码由英文逗号分隔,不要留有空格;作为发送告警短信的依据 机器分组配置

  • 进入阿里云服务器,搜索ssl证书,免费申请,1.证书申请(自行选购) 2.下载证书 3.解压下载压缩包 4.将.pfx文件放至springboot项目resources目录下    applicatio.yml中添加配置   5.设置http自动重定向https(8082端口->443端口): 在SpringApplication启动类中加入以下代码:(注意网上有些代码中的EmbeddedServletContainerFactory找不到

  • 我有一个Gradle多项目构建,看起来像这样: 我希望将目录< code>shared中的所有项目作为依赖项添加到< code>plugins中的所有项目。 更一般地说:我如何按目录配置子项目? settings.gradle的内容: build.gradle的内容:

  • 配置子项目 到目前为止你已经把ToDo项目根据功能拆分成多个模块,接下来可以用之前的方法来定义构建逻辑,下面有几点需要主要: 根目录和子目录使用相同的group和version属性值 所有的子目录都是Java项目需要Java插件来正常工作,所以你只需要在子项目中应用Java插件 web子项目是唯一一个依赖外部库的项目,它需要打包成WAR而不是JAR 子项目之间可以定义模块依赖 接下来你将学习如何定