redis的操作json对象实例1.redis存储是:key value格式的数据,其中key都是字符串,value有5种不同的数据结构
  value的数据结构:
    1.字符串类型:string
    2.哈希类型 hash :map格式
    3.列表类型 list:linkedlist格式。⽀持重复元素
    4.集合类型 set:不允许重复元素
    5.有序集合类型 sortset:不允许重复元素,且元素有顺序
2.字符串类型 string
    1.存储:set key value
    2.获取:get key
    3.删除:del key
3.哈希类型 hash:
    1.存储:hset key field value
    2.获取:hget key field
    3.删除:hdel key field
jeids的操作
string数据结构操作
//1.获取连接
Jedis jedis = new Jedis("localhost", 6379);
//2.操
//存储
jedis.set("username","zhangsan");
/
/获取
String username = ("username");
System.out.println(username);
//可以使⽤setx⽅法存储指定国旗时间的key value
jedis.setex("active",20,"hehe");//将active hehe键值对存⼊redis,并且20秒后⾃动删除键值对
//3.关闭连接
jedis.close();
hash数据结构操作
//1.获取连接
Jedis jedis = new Jedis("localhost", 6379);
//2.操作
/
/存储
jedis.hset("user","name","lisi");
jedis.hset("user","age","23");
jedis.hset("user","gender","male");
//获取
String name = jedis.hget("user", "name");
System.out.println(name);
//获取hash的所有的map的数据
Map<String, String> user = jedis.hgetAll("user");
Set<String> set = user.keySet();
for (String key : set) {
String value = (key);
System.out.println(key +":"+value);
}
//3.关闭连接
jedis.close();
list数据结构操作
//1.获取连接
Jedis jedis = new Jedis("localhost", 6379);
//2.操作
//存储
jedis.lpush("mylist","a","b","c");//从左边存 cba
jedis.rpush("mylist","a","b","c");//从右边存 abc
//获取
List<String> mylist = jedis.lrange("mylist", 0, -1); System.out.println(mylist);
//list弹出
String element = jedis.lpop("mylist");
System.out.println(element);
String element1 = jedis.rpop("mylist");
System.out.println(element1);
List<String> mylist2 = jedis.lrange("mylist", 0, -1); System.out.println(mylist2);
//3.关闭连接
jedis.close();
set和sortedset数据结构操作
//1.获取连接
Jedis jedis = new Jedis("localhost", 6379);
//2.操作
//存储数字的作⽤是输出按从⼩到⼤排列
jedis.zadd("mysorted",3,"亚瑟");
jedis.zadd("mysorted",30,"后羿");
jedis.zadd("mysorted",25,"孙悟空");
//获取
Set<String> mysorted = ange("mysorted", 0, -1); System.out.println(mysorted);
//3.关闭连接
jedis.close();
jedis连接池:
使⽤:1.创建jedispool连接池对象
  2.调⽤⽅法getResource()⽅法获取jedis连接//创建配置对象
JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(50);//最⼤活动对象
config.setMaxIdle(10);//最⼤保持的连接数
//1.创建对象
JedisPool jedisPool = new JedisPool();
//2.获取连接
Jedis jedis = Resource();
//3.使⽤
jedis.set("heheh","hahah");
//3.关闭连接
jedis.close();
案例:
1.提供⼀个页⾯,是⼀个下拉框,其中是省份
2.页⾯加载完成,发送ajax请求,加载省份
优化:使⽤redis缓存⼀些不经常发⽣变化的数据Dao部分
dao接⼝类
package cn.itcast.dao;
import cn.itcast.domain.Province;
import java.util.List;
public interface ProvinceDao {
public List<Province> findAll();
}
dao实现类
package cn.itcast.dao.impl;
import cn.itcast.dao.ProvinceDao;
import cn.itcast.domain.Province;
import cn.itcast.util.JDBCUtils;
import org.BeanPropertyRowMapper;
import org.JdbcTemplate;
import java.util.List;
public class ProvinceDaoImpl implements ProvinceDao {
//1.声明成员变量
private JdbcTemplate template = new DataSource());
@Override
public List<Province> findAll() {
//2.定义sql
String sql = "select * from province";
//3.执⾏sql
List<Province> list = template.query(sql, new BeanPropertyRowMapper<Province>(Province.class));        return list;
}
}
Service部分
service接⼝类
package cn.itcast.service;
import cn.itcast.domain.Province;
import java.util.List;
public interface ProvinceService {
public List<Province> findAll();
public String findAllJson();
}
service实现类
package cn.itcast.service.impl;
import cn.itcast.dao.ProvinceDao;
import cn.itcast.dao.impl.ProvinceDaoImpl;
import cn.itcast.domain.Province;
import cn.itcast.service.ProvinceService;
import com.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
CORBA.PRIVATE_MEMBER;
import redis.clients.jedis.Jedis;
import java.util.List;
public class ProvinceServiceImpl implements ProvinceService {
//声明dao
private ProvinceDao dao = new ProvinceDaoImpl();
@Override
public List<Province> findAll() {
return dao.findAll();
}
@Override
public String findAllJson() {
//先从redis查询数据
//1.获取连接
Jedis jedis = new Jedis("localhost", 6379);
//2.操作
/
/获取
String province_json = ("province");
//判断province_json是否是null
if (province_json == null || province_json.length() == 0){
//redis没数据,从数据库查询
System.out.println("redis没有数据,查询数据库。。。。");
//数据查询
List<Province> ps = dao.findAll();
//将list序列化为json
ObjectMapper mapper = new ObjectMapper();
try {
province_json = mapper.writeValueAsString(ps);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
//将json数据存⼊数据库中
jedis.set("province",province_json);
//归还连接
jedis.close();
}else{
System.out.println("redis有数据,查询缓存。。。。");
}
return province_json;
}
}
Servlet部分
package cn.itcast.web.servlet;
import cn.itcast.domain.Province;
import cn.itcast.service.ProvinceService;
import cn.itcast.service.impl.ProvinceServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/provinceServlet")
public class ProvinceServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //      //调⽤service
//      ProvinceService service = new ProvinceServiceImpl();
//      List<Province> list = service.findAll();
//      //序列化list为json
//      ObjectMapper mapper = new ObjectMapper();
//      String json = mapper.writeValueAsString(list);
//调⽤service查询
ProvinceServiceImpl service = new ProvinceServiceImpl();
String json = service.findAllJson();
System.out.println(json);
//响应结果
response.setContentType("application/json;charset=gbk");
}
}
Html部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery-3.3.1.js"></script>
<script>
$(function () {
/
/发送ajax请求
$.get("provinceServlet",{},function (data) {
//获取select
var province = $("#province");
//遍历json
$(data).each(function () {
//创建option标签,append追加option
var option = "<option name='"+this.id+"'>"+this.name+"</option>";                    province.append(option)
})
})
})
</script>
</head>
<body>
<select name="" id="province">
jdbctemplate查询一条数据<option value="">--请选择省份--</option>
</select>
</body>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。