Skip to content

Commit 54a7bf8

Browse files
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # pom.xml # src/main/java/com/cms/controller/LoginController.java # src/main/resources/applicationContext.xml
2 parents e27b193 + 78d9ef6 commit 54a7bf8

File tree

7 files changed

+305
-1
lines changed

7 files changed

+305
-1
lines changed

src/main/java/com/cms/controller/LoginController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public Map<String, Object> register(String account, String password, String emai
4242
} else {
4343
int userSaveIdentify = userInfoService.insertByUser(user);
4444
if (userSaveIdentify > 0) {
45-
//插入redis
4645
map.put("success", true);
4746
map.put("message", "用户注册成功");
4847
map.put("openid", openid);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.redis.dao;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.data.redis.core.RedisTemplate;
5+
import org.springframework.data.redis.serializer.RedisSerializer;
6+
7+
/**
8+
* Created by lsh on 2016/8/24.
9+
*/
10+
public abstract class AbstractBaseRedisDao<K, V> {
11+
12+
@Autowired
13+
protected RedisTemplate<K, V> redisTemplate;
14+
15+
/**
16+
* 设置redisTemplate
17+
* @param redisTemplate the redisTemplate to set
18+
*/
19+
public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
20+
this.redisTemplate = redisTemplate;
21+
}
22+
23+
/**
24+
* 获取 RedisSerializer
25+
* <br>------------------------------<br>
26+
*/
27+
protected RedisSerializer<String> getRedisSerializer() {
28+
return redisTemplate.getStringSerializer();
29+
}
30+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.redis.dao;
2+
3+
import com.redis.model.User;
4+
5+
import java.util.List;
6+
7+
/**
8+
* Created by lsh on 2016/8/24.
9+
*/
10+
11+
public interface IUserDao {
12+
/**
13+
* 新增
14+
* <br>------------------------------<br>
15+
* @param user
16+
* @return
17+
*/
18+
boolean add(User user);
19+
20+
/**
21+
* 批量新增 使用pipeline方式
22+
* <br>------------------------------<br>
23+
* @param list
24+
* @return
25+
*/
26+
boolean add(List<User> list);
27+
28+
/**
29+
* 删除
30+
* <br>------------------------------<br>
31+
* @param key
32+
*/
33+
void delete(String key);
34+
35+
/**
36+
* 删除多个
37+
* <br>------------------------------<br>
38+
* @param keys
39+
*/
40+
void delete(List<String> keys);
41+
42+
/**
43+
* 修改
44+
* <br>------------------------------<br>
45+
* @param user
46+
* @return
47+
*/
48+
boolean update(User user);
49+
50+
/**
51+
* 通过key获取
52+
* <br>------------------------------<br>
53+
* @param keyId
54+
* @return
55+
*/
56+
User get(String keyId);
57+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package com.redis.dao;
2+
3+
import com.redis.model.User;
4+
import org.springframework.dao.DataAccessException;
5+
import org.springframework.data.redis.connection.RedisConnection;
6+
import org.springframework.data.redis.core.RedisCallback;
7+
import org.springframework.data.redis.serializer.RedisSerializer;
8+
import org.springframework.stereotype.Repository;
9+
import org.springframework.util.Assert;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
/**
15+
* Created by lsh on 2016/8/24.
16+
*/
17+
@Repository(value="userDao")
18+
public class UserDaoImpl extends AbstractBaseRedisDao<String, User> implements IUserDao {
19+
/**
20+
* 新增
21+
*<br>------------------------------<br>
22+
* @param user
23+
* @return
24+
*/
25+
public boolean add(final User user) {
26+
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
27+
public Boolean doInRedis(RedisConnection connection)
28+
throws DataAccessException {
29+
RedisSerializer<String> serializer = getRedisSerializer();
30+
byte[] key = serializer.serialize(user.getOpenid());
31+
byte[] name = serializer.serialize(user.getUsername());
32+
return connection.setNX(key, name);
33+
}
34+
});
35+
return result;
36+
}
37+
38+
/**
39+
* 批量新增 使用pipeline方式
40+
*<br>------------------------------<br>
41+
*@param list
42+
*@return
43+
*/
44+
public boolean add(final List<User> list) {
45+
Assert.notEmpty(list);
46+
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
47+
public Boolean doInRedis(RedisConnection connection)
48+
throws DataAccessException {
49+
RedisSerializer<String> serializer = getRedisSerializer();
50+
for (User user : list) {
51+
byte[] key = serializer.serialize(user.getOpenid());
52+
byte[] name = serializer.serialize(user.getUsername());
53+
connection.setNX(key, name);
54+
}
55+
return true;
56+
}
57+
}, false, true);
58+
return result;
59+
}
60+
61+
/**
62+
* 删除
63+
* <br>------------------------------<br>
64+
* @param key
65+
*/
66+
public void delete(String key) {
67+
List<String> list = new ArrayList<String>();
68+
list.add(key);
69+
delete(list);
70+
}
71+
72+
/**
73+
* 删除多个
74+
* <br>------------------------------<br>
75+
* @param keys
76+
*/
77+
public void delete(List<String> keys) {
78+
redisTemplate.delete(keys);
79+
}
80+
81+
/**
82+
* 修改
83+
* <br>------------------------------<br>
84+
* @param user
85+
* @return
86+
*/
87+
public boolean update(final User user) {
88+
String key = user.getOpenid().toString();
89+
if (get(key) == null) {
90+
throw new NullPointerException("数据行不存在, key = " + key);
91+
}
92+
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
93+
public Boolean doInRedis(RedisConnection connection)
94+
throws DataAccessException {
95+
RedisSerializer<String> serializer = getRedisSerializer();
96+
byte[] key = serializer.serialize(user.getId().toString());
97+
byte[] name = serializer.serialize(user.getUsername());
98+
connection.set(key, name);
99+
return true;
100+
}
101+
});
102+
return result;
103+
}
104+
105+
106+
/**
107+
* 通过key获取
108+
* <br>------------------------------<br>
109+
* @param keyId
110+
* @return
111+
*/
112+
113+
@Override
114+
public User get(String keyId) {
115+
return null;
116+
}
117+
/*public User get(final String openid) {
118+
User result = redisTemplate.execute(new RedisCallback<User>() {
119+
public User doInRedis(RedisConnection connection)
120+
throws DataAccessException {
121+
RedisSerializer<String> serializer = getRedisSerializer();
122+
byte[] key = serializer.serialize(keyId);
123+
byte[] value = connection.get(key);
124+
if (value == null) {
125+
return null;
126+
}
127+
String name = serializer.deserialize(value);
128+
return new User(keyId, name, null);
129+
}
130+
});
131+
return result;
132+
}*/
133+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.redis.model;
2+
3+
import com.cms.model.UserInfo;
4+
5+
import java.io.Serializable;
6+
7+
/**
8+
* Created by lsh on 2016/8/24.
9+
*/
10+
11+
public class User extends UserInfo implements Serializable {
12+
private static final long serialVersionUID = -6011241820070393952L;
13+
private String openid;
14+
public User() {
15+
16+
}
17+
18+
@Override
19+
public String getOpenid() {
20+
return openid;
21+
}
22+
23+
@Override
24+
public void setOpenid(String openid) {
25+
this.openid = openid;
26+
}
27+
28+
public User(String openid) {
29+
this.openid = openid;
30+
}
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.redis.service;
2+
3+
import com.redis.dao.IUserDao;
4+
import com.redis.dao.UserDaoImpl;
5+
import com.redis.model.User;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
8+
import javax.annotation.Resource;
9+
10+
/**
11+
* Created by lsh on 2016/8/24.
12+
*/
13+
public class UserService {
14+
@Resource(name="userDao")
15+
@Autowired
16+
private IUserDao userDao;
17+
public void add(User user){
18+
userDao.add(user);
19+
}
20+
21+
public void delete(String openid){
22+
userDao.delete(openid);
23+
}
24+
25+
public User get(String openid)
26+
{
27+
return userDao.get(openid);
28+
}
29+
30+
}

src/main/resources/applicationContext.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ The MIT License (MIT)
4+
~
5+
~ Copyright (c) 2014 abel533@gmail.com
6+
~
7+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
8+
~ of this software and associated documentation files (the "Software"), to deal
9+
~ in the Software without restriction, including without limitation the rights
10+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
~ copies of the Software, and to permit persons to whom the Software is
12+
~ furnished to do so, subject to the following conditions:
13+
~
14+
~ The above copyright notice and this permission notice shall be included in
15+
~ all copies or substantial portions of the Software.
16+
~
17+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
~ THE SOFTWARE.
24+
-->
25+
226
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
327
xmlns:tx="http://www.springframework.org/schema/tx"
428
xmlns:aop="http://www.springframework.org/schema/aop"

0 commit comments

Comments
 (0)