码上敲享录 > SpringBoot常见问题详解 > springboot获取hibernate的session的几种方法

springboot获取hibernate的session的几种方法

章节目录下一章 2018-01-02已有8862人阅读 评论(0)

springboot获取hibernate的session的几种方法:

第一种:entityManager使用@PersistenceContext注入即可。

Session session = (Session) entityManager.getDelegate();


第二种:sessionFactory要自己配置

Session session2= sessionFactory.openSession();

获取sessionFactory的配置如下:

1.在application.properties中根据自己的数据库连接,修改以下代码:

spring.datasource.url=jdbc:mysql://localhost:3306/labour?useSSL=true&verifyServerCertificate=false&Unicode=true&characterEncoding=UTF8

spring.datasource.username=root

spring.datasource.password=

spring.datasource.dataSourceClassName=com.mysql.cj.jdbc.Driver


2.sessionFactory配置类:

package com.yun.lab.server.config;

import com.alibaba.druid.pool.DruidDataSource;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.bind.RelaxedPropertyResolver;

import org.springframework.context.ApplicationContextException;

import org.springframework.context.EnvironmentAware;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Primary;

import org.springframework.core.env.Environment;

import org.springframework.orm.hibernate5.HibernateTransactionManager;

import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

import org.springframework.orm.jpa.JpaTransactionManager;

import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import org.springframework.transaction.PlatformTransactionManager;

import org.springframework.transaction.annotation.EnableTransactionManagement;

import org.springframework.util.StringUtils;

import javax.persistence.EntityManagerFactory;

import javax.sql.DataSource;

import java.beans.PropertyVetoException;

import java.io.IOException;

import java.sql.SQLException;

import java.util.Arrays;

import java.util.Properties;


@Configuration

@EnableTransactionManagement

public class DatabaseConfiguration implements EnvironmentAware {

   private Environment environment;

   private RelaxedPropertyResolver datasourcePropertyResolver;

   //从application.yml中读取资源

   @Override

   public void setEnvironment(Environment environment) {

       this.environment = environment;

       this.datasourcePropertyResolver = new RelaxedPropertyResolver(environment,"spring.datasource.");

   }


   //datasource

   @Bean(initMethod = "init", destroyMethod = "close")

   public DataSource dataSource() throws SQLException {

       DruidDataSource druidDataSource = new DruidDataSource();

       druidDataSource.setUrl(datasourcePropertyResolver.getProperty("url"));

       druidDataSource.setUsername(datasourcePropertyResolver

               .getProperty("username"));

       druidDataSource.setPassword(datasourcePropertyResolver

               .getProperty("password"));

       druidDataSource.setDriverClassName(datasourcePropertyResolver

               .getProperty("dataSourceClassName"));

       druidDataSource.setInitialSize(1);

       druidDataSource.setMinIdle(1);

       druidDataSource.setMaxActive(20);

       druidDataSource.setMaxWait(60000);

       druidDataSource.setTimeBetweenEvictionRunsMillis(60000);

       druidDataSource.setMinEvictableIdleTimeMillis(300000);

       druidDataSource.setTestWhileIdle(true);

       druidDataSource.setTestOnBorrow(false);

       druidDataSource.setTestOnReturn(false);  

       return druidDataSource;

   }


   //sessionFactory

   @Bean

   public LocalSessionFactoryBean sessionFactory() throws SQLException{

       LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();

       localSessionFactoryBean.setDataSource(this.dataSource());

       Properties properties1 = new Properties();

       properties1.setProperty("hibernate.hbm2ddl.auto","none");

       properties1.setProperty("hibernate.dialect","org.hibernate.dialect.MySQL5InnoDBDialect");

       properties1.setProperty("hibernate.show_sql","true");

       localSessionFactoryBean.setHibernateProperties(properties1);

       localSessionFactoryBean.setPackagesToScan("com.yun.lab.server.dts.domain.*");

       return localSessionFactoryBean;

   }

   @Bean("sessionFactory")

   @Primary

   public SessionFactory sessionFactory(LocalSessionFactoryBean localSessionFactoryBean) throws

           PropertyVetoException, IOException {

       SessionFactory sessionFactory = localSessionFactoryBean.getObject();

       return sessionFactory;

   }

   //txManager事务开启

   @Bean("transactionManager")

   public HibernateTransactionManager txManager() throws SQLException {

       HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();

       hibernateTransactionManager.setSessionFactory(sessionFactory().getObject());

       return hibernateTransactionManager;

   }

}

3.使用sessionFactory方法:

@Resource

private SessionFactory sessionFactory;

如果是Session session3= sessionFactory.getCurrentSession();获取Session,要在使用的类上或者方法上添加事务管理@Transactional


本文地址:http://www.yayihouse.com/yayishuwu/chapter/1071










向大家推荐《Activiti工作流实战教程》:https://xiaozhuanlan.com/activiti
0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交