Springboot mybatisplus redis 如何实现二级缓存
时间: 2024-01-21 09:03:51 浏览: 91
二级缓存的实现需要在Mybatis的配置文件中配置,具体步骤如下:
1.在SpringBoot的配置文件中,配置Mybatis的mapperLocations,typeAliasesPackage等属性。
2.在Mybatis的配置文件中,添加以下配置:
```
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="defaultExecutorType" value="REUSE"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="logImpl" value="LOG4J"/>
<setting name="localCacheScope" value="SESSION"/>
<setting name="jdbcTypeForNull" value="NULL"/>
<setting name="defaultStatementTimeout" value="2500"/>
<setting name="defaultFetchSize" value="1000"/>
<setting name="safeRowBoundsEnabled" value="false"/>
<setting name="callSettersOnNulls" value="false"/>
<setting name="useColumnLabel" value="true"/>
<setting name="returnInstanceForEmptyRow" value="false"/>
<setting name="useGeneratedKeys" value="false"/>
<setting name="autoMappingBehavior" value="PARTIAL"/>
<setting name="autoMappingUnknownColumnBehavior" value="NONE"/>
<setting name="cacheRefNamespace" value=""/>
<setting name="defaultScriptingLanguage" value="XML"/>
<setting name="defaultEnumTypeHandler" value="org.apache.ibatis.type.EnumOrdinalTypeHandler"/>
<setting name="defaultNumericTypeHandler" value="org.apache.ibatis.type.BigDecimalTypeHandler"/>
<setting name="defaultBooleanTypeHandler" value="org.apache.ibatis.type.BooleanTypeHandler"/>
<setting name="defaultTypeHandler" value="org.apache.ibatis.type.ObjectTypeHandler"/>
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
<setting name="defaultSqlProviderType" value="org.apache.ibatis.extensions.ibatisprovider.CustomSqlProvider"/>
<setting name="excludeStatementTypes" value=""/>
<setting name="mapEmptyStringsToNulls" value="false"/>
<setting name="callAnyGetterOnMap" value="false"/>
<setting name="returnInstanceForEmptyCollection" value="false"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useActualParamName" value="true"/>
<setting name="logPrefix" value=""/>
<setting name="configurationFactory" value=""/>
<setting name="vfsImpl" value=""/>
<setting name="usePrefixesInMapper" value="false"/>
<setting name="useGeneratedKeysWithJdbc4GeneratedKeys" value="false"/>
<setting name="sqlSessionFactoryBuilder" value=""/>
<setting name="autoDelimitKeywords" value="false"/>
<setting name="databaseIdProvider" value=""/>
<setting name="defaultSqlProviderAnnotation" value=""/>
<setting name="localCacheHashcode" value="false"/>
<setting name="mailSession" value=""/>
<setting name="useClassNameAsMapKey" value="true"/>
<setting name="globalPropertiesRef" value=""/>
<setting name="sqlResultSetHandlerFactory" value=""/>
<setting name="databaseSchema" value=""/>
<setting name="configurationProperties" value=""/>
</settings>
<typeAliases>
<typeAlias alias="Student" type="com.csdn.entity.Student"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/csdn/mapper/StudentMapper.xml"/>
</mappers>
```
其中,设置cacheEnabled为true,开启缓存;设置localCacheScope为SESSION,开启二级缓存。
3.在mapper的.xml文件中,添加以下配置:
```
<select id="getStudent" resultMap="StudentResultMap" useCache="true" statementType="STATEMENT">
select * from student where id = #{id}
</select>
```
其中,useCache为true,开启缓存。
4.在需要使用二级缓存的实体类中,添加以下注解:
```
@CacheNamespace(eviction = FifoCache.class, flushInterval = 60000, size = 512, readWrite = true)
```
其中,eviction表示策略,flushInterval表示刷新时间,size表示缓存最大值,readWrite表示是否支持读写。
以上就是SpringBoot+MybatisPlus+Redis实现二级缓存的步骤。希望能对你有所帮助。
阅读全文