MemberDaoImpl.java
3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.pudp.bae.dao.redis;
import java.util.ArrayList;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;
import com.pudp.bae.base.RedisGeneratorDao;
import com.pudp.bae.model.Member;
@Repository(value="memberDao")
public class MemberDaoImpl extends RedisGeneratorDao<String,Member> implements MemberDao{
/**
* 添加对象
*/
@Override
public boolean add(final Member member) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
byte[] key = serializer.serialize(member.getId());
byte[] name = serializer.serialize(member.getNickname());
return connection.setNX(key, name);
}
});
return result;
}
/**
* 添加集合
*/
@Override
public boolean add(final List<Member> list) {
Assert.notEmpty(list);
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
for (Member member : list) {
byte[] key = serializer.serialize(member.getId());
byte[] name = serializer.serialize(member.getNickname());
connection.setNX(key, name);
}
return true;
}
}, false, true);
return result;
}
/**
* 删除对象 ,依赖key
*/
@Override
public void delete(String key) {
List<String> list = new ArrayList<String>();
list.add(key);
delete(list);
}
/**
* 删除集合 ,依赖key集合
*/
public void delete(List<String> keys) {
redisTemplate.delete(keys);
}
/**
* 修改对象
*/
public boolean update(final Member member) {
String key = member.getId();
if (get(key) == null) {
throw new NullPointerException("数据行不存在, key = " + key);
}
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
byte[] key = serializer.serialize(member.getId());
byte[] name = serializer.serialize(member.getNickname());
connection.set(key, name);
return true;
}
});
return result;
}
/**
* 根据key获取对象
*/
@Override
public Member get(final String keyId) {
Member result = redisTemplate.execute(new RedisCallback<Member>() {
public Member doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
byte[] key = serializer.serialize(keyId);
byte[] value = connection.get(key);
if (value == null) {
return null;
}
String nickname = serializer.deserialize(value);
return new Member(keyId, nickname);
}
});
return result;
}
}