-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e281783
commit b29c13a
Showing
11 changed files
with
367 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/com/training/core/controller/BaseRedisController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.training.core.controller; | ||
|
||
import io.swagger.annotations.ApiImplicitParam; | ||
import io.swagger.annotations.ApiOperation; | ||
|
||
import javax.annotation.Resource; | ||
|
||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import com.training.core.dao.impl.RedisBaseDaoImpl; | ||
import com.training.core.dto.ResultDataDto; | ||
import com.training.core.entity.BaseEntity; | ||
import com.training.core.util.GenericeClassUtils; | ||
|
||
public class BaseRedisController<T extends BaseEntity> extends BaseController<T> { | ||
|
||
@SuppressWarnings("unchecked") | ||
protected Class<T> entityClass = (Class<T>) GenericeClassUtils.getSuperClassGenricType(this.getClass(), 0); | ||
|
||
@Resource(name = "redisBaseDao") | ||
protected RedisBaseDaoImpl<T> baseDao; | ||
|
||
/** | ||
* 根据Id查询实体 | ||
*/ | ||
@ApiOperation(value="根据Id查询实体", notes="getEntityById") | ||
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer") | ||
@RequestMapping(value = "/getEntityById/{id}", method = RequestMethod.GET) | ||
public @ResponseBody ResultDataDto getEntityById(@PathVariable(value = "id") final Integer id) { | ||
T entity = baseDao.getEntityById(entityClass, id); | ||
return new ResultDataDto(entity); | ||
} | ||
|
||
/** | ||
* 新增实体 | ||
*/ | ||
@ApiOperation(value="新增实体", notes="addEntity") | ||
@ApiImplicitParam(name = "entity", value = "实体Json", required = true, dataType = "application/json") | ||
@RequestMapping(value = "/addEntity", method = RequestMethod.POST, consumes = "application/json") | ||
public @ResponseBody ResultDataDto addEntity(@RequestBody final T entity) { | ||
baseDao.addEntity(entity); | ||
return ResultDataDto.addAddSuccess(); | ||
} | ||
|
||
|
||
} |
107 changes: 107 additions & 0 deletions
107
src/main/java/com/training/core/dao/impl/RedisBaseDaoImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.training.core.dao.impl; | ||
|
||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
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 com.google.gson.Gson; | ||
import com.training.core.dao.BaseDao; | ||
import com.training.core.dto.FlexiPageDto; | ||
import com.training.core.entity.BaseEntity; | ||
|
||
import org.springframework.stereotype.Repository; | ||
|
||
import tk.mybatis.mapper.entity.Example; | ||
|
||
@Repository("redisBaseDao") | ||
public class RedisBaseDaoImpl<T extends BaseEntity> implements BaseDao<T> { | ||
|
||
@Autowired | ||
protected RedisTemplate<Serializable, Serializable> redisTemplate; | ||
|
||
@Override | ||
public T getEntityById(final Class<T> cls, final Integer id) { | ||
return redisTemplate.execute(new RedisCallback<T>() { | ||
@Override | ||
public T doInRedis(RedisConnection connection) throws DataAccessException { | ||
byte[] key = redisTemplate.getStringSerializer().serialize(cls.getName() + "_" + id); | ||
if (connection.exists(key)) { | ||
byte[] value = connection.get(key); | ||
String json = redisTemplate.getStringSerializer().deserialize(value); | ||
return new Gson().fromJson(json, cls); | ||
} | ||
return null; | ||
} | ||
}); | ||
} | ||
|
||
public void addEntity(final T entity) { | ||
redisTemplate.execute(new RedisCallback<Object>() { | ||
|
||
@Override | ||
public Object doInRedis(RedisConnection connection) throws DataAccessException { | ||
connection.set(redisTemplate.getStringSerializer().serialize(entity.getClass().getName() + "_" + entity.getId()), | ||
redisTemplate.getStringSerializer().serialize(new Gson().toJson(entity))); | ||
return null; | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void updateEntity(final T entity) { | ||
redisTemplate.execute(new RedisCallback<Object>() { | ||
|
||
@Override | ||
public Object doInRedis(RedisConnection connection) throws DataAccessException { | ||
connection.set(redisTemplate.getStringSerializer().serialize(entity.getClass().getName() + "_" + entity.getId()), | ||
redisTemplate.getStringSerializer().serialize(new Gson().toJson(entity))); | ||
return null; | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void deleteEntityById(final Class<T> cls, final Integer id) { | ||
redisTemplate.execute(new RedisCallback<Object>() { | ||
|
||
@Override | ||
public Object doInRedis(RedisConnection connection) throws DataAccessException { | ||
connection.del(redisTemplate.getStringSerializer().serialize(cls.getName() + "_" + id.toString())); | ||
return null; | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public List<T> selectAll(Class<T> cls) { | ||
return null; | ||
} | ||
|
||
public void setRedisTemplate(RedisTemplate<Serializable, Serializable> redisTemplate) { | ||
this.redisTemplate = redisTemplate; | ||
} | ||
|
||
@Override | ||
public List<T> findByLike(Example example) { | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
@Override | ||
public List<T> findByPage(Example example, FlexiPageDto flexiPageDto) { | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
@Override | ||
public int findRowCount(Example example) { | ||
// TODO Auto-generated method stub | ||
return 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.training.core.redis; | ||
|
||
import java.io.Serializable; | ||
import java.lang.reflect.Method; | ||
|
||
import org.springframework.cache.CacheManager; | ||
import org.springframework.cache.annotation.CachingConfigurerSupport; | ||
import org.springframework.cache.annotation.EnableCaching; | ||
import org.springframework.cache.interceptor.KeyGenerator; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.cache.RedisCacheManager; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
|
||
@Configuration | ||
@EnableCaching | ||
public class RedisConfig extends CachingConfigurerSupport { | ||
|
||
@Bean | ||
public KeyGenerator biliKeyGenerator(){ | ||
return new KeyGenerator() { | ||
|
||
@Override | ||
public Object generate(Object target, Method method, Object... params) { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(target.getClass().getName()); | ||
sb.append(method.getName()); | ||
for (Object obj : params) { | ||
sb.append(obj.toString()); | ||
} | ||
return sb.toString(); | ||
} | ||
}; | ||
|
||
} | ||
|
||
@Bean | ||
public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) { | ||
return new RedisCacheManager(redisTemplate); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<Serializable, Serializable> redisTemplate(RedisConnectionFactory redisConnectionFactory) { | ||
RedisTemplate<Serializable, Serializable> redisTemplate = new RedisTemplate<Serializable, Serializable>(); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory); | ||
return redisTemplate; | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/training/core/redis/RedisObjectSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.training.core.redis; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.core.serializer.support.DeserializingConverter; | ||
import org.springframework.core.serializer.support.SerializingConverter; | ||
import org.springframework.data.redis.serializer.RedisSerializer; | ||
import org.springframework.data.redis.serializer.SerializationException; | ||
|
||
public class RedisObjectSerializer implements RedisSerializer<Object> { | ||
|
||
private Converter<Object, byte[]> serializer = new SerializingConverter(); | ||
private Converter<byte[], Object> deserializer = new DeserializingConverter(); | ||
|
||
static final byte[] EMPTY_ARRAY = new byte[0]; | ||
|
||
public Object deserialize(byte[] bytes) { | ||
if (isEmpty(bytes)) { | ||
return null; | ||
} | ||
|
||
try { | ||
return deserializer.convert(bytes); | ||
} catch (Exception ex) { | ||
throw new SerializationException("Cannot deserialize", ex); | ||
} | ||
} | ||
|
||
public byte[] serialize(Object object) { | ||
if (object == null) { | ||
return EMPTY_ARRAY; | ||
} | ||
|
||
try { | ||
return serializer.convert(object); | ||
} catch (Exception ex) { | ||
return EMPTY_ARRAY; | ||
} | ||
} | ||
|
||
private boolean isEmpty(byte[] data) { | ||
return (data == null || data.length == 0); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/training/core/service/impl/RedisBaseServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.training.core.service.impl; | ||
|
||
import javax.annotation.Resource; | ||
|
||
import com.training.core.dao.BaseDao; | ||
import com.training.core.entity.BaseEntity; | ||
import com.training.core.service.BaseService; | ||
import com.training.core.util.GenericeClassUtils; | ||
|
||
import java.util.List; | ||
|
||
public class RedisBaseServiceImpl<T extends BaseEntity> implements BaseService<T> { | ||
|
||
@SuppressWarnings("unchecked") | ||
protected Class<T> entityClass = (Class<T>) GenericeClassUtils.getSuperClassGenricType(this.getClass(), 0); | ||
|
||
@Resource(name = "redisBaseDao") | ||
private BaseDao<T> baseDao; | ||
|
||
@Override | ||
public T getEntityById(Integer id) { | ||
return baseDao.getEntityById(entityClass, id); | ||
} | ||
|
||
@Override | ||
public void addEntity(T entity) { | ||
baseDao.addEntity(entity); | ||
} | ||
|
||
@Override | ||
public void updateEntity(T entity) { | ||
baseDao.updateEntity(entity); | ||
} | ||
|
||
@Override | ||
public void deleteEntityById(Integer id) { | ||
baseDao.deleteEntityById(entityClass, id); | ||
} | ||
|
||
@Override | ||
public List<T> selectAll() { | ||
return baseDao.selectAll(entityClass); | ||
} | ||
|
||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/training/redis/controller/DemoRedisController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.training.redis.controller; | ||
|
||
import javax.annotation.Resource; | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.training.core.controller.BaseRedisController; | ||
import com.training.redis.entity.DemoRedis; | ||
import com.training.redis.service.DemoRedisService; | ||
|
||
@RestController | ||
@RequestMapping(value="/demoRedis") | ||
public class DemoRedisController extends BaseRedisController<DemoRedis> { | ||
|
||
@Resource | ||
private DemoRedisService demoRedisService; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.training.redis.entity; | ||
|
||
import com.training.core.entity.BaseEntity; | ||
|
||
@SuppressWarnings("serial") | ||
public class DemoRedis extends BaseEntity { | ||
|
||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/training/redis/service/DemoRedisService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.training.redis.service; | ||
|
||
import com.training.core.service.BaseService; | ||
import com.training.redis.entity.DemoRedis; | ||
|
||
public interface DemoRedisService extends BaseService<DemoRedis> { | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/training/redis/service/impl/DemoRedisServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.training.redis.service.impl; | ||
|
||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import com.training.core.service.impl.RedisBaseServiceImpl; | ||
import com.training.redis.entity.DemoRedis; | ||
import com.training.redis.service.DemoRedisService; | ||
|
||
@Service | ||
public class DemoRedisServiceImpl extends RedisBaseServiceImpl<DemoRedis> implements DemoRedisService { | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters