使用Redis存取数据+数据库存取(spring+java)
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)的更多相关文章
- XML Schema格式的"日期型数据”数据库存取
对于XML Schema格式的"日期型数据"在数据库中存于datetime字段的时候,出现错误 mysql> select @@sql_mode; +------------ ...
- Python写的链接数据库存取数据
Python写的链接数据库存取数据 #!/usr/bin/python # -*- coding: UTF-8 -*- from __future__ import print_function im ...
- redis存取数据list
登录redis客户端 一.存取数据 1.左右添加数据 2.读取数据lrange 3.左右固定弹出一个元素 4.列表中个数 5.删除元素值为value的元素 count 值要自己给定: 6.获取和设置指 ...
- redis存取数据Set
一.set集合无序不重复 二.存取数据 1. 2. 3. 4.set集合差集运算 找出并返回前面集合有后面没有的元素: 5.set集合交际运算 6.并集运算 sunion 7.随机弹出一个元素,因为s ...
- spring + redis 实现数据的缓存
1.实现目标 通过redis缓存数据.(目的不是加快查询的速度,而是减少数据库的负担) 2.所需jar包 注意:jdies和commons-pool两个jar的版本是有对应关系的,注意引入jar包是要 ...
- Spring Boot使用redis做数据缓存
1 添加redis支持 在pom.xml中添加 <dependency> <groupId>org.springframework.boot</groupId> & ...
- Spring+Redis集成+关系型数据库持久化
本篇文章主要介绍了"Spring+Redis集成+关系型数据库持久化",主要涉及到Spring+Redis集成+关系型数据库持久化方面的内容,对于Spring+Redis集成+关系 ...
- SpringBoot 结合 Spring Cache 操作 Redis 实现数据缓存
系统环境: Redis 版本:5.0.7 SpringBoot 版本:2.2.2.RELEASE 参考地址: Redus 官方网址:https://redis.io/ 博文示例项目 Github 地址 ...
- sqlite3的图片的(二进制数据)存取操作
sqlite3的图片的(二进制数据)存取操作 前言 上篇介绍了sqlite3的一些常用插入操作方法和注意事项,在实际项目中遇到了图片缓存的问题,由于服务器不是很稳定,且受到外界环境的干扰(例如断电 ...
随机推荐
- springboot伪静态
在日常网站访问中,会把动态地址改造成伪静态地址. 例如: 访问新闻栏目 /col/1/,这是原有地址,如果这样访问,不利于搜索引擎检索收录,同时安全性也不是很好. 改造之后: /col/1.html. ...
- javascript实现文本框标签验证
< !DOCTYPE html > <html lang = "en" > <head > <meta charset = "U ...
- css text-shadow
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 前端之body标签中相关标签(一)
一 字体标签 字体标签包含:h1~h6.<font>.<u>.<b>.<strong><em>.<sup>.<sub& ...
- webapi 开启gzip压缩
1.nuget安装Microsoft.AspNet.WebApi.Extensions.Compression.Server 2.global.asax.cs里引用System.Net.Http.Ex ...
- SQLServer中取当前年,月,日,时,分,秒
Select GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName(month,GetDate()) as '月',DateName ...
- 第4课 简易浏览器-WebViewer组件的使用方法
做一个手机浏览器,需要哪些组件呢? 一.组件设计 二.组件属性及命名修改 三.逻辑设计 1.导航按钮代码:前进.后退.主页 2.访问网页按钮 1)根据用户在地址栏输入的地址书写,判断书写中是否含有“h ...
- IIS6.0配置正常,但是显示“网页无法访问”,Httperr.log中显示全是“Connections_refused”,问题总结
转自:http://blog.csdn.net/foxeatapple/article/details/21983869 最近部门的Web服务器突然无法访问! 加班解决! 问题症状: 1.“Inter ...
- java.langThrowable:STACKTRACE
Jboss版本是4.2.0.GA代码运行完后总报错 但是程序的运行结果没问题 请问下这是什么原因2009-12-11 01:53:26,611 INFO [org.jboss.resource.co ...
- Web API 2 入门——使用ASP.NET Web API和Angular.js构建单页应用程序(SPA)(谷歌翻译)
在这篇文章中 概观 演习 概要 由网络营 下载网络营训练包 在传统的Web应用程序中,客户机(浏览器)通过请求页面启动与服务器的通信.然后,服务器处理请求,并将页面的HTML发送给客户端.在与页面的后 ...