上一篇集成了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&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"