版权声明:A_芦瑞华_i_DO.欢迎转载 https://blog.csdn.net/weixin_43067223/article/details/84501306
整合springBoot和Mabatis
之前有文章已经整合,请翻阅。
整合redis和Mabatis
第一步:下载redis
之前有文章下载过,注意整合使用的时候需要开启redis的服务。安装目录下有个start.bat请打开。
第二步: 添加jar支持
<!--jedis连接-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
</dependency>
<!-- mybatis 整合redis 包 -->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-redis</artifactId>
<version>1.0.0-beta2</version>
</dependency>
- 如果redis服务想被外部链接访问,需要修改redis.conf配置 (69行和88行)
- 注释掉 #bind localhost或者127.0.0.1 69行
- 修改保护:protected-mode no 或者上面bind 相应IP 这里的保护可以默认
第三步:配置application.properties
#redis配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.database=0
spring.redis.timeout=3000
spring.redis.jedis.pool.max-active=200
spring.redis.jedis.pool.max-idle=100
spring.redis.jedis.pool.min-idle=1
spring.redis.jedis.pool.max-wait=1000
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.test-on-return=true
第四步:在mapper.xml文件中添加类的依赖关系
- 如果是注解式Mabatis的sql语句需要使用注解在dao层的接口方法上
@CacheNamespace(implementation = RedisCache.class) - 如果是mapper.xml文件需要加入这个类的依赖
<cache type="org.mybatis.caches.redis.RedisCache" blocking="false"
flushInterval="0" readOnly="true" size="1024" eviction="FIFO"/>
- 例如NewsDaoMapper.xml
代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aaa.sb.dao.NewsDao">
<cache type="org.mybatis.caches.redis.RedisCache" blocking="false"
flushInterval="0" readOnly="true" size="1024" eviction="FIFO"/>
<insert id="add">
insert into tb_news values (seq_newsid.nextval,#{title},#{content},#{type},0,#{picPath},#{fileName})
</insert>
<select id="getList" resultType="map">
select * from tb_news
</select>
<select id="selectNewsMsgById" resultType="map">
select * from tb_news where newsid = #{newsId}
</select>
<update id="updateOne">
update tb_news
<set>
<if test="title != null">
title = #{title},
</if>
<if test="content != null">
content = #{content},
</if>
<if test="typeId != null">
typeid = #{typeId},
</if>
<if test="picPath != null">
picpath = #{picPath},
</if>
<if test="fileName != null">
filename = #{fileName},
</if>
</set>
where newsid = ${newsId}
</update>
<delete id="toDelete">
delete from tb_news where newsid = #{newsId}
</delete>
</mapper>
- 存储过程不可以使用redis进行缓存
代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aaa.sb.dao.EmpDao">
<cache type="org.mybatis.caches.redis.RedisCache" blocking="false"
flushInterval="0" readOnly="true" size="1024" eviction="FIFO"/>
<!--根据部门编号查询(存储过程的调用)-->
<!--statementType="CALLABLE"属性和值,返回值不写-->
<select id="getListByPro" statementType="CALLABLE" useCache="false">
call pkg_emp.pro_query_empbydeptno(#{deptNo,mode=IN,jdbcType=INTEGER},
#{empList,mode=OUT,jdbcType=CURSOR,javaType=java.sql.ResultSet,resultMap=el})
</select>
<resultMap id="el" type="emp">
<id column="empno" property="empNo"></id>
<result column="ename" property="ename"></result>
<result column="job" property="job"></result>
<result column="sal" property="salary"></result>
<result column="hiredate" property="hireDate"></result>
</resultMap>
</mapper>