<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.19</version> </dependency>
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
// 篇幅受限,省略了
@Bean
public JedisPoolConfig getRedisConfig(){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(maxIdle);
config.setMaxTotal(maxTotal);
config.setMaxWaitMillis(maxWaitMillis);
config.setMinIdle(minIdle);
return config;
}
@Bean(name = "jedisConnectionFactory")
public JedisConnectionFactory getConnectionFactory(){
JedisConnectionFactory factory = new JedisConnectionFactory();
JedisPoolConfig config = getRedisConfig();
factory.setPoolConfig(config);
factory.setHostName(host);
factory.setPort(port);
factory.setDatabase(database);
factory.setPassword(password);
factory.setTimeout(timeout);
return factory;
}
@Bean(name = "redisTemplate")
public RedisTemplate<?, ?> getRedisTemplate(){
RedisTemplate<?,?> template = new StringRedisTemplate(getConnectionFactory());
return template;
}
}
#redis spring.redis.host=10.93.84.53 spring.redis.port=6379 spring.redis.password=bigdata123 spring.redis.database=15 spring.redis.timeout=0 spring.redis.pool.maxTotal=8 spring.redis.pool.maxWaitMillis=1000 spring.redis.pool.maxIdle=8 spring.redis.pool.minIdle=0
public class MybatisRedisCache implements Cache {
private static JedisConnectionFactory jedisConnectionFactory;
private final String id;
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
public MybatisRedisCache(final String id) {
if (id == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
this.id = id;
}
@Override
public void clear() {
RedisConnection connection = null;
try {
connection = jedisConnectionFactory.getConnection();
connection.flushDb();
connection.flushAll();
} catch (JedisConnectionException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
}
@Override
public String getId() {
return this.id;
}
@Override
public Object getObject(Object key) {
Object result = null;
RedisConnection connection = null;
try {
connection = jedisConnectionFactory.getConnection();
RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();
result = serializer.deserialize(connection.get(serializer.serialize(key)));
} catch (JedisConnectionException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
return result;
}
@Override
public ReadWriteLock getReadWriteLock() {
return this.readWriteLock;
}
@Override
public int getSize() {
int result = 0;
RedisConnection connection = null;
try {
connection = jedisConnectionFactory.getConnection();
result = Integer.valueOf(connection.dbSize().toString());
} catch (JedisConnectionException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
return result;
}
@Override
public void putObject(Object key, Object value) {
RedisConnection connection = null;
try {
connection = jedisConnectionFactory.getConnection();
RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();
connection.set(serializer.serialize(key), serializer.serialize(value));
} catch (JedisConnectionException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
}
@Override
public Object removeObject(Object key) {
RedisConnection connection = null;
Object result = null;
try {
connection = jedisConnectionFactory.getConnection();
RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();
result = connection.expire(serializer.serialize(key), 0);
} catch (JedisConnectionException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
return result;
}
public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {
MybatisRedisCache.jedisConnectionFactory = jedisConnectionFactory;
}
}
@Component
public class RedisCacheTransfer {
@Autowired
public void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {
MybatisRedisCache.setJedisConnectionFactory(jedisConnectionFactory);
}
}
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 全局参数 --> <settings> <!-- 使全局的映射器启用或禁用缓存。 --> <setting name="cacheEnabled" value="true"/> </settings> </configuration>
@Bean(name = "moonlightSqlSessionFactory")
@Primary
public SqlSessionFactory moonlightSqlSessionFactory(@Qualifier("moonlightData") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis-mapper/*.xml"));
bean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
return bean.getObject();
}
<mapper namespace="com.kangaroo.studio.moonlight.dao.mapper.MoonlightMapper">
<cache type="com.kangaroo.studio.moonlight.dao.cache.MybatisRedisCache"/>
<resultMap id="geoFenceList" type="com.kangaroo.studio.moonlight.dao.model.GeoFence">
<constructor>
<idArg column="id" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="name" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="type" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="group" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="geo" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="createTime" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="updateTime" javaType="java.lang.String" jdbcType="VARCHAR" />
</constructor>
</resultMap>
<select id="queryGeoFence" parameterType="com.kangaroo.studio.moonlight.dao.model.GeoFenceQueryParam" resultMap="geoFenceList">
select <include refid="base_column"/> from geoFence where 1=1
<if test="type != null">
and type = #{type}
</if>
<if test="name != null">
and name like concat('%', #{name},'%')
</if>
<if test="group != null">
and `group` like concat('%', #{group},'%')
</if>
<if test="startTime != null">
and createTime >= #{startTime}
</if>
<if test="endTime != null">
and createTime <= #{endTime}
</if>
</select>
</mapper>
<cache type="com.kangaroo.studio.moonlight.dao.cache.MybatisRedisCache"/>
public class GeoFence implements Serializable {
// setter和getter省略
}
public class GeoFenceParam implements Serializable {
// setter和getter省略
}
@Mapper
public interface MoonlightMapper {
List<GeoFence> queryGeoFence(GeoFenceQueryParam geoFenceQueryParam);
}
@RequestMapping(value = "/fence/query", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<Response> queryFence(@RequestBody GeoFenceQueryParam geoFenceQueryParam) {
try {
Integer pageNum = geoFenceQueryParam.getPageNum()!=null?geoFenceQueryParam.getPageNum():1;
Integer pageSize = geoFenceQueryParam.getPageSize()!=null?geoFenceQueryParam.getPageSize():10;
PageHelper.startPage(pageNum, pageSize);
List<GeoFence> list = moonlightMapper.queryGeoFence(geoFenceQueryParam);
return new ResponseEntity<>(
new Response(ResultCode.SUCCESS, "查询geoFence成功", list),
HttpStatus.OK);
} catch (Exception e) {
logger.error("查询geoFence失败", e);
return new ResponseEntity<>(
new Response(ResultCode.EXCEPTION, "查询geoFence失败", null),
HttpStatus.INTERNAL_SERVER_ERROR);
}
curl -H "Content-Type:application/json" -XPOST http://。。。/moonlight/fence/query -d '{
"name" : "test",
"group": "test",
"type": 1,
"startTime":"2017-12-06 00:00:00",
"endTime":"2017-12-06 16:00:00",
"pageNum": 1,
"pageSize": 8
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有