RoleMapper接口:

package com.wbg.springRedis.dao;

import com.wbg.springRedis.entity.Role;
import org.springframework.stereotype.Repository; import java.util.List;
@Repository
public interface RoleMapper {
List<Role> listAll();
int add(Role role);
}

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.wbg.springRedis.dao.RoleMapper">
<insert id="add" parameterType="com.wbg.springRedis.entity.Role">
insert into role(role_name,note) values(#{roleName},#{note})
</insert>
<select id="listAll" resultType="com.wbg.springRedis.entity.Role">
select * from role
</select>
</mapper>

Role类:

package com.wbg.springRedis.entity;

import java.io.Serializable;

public class Role  {
private int id;
private String roleName;
private String note;
@Override
public String toString() {
return "Role{" +
"id=" + id +
", roleName='" + roleName + '\'' +
", note='" + note + '\'' +
'}';
} public Role() {
} public Role(String roleName, String note) {
this.roleName = roleName;
this.note = note;
} public Role(int id, String roleName, String note) {
this.id = id;
this.roleName = roleName;
this.note = note;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getRoleName() {
return roleName;
} public void setRoleName(String roleName) {
this.roleName = roleName;
} public String getNote() {
return note;
} public void setNote(String note) {
this.note = note;
}
}

xml文件:

   //纯java
  @Bean
public JedisConnectionFactory jedisConnectionFactory(){
JedisConnectionFactory jcf = new JedisConnectionFactory();
jcf.setHostName("localhost");
return jcf;
}
@Bean
public RedisTemplate redisTemplate(){
RedisTemplate rt = new RedisTemplate();
rt.setConnectionFactory(jedisConnectionFactory());
rt.setKeySerializer(new StringRedisSerializer());
rt.setValueSerializer(new StringRedisSerializer());
return rt;
}
    @Autowired
private RedisTemplate redisTemplate;
@Autowired
private RoleMapper roleMapper;
 public List<Role> listRole() throws IOException {
//使用jackJson序列化
ObjectMapper om = new ObjectMapper();
//键
String key = "listRole";
//原生
//Jedis jedis = new Jedis();
/* if (jedis.exists(key)){
return om.readValue(jedis.get(key), new TypeReference<List<Role>>(){});
}*/
//如果存在 就返回
if (redisTemplate.hasKey(key)){
return om.readValue((String) redisTemplate.opsForValue().get(key), new TypeReference<List<Role>>() {
});
}
List<Role> list = roleMapper.listAll();
//原生设置值
//jedis.set(key, om.writeValueAsString(list));
//设置
redisTemplate.opsForValue().set(key, om.writeValueAsString(list)); return list;
}

使用Redis存取数据+数据库存取(spring+java)的更多相关文章

  1. XML Schema格式的"日期型数据”数据库存取

    对于XML Schema格式的"日期型数据"在数据库中存于datetime字段的时候,出现错误 mysql> select @@sql_mode; +------------ ...

  2. Python写的链接数据库存取数据

    Python写的链接数据库存取数据 #!/usr/bin/python # -*- coding: UTF-8 -*- from __future__ import print_function im ...

  3. redis存取数据list

    登录redis客户端 一.存取数据 1.左右添加数据 2.读取数据lrange 3.左右固定弹出一个元素 4.列表中个数 5.删除元素值为value的元素 count 值要自己给定: 6.获取和设置指 ...

  4. redis存取数据Set

    一.set集合无序不重复 二.存取数据 1. 2. 3. 4.set集合差集运算 找出并返回前面集合有后面没有的元素: 5.set集合交际运算 6.并集运算 sunion 7.随机弹出一个元素,因为s ...

  5. spring + redis 实现数据的缓存

    1.实现目标 通过redis缓存数据.(目的不是加快查询的速度,而是减少数据库的负担) 2.所需jar包 注意:jdies和commons-pool两个jar的版本是有对应关系的,注意引入jar包是要 ...

  6. Spring Boot使用redis做数据缓存

    1 添加redis支持 在pom.xml中添加 <dependency> <groupId>org.springframework.boot</groupId> & ...

  7. Spring+Redis集成+关系型数据库持久化

    本篇文章主要介绍了"Spring+Redis集成+关系型数据库持久化",主要涉及到Spring+Redis集成+关系型数据库持久化方面的内容,对于Spring+Redis集成+关系 ...

  8. SpringBoot 结合 Spring Cache 操作 Redis 实现数据缓存

    系统环境: Redis 版本:5.0.7 SpringBoot 版本:2.2.2.RELEASE 参考地址: Redus 官方网址:https://redis.io/ 博文示例项目 Github 地址 ...

  9. sqlite3的图片的(二进制数据)存取操作

    sqlite3的图片的(二进制数据)存取操作   前言 上篇介绍了sqlite3的一些常用插入操作方法和注意事项,在实际项目中遇到了图片缓存的问题,由于服务器不是很稳定,且受到外界环境的干扰(例如断电 ...

随机推荐

  1. docker 安装ElasticSearch的中文分词器IK

    首先确保ElasticSearch镜像已经启动 安装插件 方式一:在线安装 进入容器 docker exec -it elasticsearch /bin/bash 在线下载并安装 ./bin/ela ...

  2. SPDY和HTTP

    SPDY 是什么 ? SPDY 是 Google 开发的基于传输控制协议 (TCP) 的应用层协议.SPDY 协议旨在通过压缩.多路复用和优先级来缩短网页的加载时间和提高安全性.(SPDY 是 Spe ...

  3. spring 代理

    java动态代理实现 1. Java自带的动态代理,反射生成字节码 2. Cglib调用asm生成子类 spring 中代理实现 1. 如果类实现了接口,使用java动态代理 2. 没有实现接口,使用 ...

  4. CentOS使用fdisk扩展磁盘空间

    使用情况: 1. 虚拟机配置150G硬盘,初始化为/dev/sdb1,后因为磁盘空间不够使用,动态扩容至300G,扩容完成后,想要动态扩容/dev/sdb1分区. 2. 磁盘空间300G,之前分区只划 ...

  5. <Android 应用 之路> 百度地图API使用(3)

    前言 上一篇讲解并实践了百度地图基本显示,地图类型,实时交通图,热力图,地图控制和手势控制,今天看下在地图上添加view和覆盖物. 地图Logo不允许遮挡,可通过mBaiduMap.setPaddin ...

  6. margin的auto的理解 top,left[,bottom,right] position

    auto auto 总是试图充满父元素 margin有四个值: All the margin properties can have the following values: auto - the ...

  7. 【PHP系列】框架的抉择

    缘起 在PHP开发中,选择合适的框架有助于加快软件开发,节约宝贵的项目时间,让开发者专注于功能的实现上.框架的问题是需要很多的投入,选择框架时,我们更看重这个框架的未来,存在多年的大型框架必须要有好的 ...

  8. react native项目直接全局定义横竖屏幕

    这里指的是项目本身为横屏项目,直接全局设置,当然还有其他方法,使用react-native-orientation 全局定义如图: iOS android:

  9. linux 软连接和硬链接示意图

    创建软连接 ln -s 1.txt 1-softlink.txt 创建硬链接 ln 1.txt 1-hardlink.txt

  10. Linux->解决用userdel删除不掉用户的问题

    情况: 一般我们移除,都是先把用户从组中删除,再依次把组删掉,但是这里出现了问题: root@ per# userdel -r mysql userdel: user mysql is current ...