package com.libo.dao.impl;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import com.libo.dao.NewsDao;
import com.libo.po.News;
import com.libo.util.Util;
@Repository("newsDao")
public class NewsDaoImpl implements NewsDao {
//注入redisTemplate
@Autowired
RedisTemplate<Serializable, Serializable> reidsTemplate;
@Override
public Boolean saveOrUpdate(final News news) {
//hash
//T:返回值类型
return reidsTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection arg0)
throws DataAccessException {
arg0.select(4);
//添加
arg0.hSet(("news:"+news.getId()).getBytes(), "id".getBytes(), news.getId().getBytes());
arg0.hSet(("news:"+news.getId()).getBytes(), "title".getBytes(), news.getTitle().getBytes());
arg0.hSet(("news:"+news.getId()).getBytes(), "author".getBytes(), news.getAuthor().getBytes());
arg0.hSet(("news:"+news.getId()).getBytes(), "content".getBytes(), news.getContent().getBytes());
arg0.hSet(("news:"+news.getId()).getBytes(), "createdate".getBytes(), news.getCreateDate().getBytes());
//list添加ID
arg0.lPush("newid".getBytes(), ("news:"+news.getId()).getBytes());
return true;
}
});
}
@Override
public void del(final String id) {
reidsTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection arg0)
throws DataAccessException {
//删除list
arg0.lRem("newid".getBytes(), 1, id.getBytes());
//hash
arg0.del(id.getBytes());
return true;
}
});
}
@Override
public List<Map<String, String>> query(final long currentPage,final long pageSize) {
//获取ID
return reidsTemplate.execute(new RedisCallback<List<Map<String, String>>>() {
@Override
public List<Map<String, String>> doInRedis(RedisConnection arg0)
throws DataAccessException {
arg0.select(4);
List<Map<String, String>> maps=new ArrayList<Map<String,String>>();
//获取list中的id
//long count=arg0.lLen("newid".getBytes());
//获取当前页的id
System.out.println(currentPage);
System.out.println(pageSize);
List<byte[]> newids=arg0.lRange("newid".getBytes(), (currentPage-1)*pageSize, currentPage*pageSize-1);
for (byte[] bs : newids) {
Map<byte[], byte[]> map=arg0.hGetAll(bs);
maps.add(Util.mapToMap(map));
}
return maps;
}
});
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
只要你的redis已经准备好即可。该项目需要maven依赖,请自行安装maven。然后把redis的链接地址改一下,用tomcat发布项目之后,进入项目:浏览器地址栏输入http://localhost:8080/ProjectRedis/mvc/news/query?current=1即可进入项目后面的current代表了分页【开始进入时没有数据,在首页进行添加即可】
资源推荐
资源详情
资源评论
收起资源包目录
ProjectRedis.zip (45个子文件)
ProjectRedis
pom.xml 2KB
.springBeans 422B
target
test-classes
m2e-jee
web-resources
META-INF
maven
ProjectRedis
ProjectRedis
pom.xml 2KB
pom.properties 246B
MANIFEST.MF 114B
classes
beans.xml 2KB
com
libo
dao
NewsDao.class 348B
impl
NewsDaoImpl$3.class 3KB
NewsDaoImpl.class 2KB
NewsDaoImpl$1.class 2KB
NewsDaoImpl$2.class 2KB
po
News.class 1KB
controller
NewsController.class 2KB
service
NewsService.class 360B
impl
NewsServiceImpl.class 1KB
demo
DemoTest.class 5KB
util
Util.class 1KB
springmvc-beans.xml 836B
.settings
org.eclipse.m2e.core.prefs 90B
com.genuitec.runtime.libraries.xml 108B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 450B
org.eclipse.core.resources.prefs 191B
org.eclipse.wst.common.component 678B
org.eclipse.wst.common.project.facet.core.prefs.xml 161B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 597B
org.eclipse.jdt.core.prefs 430B
src
test
resources
java
main
webapp
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 2KB
spring.tld 17KB
spring-form.tld 74KB
index.jsp 1KB
resources
java
beans.xml 2KB
com
libo
dao
NewsDao.java 279B
impl
NewsDaoImpl.java 3KB
po
News.java 1KB
controller
NewsController.java 903B
service
NewsService.java 287B
impl
NewsServiceImpl.java 786B
demo
DemoTest.java 2KB
util
Util.java 575B
springmvc-beans.xml 836B
.project 2KB
.classpath 1KB
build
classes
共 45 条
- 1
资源评论
- yingyingchenhtb2018-06-07好,了解了
- iamok2017-08-11正好这些东西我都需要入门,谢谢
- gulixiong2018-03-25有一定的参考价值
yy251066394
- 粉丝: 17
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功