springboot 整合hibernate
Posted BigNew
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot 整合hibernate相关的知识,希望对你有一定的参考价值。
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.0</modelVersion> <groupId>com.ztsj</groupId> <artifactId>ztsj-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.16.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 加了该配置文件 会自动启动数据库 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </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的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot整合Hibernate-Validator校验器
SpringBoot整合Hibernate-Validator校验器