IntelliJ IDEA Spring Boot(5) 集成Hibernate

该博客介绍了如何在IntelliJ IDEA中使用Spring Boot集成Hibernate,并进一步整合Ehcache作为缓存解决方案,为后续的mybatis与ehcache集成奠定基础。
摘要由CSDN通过智能技术生成

   上一篇集成了mybatis。这一篇我来集成hibernate,顺道把ehcache缓存也集成进去。下篇我们再讲 mybatis与ehcache集成

    pom依赖增加

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.44</version>
</dependency>
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.0.31</version>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 集成ehcache需要的依赖-->
<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-ehcache</artifactId>
</dependency>

#log
logging.file=mylog.log
logging.level.root=warn
logging.level.org.springframework=info
logging.level.org.hibernate=warn
#logging.level.org.hibernate.engine.QueryParameters=DEBUG
#logging.level.org.hibernate.engine.query.HQLQueryPlan=DEBUG
#logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
#logging.level.org.hibernate.type.descriptor.sql.BasicExtractor=TRACE
#控制是否显示缓存相关日志
#logging.level.org.hibernate.cache=DEBUG
#thymeleaf
spring.thymeleaf.cache=false

#mysql
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url=jdbc\:mysql\://localhost\:3306/dadmin?useUnicode\=true&amp;characterEncoding\=UTF-8
spring.datasource.username=root
spring.datasource.password=123456

#druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.filters=stat
spring.datasource.maxActive= 20
spring.datasource.initialSize= 1
spring.datasource.maxWait= 60000
spring.datasource.minIdle =1
spring.datasource.timeBetweenEvictionRunsMillis= 60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery= select 'x'
spring.datasource.testWhileIdle= true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn= false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxOpenPreparedStatements= 20

#hibernate
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.format_sql = true
spring.jpa.properties.hibernate.show_sql = true
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
# 打开hibernate统计信息
spring.jpa.properties.hibernate.generate_statistics=true
# 打开二级缓存
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
# 打开查询缓存
spring.jpa.properties.hibernate.cache.use_query_cache=true
# 指定缓存provider
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
# 配置shared-cache-mode
spring.jpa.properties.javax.persistence.sharedCache.mode=ENABLE_SELECTIVE
spring.jpa.properties.hibernate.cache.provider_configuration_file_resource_path=classpath:ehcache.xml
spring.cache.ehcache.config=ehcache.xml
配置信息,在新建一个ehcache.xml

<ehcache name="springBoot_Ehcache" updateCheck="false">
    <diskStore path="../springBoot_Ehcache"/>
    <!-- hibernate ehcache-->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
    />
    <cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值