springboot 整合hibernate

2021-01-26 04:14

阅读:526

标签:factor   lease   localhost   red   text   snapshot   wired   set   packages   

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/xsd/maven-4.0.0.xsd">
  modelVersion>4.0.0modelVersion>
  groupId>com.ztsjgroupId>
  artifactId>ztsj-demoartifactId>
  version>0.0.1-SNAPSHOTversion>
  
  parent>
    groupId>org.springframework.bootgroupId>
    artifactId>spring-boot-starter-parentartifactId>
    version>1.5.16.RELEASEversion>
  parent>
  
   dependencyManagement>
        dependencies>
            dependency>
                groupId>org.springframework.cloudgroupId>
                artifactId>spring-cloud-dependenciesartifactId>
                version>Edgware.SR4version>
                type>pomtype>
                scope>importscope>
            dependency>
        dependencies>
  dependencyManagement>
  dependencies>
      dependency>
      groupId>org.springframework.bootgroupId>
      artifactId>spring-boot-starter-webartifactId>
    dependency>
 
      
      dependency>
          groupId>org.springframework.bootgroupId>
          artifactId>spring-boot-starter-data-jpaartifactId>
      dependency>
      
          
    dependency>
        groupId>mysqlgroupId>
        artifactId>mysql-connector-javaartifactId>
    dependency>
  dependencies>
  
project>

application.xml

#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/ztsj?useunicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database=mysql
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
# stripped before adding them to the entity manager
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

 

配置类

package com.ims;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

@Configuration
public class HibernateConfig {
     
    @Value("${spring.jpa.properties.hibernate.current_session_context_class}")
    public String current_session_context_class;
    @Autowired
    private DataSource dataSource;
    
    @Bean
    public LocalSessionFactoryBean sessionFactoryBean() {
       LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
       sessionFactoryBean.setDataSource(dataSource);
       sessionFactoryBean.setPackagesToScan("com.ims");//dao和entity的公共包
       Properties properties = new Properties();
       properties.setProperty("hibernate.current_session_context_class", current_session_context_class);
       sessionFactoryBean.setHibernateProperties(properties);
       return sessionFactoryBean;
    }
}

 

使用sessionFactory

@Autowired
    private EntityManagerFactory sessionFactory;
  
    public Session getSession() {
     
   ##使用此方式 service层 要使用事务管理
return sessionFactory.unwrap(SessionFactory.class).getCurrentSession();     
  ##使用此方式 service层 不要使用事务管理,但是要手动关闭session
   #return sessionFactory.unwrap(SessionFactory.class).openSession()
}

 

 

springboot 整合hibernate

标签:factor   lease   localhost   red   text   snapshot   wired   set   packages   

原文地址:https://www.cnblogs.com/bignew/p/12853809.html


评论


亲,登录后才可以留言!