├── README.md ├── spring-redis ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── spring │ │ │ └── redis │ │ │ ├── controller │ │ │ └── RedisController.java │ │ │ ├── pojo │ │ │ └── User.java │ │ │ └── util │ │ │ └── RedisUtil.java │ ├── resources │ │ ├── log4j.properties │ │ ├── redis.properties │ │ └── spring │ │ │ ├── applicationContext-redis.xml │ │ │ ├── applicationContext.xml │ │ │ └── spring-redis-servlet.xml │ └── webapp │ │ └── WEB-INF │ │ └── web.xml │ └── test │ └── java │ └── com │ └── spring │ └── redis │ └── util │ └── RedisUtilTest.java ├── wyait-common ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── wyait │ └── common │ └── utils │ ├── DateUtil.java │ ├── JSONUtils.java │ └── ValidateUtil.java └── wyait-redis ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── wyait │ │ └── redis │ │ ├── WyaitRedisApplication.java │ │ ├── config │ │ └── RedisConfig.java │ │ ├── controller │ │ └── RedisCacheController.java │ │ ├── pojo │ │ └── Cat.java │ │ ├── service │ │ └── CatService.java │ │ └── utils │ │ └── RedisUtils.java └── resources │ └── application.properties └── test └── java └── com └── wyait └── redis └── WyaitRedisApplicationTests.java /README.md: -------------------------------------------------------------------------------- 1 | # project 2 | project 公开项目,供大家使用,快速上手;并附上博客链接,详细描述使用方法和注意事项; 项目中的技术框架稳定版本不定时更新,欢迎关注; 3 | 4 | 项目中的技术框架稳定版本不定时更新,欢迎关注; 5 | 6 | **项目说明**: 7 | [ **wyait-common** ](https://github.com/wyait/project.git) : 通用工具类; 8 | [ **wyait-redis** ](https://github.com/wyait/project.git):[spring boot 1.5.9整合redis;包含redis封装的API工具类和缓存管理的注解配置和使用;http://blog.51cto.com/wyait/2048478](http://blog.51cto.com/wyait/2048478); 9 | [ **spring-redis** ](https://github.com/wyait/project.git):[spring整合redis;包含redis封装的API工具类和缓存管理的配置、使用和注意事项;http://blog.51cto.com/wyait/2049866](http://blog.51cto.com/wyait/2049866); 10 | [ **wyait-manage** ](https://github.com/wyait/project.git):[spring boot + mybatis + shiro + layui 搭建的后台权限管理系统;http://blog.51cto.com/wyait/2082803](http://blog.51cto.com/wyait/2082803); 11 | -------------------------------------------------------------------------------- /spring-redis/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.spring.redis 5 | spring-redis 6 | 0.0.1-SNAPSHOT 7 | war 8 | 9 | 21 | 22 | org.springframework 23 | spring-context 24 | 4.3.5.RELEASE 25 | 26 | 27 | org.springframework 28 | spring-beans 29 | 4.3.5.RELEASE 30 | 31 | 32 | org.springframework 33 | spring-webmvc 34 | 4.3.5.RELEASE 35 | 36 | 37 | 38 | redis.clients 39 | jedis 40 | 2.9.0 41 | 42 | 43 | 44 | org.springframework.data 45 | spring-data-redis 46 | 1.7.5.RELEASE 47 | 48 | 49 | 50 | com.fasterxml.jackson.core 51 | jackson-databind 52 | 2.8.9 53 | 54 | 55 | 56 | org.apache.commons 57 | commons-lang3 58 | 3.6 59 | 60 | 61 | commons-io 62 | commons-io 63 | 2.5 64 | 65 | 66 | 67 | org.slf4j 68 | slf4j-log4j12 69 | 1.7.25 70 | 71 | 72 | 73 | 74 | 75 | 76 | org.apache.tomcat.maven 77 | tomcat7-maven-plugin 78 | 79 | 8063 80 | / 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /spring-redis/src/main/java/com/spring/redis/controller/RedisController.java: -------------------------------------------------------------------------------- 1 | package com.spring.redis.controller; 2 | 3 | import java.util.HashMap; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RequestMethod; 9 | import org.springframework.web.bind.annotation.ResponseBody; 10 | 11 | import com.spring.redis.pojo.User; 12 | import com.spring.redis.util.RedisUtil; 13 | 14 | @Controller 15 | @RequestMapping("/") 16 | public class RedisController { 17 | @Autowired 18 | private RedisUtil redisUtil; 19 | 20 | @RequestMapping(value = "add", method = RequestMethod.GET) 21 | @ResponseBody 22 | public String add() { 23 | System.out.println("================华丽进======================="); 24 | this.redisUtil.set("aaa", "aaa"); 25 | System.out.println("================华丽出======================="); 26 | return "hello world"; 27 | } 28 | 29 | @RequestMapping(value = "get", method = RequestMethod.GET) 30 | @ResponseBody 31 | public String get() { 32 | System.out.println("================获取redis中数据======================="); 33 | return String.valueOf(this.redisUtil.get("aaa")); 34 | } 35 | 36 | @RequestMapping(value = "addHash", method = RequestMethod.GET) 37 | @ResponseBody 38 | public String addHash() { 39 | System.out.println("================华丽进===addHash===================="); 40 | User u = new User(); 41 | u.setAge(11); 42 | u.setId(1L); 43 | u.setName("李四"); 44 | // 存储hash结构的数据,可以是HashMap 45 | this.redisUtil.setHash("mmm", "bbb:ccc", u); 46 | HashMap map = new HashMap(); 47 | map.put("user", u); 48 | this.redisUtil.setHash("mmm", "nnn", map); 49 | System.out.println("================华丽出====addHash==================="); 50 | return "hello world"; 51 | } 52 | 53 | @RequestMapping(value = "getHash", method = RequestMethod.GET) 54 | @ResponseBody 55 | public HashMap getHash() { 56 | System.out 57 | .println("================获取redis中数据========getHash==============="); 58 | User user = (User) this.redisUtil.getHash("mmm", "bbb:ccc"); 59 | System.out 60 | .println("================获取redis中数据========getHash===============" 61 | + user); 62 | return (HashMap) this.redisUtil.getHash("mmm", "nnn"); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /spring-redis/src/main/java/com/spring/redis/pojo/User.java: -------------------------------------------------------------------------------- 1 | package com.spring.redis.pojo; 2 | 3 | import java.io.Serializable; 4 | 5 | public class User implements Serializable { 6 | private static final long serialVersionUID = 1L; 7 | private Long id; 8 | private Integer age; 9 | private String name; 10 | 11 | public Long getId() { 12 | return id; 13 | } 14 | 15 | public void setId(Long id) { 16 | this.id = id; 17 | } 18 | 19 | public Integer getAge() { 20 | return age; 21 | } 22 | 23 | public void setAge(Integer age) { 24 | this.age = age; 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return "User [id=" + id + ", age=" + age + ", name=" + name + "]"; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /spring-redis/src/main/java/com/spring/redis/util/RedisUtil.java: -------------------------------------------------------------------------------- 1 | package com.spring.redis.util; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | import java.util.Set; 6 | import java.util.concurrent.TimeUnit; 7 | 8 | import org.apache.commons.lang3.StringUtils; 9 | import org.springframework.data.redis.core.RedisTemplate; 10 | import org.springframework.util.CollectionUtils; 11 | 12 | /** 13 | * 14 | * @项目名称:common 15 | * @类名称:RedisUtil 16 | * @类描述:基于spring和redis的redisTemplate工具类; 17 | * @创建人:wyait 18 | * @创建时间:2017年12月8日 下午3:32:38 19 | * @version:V1.0 20 | */ 21 | // @Component 22 | public class RedisUtil { 23 | // 通过构造方法注入 24 | // @Autowired 25 | private RedisTemplate redisTemplate; 26 | 27 | /* 28 | * 如果使用注解注入RedisTemplate对象,则不需要该setter方法 29 | */ 30 | public void setRedisTemplate(RedisTemplate redisTemplate) { 31 | this.redisTemplate = redisTemplate; 32 | } 33 | 34 | /** 35 | * String类型缓存获取 36 | * @param key 键 37 | * @return 值 38 | */ 39 | public Object get(String key) { 40 | return key == null ? null : redisTemplate.opsForValue().get(key); 41 | } 42 | 43 | /** 44 | * String类型缓存保存 45 | * @param key 键 46 | * @param value 值 47 | * @return true:成功;false:失败 48 | */ 49 | public boolean set(String key, Object value) { 50 | try { 51 | if (StringUtils.isNotEmpty(key) && null != value) { 52 | redisTemplate.opsForValue().set(key, value); 53 | return true; 54 | } 55 | } catch (Exception e) { 56 | e.printStackTrace(); 57 | } 58 | return false; 59 | } 60 | 61 | /** 62 | * 指定缓存失效时间 63 | * @param key 键 64 | * @param time 时间(秒) 65 | * @return true:成功;false:失败 66 | */ 67 | public boolean expire(String key, long time) { 68 | try { 69 | if (StringUtils.isNotEmpty(key) && time > 0) { 70 | redisTemplate.expire(key, time, TimeUnit.SECONDS); 71 | return true; 72 | } 73 | } catch (Exception e) { 74 | e.printStackTrace(); 75 | } 76 | return false; 77 | } 78 | 79 | /** 80 | * 根据key获取过期时间 81 | * @param key 键 不能为null 82 | * @return 时间(秒)返回0代表为永久有效;-1代表key不存在 83 | */ 84 | public long getExpire(String key) { 85 | if (StringUtils.isNotEmpty(key)) { 86 | return redisTemplate.getExpire(key, TimeUnit.SECONDS); 87 | } 88 | return -1L; 89 | } 90 | 91 | /** 92 | * 判断key是否存在 93 | * @param key 键 94 | * @return true:存在;false:不存在 95 | */ 96 | public boolean exists(String key) { 97 | try { 98 | if (StringUtils.isNotEmpty(key)) { 99 | return redisTemplate.hasKey(key); 100 | } 101 | } catch (Exception e) { 102 | e.printStackTrace(); 103 | } 104 | return false; 105 | } 106 | 107 | /** 108 | * 删除缓存(批量) 109 | * @param keys(可变参数)可以传一个值或多个 110 | */ 111 | @SuppressWarnings("unchecked") 112 | public void delete(String... keys) { 113 | if (keys != null && keys.length > 0) { 114 | if (keys.length == 1) { 115 | redisTemplate.delete(keys[0]); 116 | } else { 117 | // 批量删除 118 | redisTemplate.delete(CollectionUtils.arrayToList(keys)); 119 | } 120 | } 121 | } 122 | 123 | /** 124 | * String类型缓存保存并设置时间 125 | * @param key 键 126 | * @param value 值 127 | * @param time 时间(秒):time要大于0 如果time小于等于0 将设置无限期 128 | * @return true:成功;false:失败 129 | */ 130 | public boolean set(String key, Object value, long time) { 131 | try { 132 | if (StringUtils.isNotEmpty(key) && null != value) { 133 | if (time > 0) { 134 | redisTemplate.opsForValue().set(key, value, time, 135 | TimeUnit.SECONDS); 136 | } else { 137 | set(key, value); 138 | } 139 | return true; 140 | } 141 | } catch (Exception e) { 142 | e.printStackTrace(); 143 | } 144 | return false; 145 | } 146 | 147 | /** 148 | * 递增 149 | * @param key 键 150 | * @param by 要增加几(大于0) 151 | * @return 152 | */ 153 | public long increment(String key, long delta) { 154 | if (delta < 0) { 155 | throw new RuntimeException("递增因子必须大于0"); 156 | } 157 | if (StringUtils.isNotEmpty(key)) { 158 | return redisTemplate.opsForValue().increment(key, delta); 159 | } else { 160 | return -1L; 161 | } 162 | } 163 | 164 | /** 165 | * 递减 166 | * @param key 键 167 | * @param by 要减少几(小于0) 168 | * @return 169 | */ 170 | public long decrement(String key, long delta) { 171 | if (delta < 0) { 172 | throw new RuntimeException("递减因子必须大于0"); 173 | } 174 | if (StringUtils.isNotEmpty(key)) { 175 | return redisTemplate.opsForValue().increment(key, -delta); 176 | } else { 177 | return -1L; 178 | } 179 | } 180 | 181 | /** 182 | * 获取hash结构的数据 (Hash类型) 183 | * @param key 键: 不能为null 184 | * @param item 项: 不能为null 185 | * @return 值 186 | */ 187 | public Object getHash(String key, String item) { 188 | if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(item)) { 189 | return redisTemplate.opsForHash().get(key, item); 190 | } else { 191 | return null; 192 | } 193 | } 194 | 195 | /** 196 | * 获取Key对应的所有键值HashMap 197 | * @param key 键 198 | * @return 对应的多个键值 199 | */ 200 | public Map getHashMap(String key) { 201 | if (StringUtils.isNotEmpty(key)) { 202 | return redisTemplate.opsForHash().entries(key); 203 | } else { 204 | return null; 205 | } 206 | } 207 | 208 | /** 209 | * 保存hashMap结构的数据(HashMap类型) 210 | * @param key 键 211 | * @param map 对应多个键值 212 | * @return true:成功;false:失败 213 | */ 214 | public boolean setHashMap(String key, Map map) { 215 | try { 216 | if (StringUtils.isNotEmpty(key) && null != map && map.size() > 0) { 217 | redisTemplate.opsForHash().putAll(key, map); 218 | return true; 219 | } 220 | } catch (Exception e) { 221 | e.printStackTrace(); 222 | } 223 | return false; 224 | } 225 | 226 | /** 227 | * 保存hashMap结构的数据,并设置时间 (HashMap类型) 228 | * @param key 键 229 | * @param map 对应多个键值 230 | * @param time 时间(秒) 231 | * @return true:成功; false:失败 232 | */ 233 | public boolean setHashMap(String key, Map map, long time) { 234 | try { 235 | if (StringUtils.isNotEmpty(key) && null != map && map.size() > 0) { 236 | redisTemplate.opsForHash().putAll(key, map); 237 | if (time > 0) { 238 | expire(key, time); 239 | } 240 | return true; 241 | } 242 | } catch (Exception e) { 243 | e.printStackTrace(); 244 | } 245 | return false; 246 | } 247 | 248 | /** 249 | * 向一个hash类型的数据中保存数据,如果不存在将创建 250 | * @param key 键 251 | * @param item 项 252 | * @param value 值 253 | * @return true:成功;false:失败 254 | */ 255 | public boolean setHash(String key, String item, Object value) { 256 | try { 257 | if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(item) 258 | && null != value) { 259 | redisTemplate.opsForHash().put(key, item, value); 260 | return true; 261 | } 262 | } catch (Exception e) { 263 | e.printStackTrace(); 264 | } 265 | return false; 266 | } 267 | 268 | /** 269 | * 向一个hash类型的数据中保存数据,如果不存在将创建,指定保存时间 270 | * @param key 键 271 | * @param item 项 272 | * @param value 值 273 | * @param time 时间(秒) 注意:如果已存在的hash数据时间,这里将会替换原有的时间 274 | * @return true:成功;false:失败 275 | */ 276 | public boolean setHash(String key, String item, Object value, long time) { 277 | try { 278 | if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(item) 279 | && null != value) { 280 | redisTemplate.opsForHash().put(key, item, value); 281 | if (time > 0) { 282 | expire(key, time); 283 | } 284 | return true; 285 | } 286 | } catch (Exception e) { 287 | e.printStackTrace(); 288 | } 289 | return false; 290 | } 291 | 292 | /** 293 | * 删除hash数据中的值 294 | * @param key 键 不能为null 295 | * @param item 项 可以使多个 不能为null 296 | */ 297 | public void deleteHash(String key, Object... item) { 298 | if (StringUtils.isNotEmpty(key) && null != item) { 299 | redisTemplate.opsForHash().delete(key, item); 300 | } 301 | } 302 | 303 | /** 304 | * 判断hash数据中是否有该项的值 305 | * @param key 键 不能为null 306 | * @param item 项 不能为null 307 | * @return true:存在; false:不存在 308 | */ 309 | public boolean existHashKey(String key, String item) { 310 | if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(item)) { 311 | return redisTemplate.opsForHash().hasKey(key, item); 312 | } else { 313 | return false; 314 | } 315 | } 316 | 317 | /** 318 | * hash递增,如果不存在,就会创建一个,并把新增后的值返回 319 | * @param key 键 320 | * @param item 项 321 | * @param by 要增加几(大于0) 322 | * @return -1,失败 323 | */ 324 | public double incrementHash(String key, String item, double by) { 325 | if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(item)) { 326 | return redisTemplate.opsForHash().increment(key, item, by); 327 | } else { 328 | return -1; 329 | } 330 | } 331 | 332 | /** 333 | * hash递减 334 | * @param key 键 335 | * @param item 项 336 | * @param by 要减少记(小于0) 337 | * @return -1,失败 338 | */ 339 | public double decrementHash(String key, String item, double by) { 340 | if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(item)) { 341 | return redisTemplate.opsForHash().increment(key, item, -by); 342 | } else { 343 | return -1; 344 | } 345 | } 346 | 347 | // ============================set============================= 348 | /** 349 | * 根据key获取Set中的所有值 350 | * @param key 键 351 | * @return 352 | */ 353 | public Set getSet(String key) { 354 | try { 355 | if (StringUtils.isNotEmpty(key)) { 356 | return redisTemplate.opsForSet().members(key); 357 | } 358 | } catch (Exception e) { 359 | e.printStackTrace(); 360 | } 361 | return null; 362 | } 363 | 364 | /** 365 | * 根据value从一个set中查询,是否存在 366 | * @param key 键 367 | * @param value 值 368 | * @return true:存在; false:不存在 369 | */ 370 | public boolean existSetKey(String key, Object value) { 371 | try { 372 | if (StringUtils.isNotEmpty(key) && null != value) { 373 | return redisTemplate.opsForSet().isMember(key, value); 374 | } 375 | } catch (Exception e) { 376 | e.printStackTrace(); 377 | } 378 | return false; 379 | } 380 | 381 | /** 382 | * 将数据放入set缓存 383 | * @param key 键 384 | * @param values 值 可以是多个 385 | * @return 成功个数 386 | */ 387 | public long setSet(String key, Object... values) { 388 | try { 389 | if (StringUtils.isNotEmpty(key) && null != values) { 390 | return redisTemplate.opsForSet().add(key, values); 391 | } 392 | } catch (Exception e) { 393 | e.printStackTrace(); 394 | } 395 | return 0; 396 | } 397 | 398 | /** 399 | * 将set数据放入缓存,指定保存时间 400 | * @param key 键 401 | * @param time 时间(秒) 402 | * @param values 值 可以是多个 403 | * @return 成功个数 404 | */ 405 | public long setSet(String key, long time, Object... values) { 406 | try { 407 | if (StringUtils.isNotEmpty(key) && null != values) { 408 | Long count = redisTemplate.opsForSet().add(key, values); 409 | if (time > 0) 410 | expire(key, time); 411 | return count; 412 | } 413 | } catch (Exception e) { 414 | e.printStackTrace(); 415 | } 416 | return 0; 417 | } 418 | 419 | /** 420 | * 获取set缓存的长度 421 | * @param key 键 422 | * @return 423 | */ 424 | public long getSetSize(String key) { 425 | try { 426 | if (StringUtils.isNotEmpty(key)) { 427 | return redisTemplate.opsForSet().size(key); 428 | } 429 | } catch (Exception e) { 430 | e.printStackTrace(); 431 | } 432 | return 0; 433 | } 434 | 435 | /** 436 | * 移除值为value的 437 | * @param key 键 438 | * @param values 值 可以是多个 439 | * @return 移除的个数 440 | */ 441 | public long removeSet(String key, Object... values) { 442 | try { 443 | if (StringUtils.isNotEmpty(key) && null != values) { 444 | Long count = redisTemplate.opsForSet().remove(key, values); 445 | return count; 446 | } 447 | } catch (Exception e) { 448 | e.printStackTrace(); 449 | } 450 | return 0; 451 | } 452 | 453 | // ===============================list================================= 454 | 455 | /** 456 | * 获取list缓存的内容 457 | * @param key 键 458 | * @param start 开始 459 | * @param end 结束 0 到 -1代表所有值 460 | * @return 461 | */ 462 | public List getList(String key, long start, long end) { 463 | try { 464 | if (StringUtils.isNotEmpty(key)) { 465 | return redisTemplate.opsForList().range(key, start, end); 466 | } 467 | } catch (Exception e) { 468 | e.printStackTrace(); 469 | } 470 | return null; 471 | } 472 | 473 | /** 474 | * 获取list缓存的长度 475 | * @param key 键 476 | * @return 477 | */ 478 | public long getListSize(String key) { 479 | try { 480 | if (StringUtils.isNotEmpty(key)) { 481 | return redisTemplate.opsForList().size(key); 482 | } 483 | } catch (Exception e) { 484 | e.printStackTrace(); 485 | } 486 | return 0; 487 | } 488 | 489 | /** 490 | * 通过索引 获取list中的值 491 | * @param key 键 492 | * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推 493 | * @return 494 | */ 495 | public Object getListIndex(String key, long index) { 496 | try { 497 | if (StringUtils.isNotEmpty(key)) { 498 | return redisTemplate.opsForList().index(key, index); 499 | } 500 | } catch (Exception e) { 501 | e.printStackTrace(); 502 | } 503 | return null; 504 | } 505 | 506 | /** 507 | * 将list放入缓存,指定时间 508 | * @param key 键 509 | * @param value 值 510 | * @return 511 | */ 512 | public boolean setList(String key, Object value) { 513 | try { 514 | if (StringUtils.isNotEmpty(key) && null != value) { 515 | redisTemplate.opsForList().rightPush(key, value); 516 | return true; 517 | } 518 | } catch (Exception e) { 519 | e.printStackTrace(); 520 | } 521 | return false; 522 | } 523 | 524 | /** 525 | * 将list放入缓存,指定保存时间 526 | * @param key 键 527 | * @param value 值 528 | * @param time 时间(秒) 529 | * @return 530 | */ 531 | public boolean setList(String key, Object value, long time) { 532 | try { 533 | if (StringUtils.isNotEmpty(key) && null != value) { 534 | redisTemplate.opsForList().rightPush(key, value); 535 | if (time > 0) 536 | expire(key, time); 537 | return true; 538 | } 539 | } catch (Exception e) { 540 | e.printStackTrace(); 541 | } 542 | return false; 543 | } 544 | 545 | /** 546 | * 将list放入缓存 547 | * @param key 键 548 | * @param value 值 549 | * @return 550 | */ 551 | public boolean setList(String key, List value) { 552 | try { 553 | if (StringUtils.isNotEmpty(key) && null != value) { 554 | redisTemplate.opsForList().rightPushAll(key, value); 555 | return true; 556 | } 557 | } catch (Exception e) { 558 | e.printStackTrace(); 559 | } 560 | return false; 561 | } 562 | 563 | /** 564 | * 将list放入缓存,指定时间 565 | * @param key 键 566 | * @param value 值 567 | * @param time 时间(秒) 568 | * @return 569 | */ 570 | public boolean setList(String key, List value, long time) { 571 | try { 572 | if (StringUtils.isNotEmpty(key) && null != value) { 573 | redisTemplate.opsForList().rightPushAll(key, value); 574 | if (time > 0) 575 | expire(key, time); 576 | return true; 577 | } 578 | } catch (Exception e) { 579 | e.printStackTrace(); 580 | } 581 | return false; 582 | } 583 | 584 | /** 585 | * 根据索引修改list中的某条数据 586 | * @param key 键 587 | * @param index 索引 588 | * @param value 值 589 | * @return 590 | */ 591 | public boolean updateListIndex(String key, long index, Object value) { 592 | try { 593 | if (StringUtils.isNotEmpty(key) && null != value) { 594 | redisTemplate.opsForList().set(key, index, value); 595 | return true; 596 | } 597 | } catch (Exception e) { 598 | e.printStackTrace(); 599 | } 600 | return false; 601 | } 602 | 603 | /** 604 | * 移除N个值为value 605 | * @param key 键 606 | * @param count 移除多少个 607 | * @param value 值 608 | * @return 移除的个数 609 | */ 610 | public long removeList(String key, long count, Object value) { 611 | try { 612 | if (StringUtils.isNotEmpty(key) && null != value) { 613 | Long remove = redisTemplate.opsForList().remove(key, count, 614 | value); 615 | return remove; 616 | } 617 | } catch (Exception e) { 618 | e.printStackTrace(); 619 | } 620 | return 0; 621 | } 622 | 623 | } -------------------------------------------------------------------------------- /spring-redis/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=DEBUG,spring-redis,spring-redisConsole 2 | log4j.logger.org.mybatis = DEBUG 3 | #log4j.rootLogger=INFO,spring-redis,spring-redisConsole 4 | #log4j.logger.org.mybatis = INFO 5 | log4j.appender.spring-redis=org.apache.log4j.ConsoleAppender 6 | log4j.appender.spring-redis.layout=org.apache.log4j.PatternLayout 7 | log4j.appender.spring-redis.layout.ConversionPattern=%m%n 8 | log4j.appender.spring-redis.File=D:/logs/spring-redis.log 9 | #stay file for 90 days 10 | log4j.appender.spring-redis.layout.ConversionPattern =[%d{yyyy:MM:dd HH:mm:ss}]:%m \r\n 11 | 12 | log4j.appender.spring-redisConsole=org.apache.log4j.ConsoleAppender 13 | log4j.appender.spring-redisConsole.encoding=utf-8 14 | log4j.appender.spring-redisConsole.Target = System.out 15 | log4j.appender.spring-redisConsole.layout=org.apache.log4j.PatternLayout 16 | log4j.appender.spring-redisConsole.layout.ConversionPattern = [%d{yyyy:MM:dd HH:mm:ss}]:%m \r\n 17 | 18 | 19 | -------------------------------------------------------------------------------- /spring-redis/src/main/resources/redis.properties: -------------------------------------------------------------------------------- 1 | #ip地址 2 | redis.host.ip=127.0.0.1 3 | #端口号 4 | redis.port=6379 5 | #如果有密码 6 | redis.password= 7 | #客户端超时时间单位是毫秒 默认是2000 8 | redis.timeout=3000 9 | 10 | #最大空闲数 11 | redis.maxIdle=6 12 | #连接池的最大数据库连接数。设为0表示无限制,如果是jedis 2.4以后用redis.maxTotal 13 | #redis.maxActive=600 14 | #控制一个pool可分配多少个jedis实例,用来替换上面的redis.maxActive,如果是jedis 2.4以后用该属性 15 | redis.maxTotal=20 16 | #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。 17 | redis.maxWaitMillis=3000 18 | #连接的最小空闲时间 默认1800000毫秒(30分钟) 19 | redis.minEvictableIdleTimeMillis=300000 20 | #每次释放连接的最大数目,默认3 21 | redis.numTestsPerEvictionRun=4 22 | #逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1 23 | redis.timeBetweenEvictionRunsMillis=30000 24 | #是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个 25 | #redis.testOnBorrow=true 26 | ##在空闲时检查有效性, 默认false 27 | #redis.testWhileIdle=true 28 | 29 | #集群配置 30 | #redis.sentinel.host1=172.20.1.230 31 | #redis.sentinel.port1=26379 32 | 33 | 34 | #redis.sentinel.host2=172.20.1.231 35 | #redis.sentinel.port2=26379 36 | 37 | 38 | #redis.sentinel.host3=172.20.1.232 39 | #redis.sentinel.port3=26379 -------------------------------------------------------------------------------- /spring-redis/src/main/resources/spring/applicationContext-redis.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 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 | -------------------------------------------------------------------------------- /spring-redis/src/main/resources/spring/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | classpath:log4j.properties 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /spring-redis/src/main/resources/spring/spring-redis-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /spring-redis/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | spring-redis 7 | 8 | 9 | 10 | contextConfigLocation 11 | classpath:spring/applicationContext*.xml 12 | 13 | 14 | 15 | org.springframework.web.context.ContextLoaderListener 16 | 17 | 18 | 19 | 20 | encodingFilter 21 | org.springframework.web.filter.CharacterEncodingFilter 22 | 23 | encoding 24 | UTF8 25 | 26 | 27 | 28 | encodingFilter 29 | /* 30 | 31 | 32 | 33 | 34 | spring-redis 35 | org.springframework.web.servlet.DispatcherServlet 36 | 37 | contextConfigLocation 38 | classpath:spring/spring-redis-servlet.xml 39 | 40 | 1 41 | 42 | 43 | 44 | 45 | spring-redis 46 | / 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring-redis/src/test/java/com/spring/redis/util/RedisUtilTest.java: -------------------------------------------------------------------------------- 1 | /*package com.spring.redis.util; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.test.context.ContextConfiguration; 8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 | 10 | @RunWith(SpringJUnit4ClassRunner.class) 11 | //@ContextConfiguration({"classpath:spring/spring-redis-servlet.xml","classpath:spring/applicationContext.xml","classpath:spring/applicationContext-redis.xml"}) 12 | public class RedisUtilTest { 13 | @Autowired(required=false) 14 | private RedisUtil redisUtil; 15 | 16 | @Before 17 | public void setUp() throws Exception { 18 | //TODO 19 | } 20 | 21 | 22 | @Test 23 | public void testExpire() { 24 | boolean flag = this.redisUtil.exists("aaa"); 25 | System.out.println("=======从redis中是否存在键为“aaa”的:" + flag); 26 | } 27 | 28 | @Test 29 | public void testGet() { 30 | String data = String.valueOf(this.redisUtil.get("aaa")); 31 | System.out.println("=======从redis中获得到的:" + data); 32 | } 33 | 34 | @Test 35 | public void testSetStringObject() { 36 | this.redisUtil.set("aaa", "test_value"); 37 | System.out.println("=======从redis设置aaa"); 38 | } 39 | 40 | @Test 41 | public void testDelete() { 42 | this.redisUtil.delete("aaa"); 43 | System.out.println("=======从redis中删除aaa"); 44 | } 45 | 46 | } 47 | */ -------------------------------------------------------------------------------- /wyait-common/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.wyait.common 8 | wyait-common 9 | 1.0-SNAPSHOT 10 | 11 | wyait-common 12 | 公用工具项目 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.5.9.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 25 | 1.8 26 | 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-maven-plugin 33 | 34 | 35 | 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-web 41 | 42 | 43 | spring-boot-starter-logging 44 | org.springframework.boot 45 | 46 | 47 | 48 | 49 | 50 | org.apache.commons 51 | commons-lang3 52 | 3.6 53 | 54 | 55 | commons-io 56 | commons-io 57 | 2.5 58 | 59 | 60 | 65 | 66 | org.springframework.boot 67 | spring-boot-starter-test 68 | test 69 | 70 | 71 | -------------------------------------------------------------------------------- /wyait-common/src/main/java/com/wyait/common/utils/DateUtil.java: -------------------------------------------------------------------------------- 1 | package com.wyait.common.utils; 2 | 3 | import java.io.ObjectStreamException; 4 | import java.io.Serializable; 5 | import java.text.DateFormat; 6 | import java.text.ParseException; 7 | import java.text.SimpleDateFormat; 8 | import java.util.Date; 9 | 10 | /** 11 | * 12 | * @项目名称:common 13 | * @类名称:DateUtil 14 | * @类描述:当前线程日期处理类("yyyy-MM-dd HH:mm:ss")[线程安全] 15 | * @创建人:wyait 16 | * @创建时间:2017年6月7日 下午6:24:23 17 | * @version: 18 | */ 19 | public class DateUtil implements AutoCloseable,Serializable{ 20 | private static final long serialVersionUID = 5110771010886130754L; 21 | //饿汉单例 22 | public static DateUtil instance = new DateUtil(); 23 | private DateUtil() { 24 | } 25 | 26 | public static DateUtil getInstance() { 27 | return instance; 28 | } 29 | //防序列化(杜绝单例对象被反序列化时重新生成对象) 30 | private Object readResolve() throws ObjectStreamException { 31 | return instance; 32 | } 33 | 34 | // SimpleDateFormat线程不安全的类,使用ThreadLocal, 35 | // 也是将共享变量变为独享,线程独享肯定能比方法独享在并发环境中能减少不少创建对象的开销。如果对性能要求比较高的情况下,一般推荐使用这种方法。 36 | private static ThreadLocal threadLocal = new ThreadLocal() { 37 | @Override 38 | protected DateFormat initialValue() { 39 | return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 40 | } 41 | }; 42 | 43 | /** 44 | * 45 | * @描述:格式化String转换为Date 46 | * @创建人:wyait 47 | * @创建时间:2017年6月7日 下午6:27:03 48 | * @param dateStr 49 | * @return Date 50 | * @throws ParseException 51 | */ 52 | public static Date parse(String dateStr) throws ParseException { 53 | return threadLocal.get().parse(dateStr); 54 | } 55 | 56 | /** 57 | * 58 | * @描述:将date日期转换为string 59 | * @创建人:wyait 60 | * @创建时间:2017年6月7日 下午6:27:14 61 | * @param date 62 | * @return 格式:yyyy-MM-dd HH:mm:ss 63 | */ 64 | public static String format(Date date) { 65 | return threadLocal.get().format(date); 66 | } 67 | 68 | @Override 69 | public void close() throws Exception { 70 | } 71 | 72 | } -------------------------------------------------------------------------------- /wyait-common/src/main/java/com/wyait/common/utils/JSONUtils.java: -------------------------------------------------------------------------------- 1 | package com.wyait.common.utils; 2 | 3 | import com.fasterxml.jackson.core.type.TypeReference; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | 6 | import java.util.ArrayList; 7 | import java.util.HashMap; 8 | import java.util.List; 9 | import java.util.Map; 10 | import java.util.Map.Entry; 11 | 12 | /** 13 | * 14 | * @项目名称:common 15 | * @类名称:JSONUtils 16 | * @类描述:JackSon工具类 17 | * @创建人:wyait 18 | * @创建时间:2016年7月20日 下午2:49:32 19 | * @version 20 | */ 21 | @SuppressWarnings("all") 22 | public class JSONUtils { 23 | 24 | private final static ObjectMapper objectMapper = new ObjectMapper(); 25 | 26 | private JSONUtils() { 27 | } 28 | 29 | private static final JSONUtils JSONUTIL = new JSONUtils(); 30 | 31 | public static JSONUtils getInstance() { 32 | return JSONUTIL; 33 | } 34 | 35 | /** 36 | * javaBean,list,array convert to json string 37 | *
对象转换为json字符串 38 | */ 39 | public static String obj2json(Object obj) throws Exception { 40 | return objectMapper.writeValueAsString(obj); 41 | } 42 | 43 | /** 44 | * json string convert to javaBean 45 | *
json字符串转换为实体类对象 46 | */ 47 | public static T json2pojo(String jsonStr, Class clazz) 48 | throws Exception { 49 | return objectMapper.readValue(jsonStr, clazz); 50 | } 51 | 52 | /** 53 | * json string convert to map 54 | *
json字符串转换为Map对象 55 | */ 56 | public static Map json2map(String jsonStr) 57 | throws Exception { 58 | return objectMapper.readValue(jsonStr, Map.class); 59 | } 60 | 61 | /** 62 | * json string convert to map , convert to map with javaBean 63 | *
json字符串转换为Map对象,然后再转换为Map对象 64 | */ 65 | public static Map json2map(String jsonStr, Class clazz) 66 | throws Exception { 67 | Map> map = objectMapper.readValue(jsonStr, 68 | new TypeReference>() { 69 | }); 70 | Map result = new HashMap(); 71 | for (Entry> entry : map.entrySet()) { 72 | result.put(entry.getKey(), map2pojo(entry.getValue(), clazz)); 73 | } 74 | return result; 75 | } 76 | 77 | /** 78 | * json array string convert to list>,convert to list with javaBean 79 | *
json字符串转换为Map对象,然后再转换为List对象 80 | */ 81 | public static List json2map2list(String jsonArrayStr, Class clazz) 82 | throws Exception { 83 | List> list = objectMapper.readValue(jsonArrayStr, 84 | new TypeReference>() { 85 | }); 86 | List result = new ArrayList(); 87 | for (Map map : list) { 88 | result.add(map2pojo(map, clazz)); 89 | } 90 | return result; 91 | } 92 | 93 | /** 94 | * json array string convert to list with javaBean 95 | *
json字符串转换为List对象 96 | */ 97 | public static List json2list(String jsonArrayStr, Class clazz) 98 | throws Exception { 99 | List result = objectMapper.readValue(jsonArrayStr, objectMapper 100 | .getTypeFactory().constructCollectionType(List.class, clazz)); 101 | return result; 102 | } 103 | 104 | /** 105 | * map convert to javaBean 106 | *
Map对象转换为实体类对象 107 | */ 108 | public static T map2pojo(Map map, Class clazz) { 109 | return objectMapper.convertValue(map, clazz); 110 | } 111 | } -------------------------------------------------------------------------------- /wyait-common/src/main/java/com/wyait/common/utils/ValidateUtil.java: -------------------------------------------------------------------------------- 1 | package com.wyait.common.utils; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | 5 | import java.util.regex.Pattern; 6 | 7 | /** 8 | * @项目名称:wyait-manage 9 | * @包名:com.wyait-common.utils 10 | * @类描述: 11 | * @创建人:wyait 12 | * @创建时间:2018-01-08 15:36 13 | * @version:V1.0 14 | */ 15 | public class ValidateUtil { 16 | 17 | private static final Pattern P_EMAIL = Pattern 18 | .compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"); 19 | 20 | /** 21 | * 22 | * @描述:校验email格式 23 | * @创建人:wyait 24 | * @创建时间:2016年12月21日 下午1:38:45 25 | * @param str 26 | * @return 27 | */ 28 | public static boolean isEmail(String str) { 29 | return str != null && P_EMAIL.matcher(str).matches(); 30 | } 31 | 32 | private static final Pattern SIMPLE_PASSWORD = Pattern 33 | .compile("^[0-9_a-zA-Z]{6,20}$"); 34 | public static boolean isSimplePassword(String str) { 35 | return StringUtils.isNotBlank(str) && SIMPLE_PASSWORD.matcher(str).matches(); 36 | } 37 | 38 | private static final Pattern PASSWORD = Pattern 39 | .compile(""); 40 | /*private static final Pattern YOUNG_PASSWORD = Pattern 41 | .compile("");*/ 42 | private static final Pattern CENTER_PASSWORD = Pattern 43 | .compile(""); 44 | private static final Pattern STRONG_PASSWORD = Pattern 45 | .compile(""); 46 | 47 | /** 48 | * 49 | * @描述:密码校验:匹配小写字母、大写字母、数字、特殊符号的两种及两种以上【非中文】 50 | * @创建人:wyait 51 | * @创建时间:2017年1月5日15:19:17 52 | * @param str 53 | * @return 54 | */ 55 | public static boolean isPassword(String str) { 56 | return str != null && PASSWORD.matcher(str).matches(); 57 | } 58 | /*public static boolean isYoungPassword(String str) { 59 | return str != null && YOUNG_PASSWORD.matcher(str).matches(); 60 | }*/ 61 | public static boolean isCenterPassword(String str) { 62 | return str != null && CENTER_PASSWORD.matcher(str).matches(); 63 | } 64 | 65 | public static boolean isStrongPassword(String str) { 66 | return str != null && STRONG_PASSWORD.matcher(str).matches(); 67 | } 68 | 69 | private static final Pattern P_MOBILEPHONE = Pattern.compile("^1\\d{10}$"); 70 | 71 | /** 72 | * 73 | * @描述:校验是否为11位1开头手机号 74 | * @创建人:wyait 75 | * @创建时间:2016年12月21日 下午1:39:01 76 | * @param str 77 | * @return 78 | */ 79 | public static boolean isMobilephone(String str) { 80 | return StringUtils.isNotBlank(str) && P_MOBILEPHONE.matcher(str).matches(); 81 | } 82 | 83 | // 正负数字 84 | private static final Pattern P_NUMBER = Pattern.compile("^[-\\+]?[\\d]+$"); 85 | 86 | /** 87 | * 88 | * @描述:校验是否正负数字 89 | * @创建人:wyait 90 | * @创建时间:2016年12月21日 下午1:39:21 91 | * @param str 92 | * @return 93 | */ 94 | public static boolean isNumber(String str) { 95 | return StringUtils.isNotBlank(str) && P_NUMBER.matcher(str).matches(); 96 | } 97 | 98 | // QQ校验 4_12位 99 | private static final Pattern QQ = Pattern.compile("[1-9][0-9]{4,12}"); 100 | 101 | /** 102 | * 103 | * @描述:校验是否为4-12位正整数 104 | * @创建人:wyait 105 | * @创建时间:2016年12月21日 下午1:39:42 106 | * @param str 107 | * @return 108 | */ 109 | public static boolean isQQ(String str) { 110 | return StringUtils.isNotBlank(str) && QQ.matcher(str).matches(); 111 | } 112 | 113 | // 6位数验证码 114 | private static final Pattern CODE = Pattern.compile("[0-9]{6}$"); 115 | 116 | /** 117 | * 118 | * @描述:校验是否为6位数字验证码 119 | * @创建人:wyait 120 | * @创建时间:2016年12月21日 下午1:39:59 121 | * @param str 122 | * @return 123 | */ 124 | public static boolean isCode(String str) { 125 | return StringUtils.isNotBlank(str) && CODE.matcher(str).matches(); 126 | } 127 | 128 | // 图片验证码 4位随机字母和数字 129 | private static final Pattern PICCODE = Pattern.compile("\\w{4}$"); 130 | 131 | /** 132 | * 133 | * @描述:校验是否为4位随机正整数和字母 134 | * @创建人:wyait 135 | * @创建时间:2016年12月21日 下午1:40:15 136 | * @param str 137 | * @return 138 | */ 139 | public static boolean isPicCode(String str) { 140 | return str != null && PICCODE.matcher(str).matches(); 141 | } 142 | 143 | // 正负数(包含小数、整数) 144 | private static final Pattern P_DOUBLE = Pattern.compile(""); 145 | 146 | /** 147 | * 148 | * @描述:校验是否为正负数(包含小数、整数) 149 | * @创建人:wyait 150 | * @创建时间:2016年12月21日 下午1:40:53 151 | * @param str 152 | * @return 153 | */ 154 | public static boolean isDouble(String str) { 155 | return str != null && P_DOUBLE.matcher(str).matches(); 156 | } 157 | 158 | // 是否全是中文,如果有其他非中文字符,false 159 | private static final Pattern P_CHINESE = Pattern 160 | .compile("^[\u0391-\uFFE5]+$"); 161 | 162 | /** 163 | * 164 | * @描述:校验是否为中文汉字 165 | * @创建人:wyait 166 | * @创建时间:2016年12月21日 下午1:41:17 167 | * @param str 168 | * @return 169 | */ 170 | public static boolean isChinese(String str) { 171 | return str != null && P_CHINESE.matcher(str).matches(); 172 | } 173 | 174 | // 是否包含中文,包含即为true 175 | private static final Pattern P_CHINESE_A = Pattern 176 | .compile("[\u0391-\uFFE5]"); 177 | 178 | /** 179 | * 180 | * @描述:校验是否包含中文 181 | * @创建人:wyait 182 | * @创建时间:2016年12月21日 下午1:41:32 183 | * @param str 184 | * @return 185 | */ 186 | public static boolean hasChinese(String str) { 187 | return str != null && P_CHINESE_A.matcher(str).find(); 188 | } 189 | 190 | // 搜索条件各名称校验 191 | private static final Pattern NAME = Pattern 192 | .compile(""); 193 | 194 | /** 195 | * 196 | * @描述:搜索名称校验(字母、数字、汉字、下划线等符号) 197 | * @创建人:wyait 198 | * @创建时间:2016年12月21日 下午1:41:49 199 | * @param str 200 | * @return 201 | */ 202 | public static boolean isSearchName(String str) { 203 | return str != null && NAME.matcher(str).matches(); 204 | } 205 | 206 | /** 207 | * 银行卡简单校验(16或19位) 208 | */ 209 | private static final Pattern BANKCODE = Pattern 210 | .compile(""); 211 | 212 | /** 213 | * 214 | * @描述:银行卡号校验 215 | * @创建人:wyait 216 | * @创建时间:2016年12月21日 下午1:43:28 217 | * @param str 218 | * @return 219 | */ 220 | public static boolean isBankCode(String str) { 221 | return str != null && BANKCODE.matcher(str).matches(); 222 | } 223 | 224 | /** 225 | * 6位数字短信验证码 226 | */ 227 | private static final Pattern MESCODE = Pattern.compile("^[0-9]{6}$"); 228 | 229 | public static boolean isMessageCode(String str) { 230 | return str != null && MESCODE.matcher(str).matches(); 231 | } 232 | 233 | /** 234 | * 正整数、小数点后两位 金额校验 235 | */ 236 | private static final Pattern MONEY = Pattern 237 | .compile(""); 238 | 239 | /** 240 | * 241 | * @描述:正整数、小数点后两位 金额校验 242 | * @创建人:wyait 243 | * @创建时间:2016年12月21日 下午1:44:00 244 | * @param str 245 | * @return 246 | */ 247 | public static boolean isMoney(String str) { 248 | return str != null && MONEY.matcher(str).matches(); 249 | } 250 | 251 | /** 252 | * 小于等于100的正整数、小数点后两位 253 | */ 254 | private static final Pattern BAIMONEY = Pattern 255 | .compile(""); 256 | 257 | /** 258 | * 259 | * @描述:小于等于100的正整数、小数点后两位 260 | * @创建人:wyait 261 | * @创建时间:2016年12月21日 下午1:44:00 262 | * @param str 263 | * @return 264 | */ 265 | public static boolean isBaiMoney(String str) { 266 | return str != null && BAIMONEY.matcher(str).matches(); 267 | } 268 | 269 | // 备注校验 可输入:中英文、空格、中英文标点下划线。 270 | private static final Pattern REMARK = Pattern 271 | .compile(""); 272 | 273 | /** 274 | * 275 | * @描述:备注校验 可输入:中英文、空格、中英文标点下划线。 276 | * @创建人:wyait 277 | * @创建时间:2016年12月21日 下午1:44:09 278 | * @param str 279 | * @return 280 | */ 281 | public static boolean isRemark(String str) { 282 | return str != null && REMARK.matcher(str).matches(); 283 | } 284 | 285 | /** 286 | * 微信号校验 287 | */ 288 | private static final Pattern WXNUM = Pattern 289 | .compile(""); 290 | 291 | /** 292 | * 293 | * @描述:微信号校验 294 | * @创建人:wyait 295 | * @创建时间:2016年12月26日17:52:08 296 | * @param str 297 | * @return 298 | */ 299 | public static boolean isWxnum(String str) { 300 | return str != null && WXNUM.matcher(str).matches(); 301 | } 302 | 303 | /** 304 | * 用户名校验:字母、数字、中文、下划线组成2-20位【不能以下划线开头】 305 | */ 306 | private static final Pattern USERNAME = Pattern 307 | .compile(""); 308 | 309 | /** 310 | * 311 | * @描述:用户号校验 312 | * @创建人:wyait 313 | * @创建时间:2016年12月26日17:52:08 314 | * @param str 315 | * @return 316 | */ 317 | public static boolean isUsername(String str) { 318 | return str != null && USERNAME.matcher(str).matches(); 319 | } 320 | 321 | /** 322 | * 用户名校验:字母、中文、点组成2-20位 323 | */ 324 | private static final Pattern REALRNAME = Pattern 325 | .compile(""); 326 | 327 | /** 328 | * 329 | * @描述:真实姓名校验 330 | * @创建人:wyait 331 | * @创建时间:2016年12月26日17:52:08 332 | * @param str 333 | * @return 334 | */ 335 | public static boolean isRealname(String str) { 336 | return str != null && REALRNAME.matcher(str).matches(); 337 | } 338 | /** 339 | * 用户名校验:字母、中文、点组成2-20位 340 | */ 341 | private static final Pattern ADDRIP = Pattern 342 | .compile(""); 343 | 344 | public static boolean isAddrIP(String str) { 345 | return str != null && ADDRIP.matcher(str).matches(); 346 | } 347 | //只能输入两位小数的校验 348 | private static final Pattern MONEY2 = Pattern 349 | .compile(""); 350 | 351 | public static boolean isMONEY2(String str) { 352 | return str != null && MONEY2.matcher(str).matches(); 353 | } 354 | 355 | /**年份支持1000-3999 支持横线**/ 356 | private static final Pattern DATE=Pattern 357 | .compile(""); 358 | public static boolean isDate(String str){ 359 | return str != null && DATE.matcher(str).matches(); 360 | } 361 | // 测试 362 | public static void main(String[] args) { 363 | /*String str = "qwe7891"; 364 | String str10 = "qwertyuio@48911111"; 365 | String str11 = "qwertyuio!5678911110"; 366 | String str1 = "192.0.10.1"; 367 | String str2 = "192.168.10.05"; 368 | String str3 = "192.8.10.50"; 369 | String str4 = "256.168.10.255"; 370 | String str5 = "666.2.2.3"; 371 | String str6 = "255.26.0.0"; 372 | String str7 = "255.25.253.0"; 373 | String str8 = "255.26.273.23"; 374 | String str9 = "255.25.23.2";*/ 375 | String qq="0.12"; 376 | String qq2="1.12"; 377 | String qq3="0.123"; 378 | String qq4="0.1"; 379 | String qq5="1"; 380 | 381 | 382 | 383 | System.out.println(qq+" : "+isMONEY2(qq)); 384 | System.out.println(qq2+" : "+isMONEY2(qq2)); 385 | System.out.println(qq3+" : "+isMONEY2(qq3)); 386 | System.out.println(qq4+" : "+isMONEY2(qq4)); 387 | System.out.println(qq5+" : "+isMONEY2(qq5)); 388 | /*System.out.println(str + ": " + isPassword(str) + "+++长度" 389 | + str.length()); 390 | System.out.println(str10 + ": " + isPassword(str10) + "+++长度" 391 | + str10.length()); 392 | System.out.println(str11 + ": " + isPassword(str11) + "+++长度" 393 | + str11.length()); 394 | System.out.println(str1 + ": " + isAddrIP(str1)); 395 | System.out.println(str2 + ": " + isAddrIP(str2)); 396 | System.out.println(str3 + ": " + isAddrIP(str3)); 397 | System.out.println(str4 + ": " + isAddrIP(str4)); 398 | System.out.println(str5 + ": " + isAddrIP(str5)); 399 | System.out.println(str6 + ": " + isAddrIP(str6)); 400 | System.out.println(str7 + ": " + isAddrIP(str7)); 401 | System.out.println(str8 + ": " + isAddrIP(str8)); 402 | System.out.println(str9 + ": " + isAddrIP(str9));*/ 403 | // String str = "我是"; 404 | // String str1 = "王者"; 405 | // String str2 = "Sdfs abc"; 406 | // String str3 = "wyait的接口经理经历"; 407 | // String str4 = "WW abc.fdskfsk"; 408 | // String str5 = "6"; 409 | // String str6 = "s"; 410 | // String str7 = "djafd ffas.fsf"; 411 | // String str8 = "shi fou"; 412 | // String str9 = "a. AAAA"; 413 | // System.out.println(str + ": " + isRealname(str) + "+++长度" 414 | // + str.length()); 415 | // System.out.println(str1 + ": " + isRealname(str1)); 416 | // System.out.println(str2 + ": " + isRealname(str2)); 417 | // System.out.println(str3 + ": " + isRealname(str3)); 418 | // System.out.println(str4 + ": " + isRealname(str4)); 419 | // System.out.println(str5 + ": " + isRealname(str5)); 420 | // System.out.println(str6 + ": " + isRealname(str6)); 421 | // System.out.println(str7 + ": " + isRealname(str7)); 422 | // System.out.println(str8 + ": " + isRealname(str8)); 423 | // System.out.println(str9 + ": " + isRealname(str9)); 424 | // String str10 = "abcde611121234"; 425 | // if (isStrongPassword(str10)) { 426 | // System.out.println("强"); 427 | // } else if (isCenterPassword(str10)) { 428 | // System.out.println("中"); 429 | // } else if (isPassword(str10)) { 430 | // System.out.println("弱"); 431 | // } 432 | 433 | } 434 | 435 | 436 | } 437 | -------------------------------------------------------------------------------- /wyait-redis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.wyait.redis 7 | wyait-redis 8 | 0.0.1-RELEASE 9 | war 10 | 11 | wyait-redis 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.5.9.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-web 32 | 33 | 34 | spring-boot-starter-logging 35 | org.springframework.boot 36 | 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-data-redis 42 | 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-starter-log4j2 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-starter-test 51 | test 52 | 53 | 54 | 55 | 56 | wyait-redis 57 | 58 | 59 | org.springframework.boot 60 | spring-boot-maven-plugin 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /wyait-redis/src/main/java/com/wyait/redis/WyaitRedisApplication.java: -------------------------------------------------------------------------------- 1 | package com.wyait.redis; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; 7 | 8 | @SpringBootApplication 9 | //@EnableAutoConfiguration(exclude = RedisAutoConfiguration.class) 10 | public class WyaitRedisApplication { 11 | 12 | public static void main(String[] args) { 13 | SpringApplication.run(WyaitRedisApplication.class, args); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /wyait-redis/src/main/java/com/wyait/redis/config/RedisConfig.java: -------------------------------------------------------------------------------- 1 | package com.wyait.redis.config; 2 | 3 | import com.fasterxml.jackson.annotation.JsonAutoDetect; 4 | import com.fasterxml.jackson.annotation.PropertyAccessor; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.cache.Cache; 10 | import org.springframework.cache.CacheManager; 11 | import org.springframework.cache.annotation.*; 12 | import org.springframework.cache.interceptor.CacheErrorHandler; 13 | import org.springframework.cache.interceptor.KeyGenerator; 14 | import org.springframework.context.annotation.Bean; 15 | import org.springframework.context.annotation.Configuration; 16 | import org.springframework.data.redis.cache.RedisCacheManager; 17 | import org.springframework.data.redis.connection.RedisConnectionFactory; 18 | import org.springframework.data.redis.core.*; 19 | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; 20 | import org.springframework.data.redis.serializer.StringRedisSerializer; 21 | 22 | import java.lang.reflect.Field; 23 | import java.lang.reflect.Method; 24 | import java.util.*; 25 | 26 | /** 27 | * @项目名称:wyait-redis 28 | * @类名称:RedisConfig 29 | * @类描述:redis配置类 30 | * @创建人:wyait 31 | * @创建时间:2017年12月3日14:31:45 32 | * @version:1.0.0 33 | */ 34 | @Configuration @EnableCaching//(缓存支持) 35 | // //继承CachingConfigurerSupport,重写CacheManager方法。 36 | public class RedisConfig extends CachingConfigurerSupport { 37 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 38 | 39 | /** 40 | * 注入 RedisConnectionFactory 41 | */ 42 | @Autowired RedisConnectionFactory redisConnectionFactory; 43 | 44 | /** 45 | * 指定key的生成策略 46 | * @return KeyGenerator 47 | */ 48 | @Bean public KeyGenerator keyGenerator() { 49 | return new KeyGenerator() { 50 | @Override public Object generate(Object target, Method method, 51 | Object... params) { 52 | StringBuilder sb = new StringBuilder(); 53 | String[] value = new String[1]; 54 | // sb.append(target.getClass().getName()); 55 | // sb.append(":" + method.getName()); 56 | Cacheable cacheable = method.getAnnotation(Cacheable.class); 57 | if (cacheable != null) { 58 | value = cacheable.value(); 59 | } 60 | CachePut cachePut = method.getAnnotation(CachePut.class); 61 | if (cachePut != null) { 62 | value = cachePut.value(); 63 | } 64 | CacheEvict cacheEvict = method.getAnnotation(CacheEvict.class); 65 | if (cacheEvict != null) { 66 | value = cacheEvict.value(); 67 | } 68 | sb.append(value[0]); 69 | //获取参数值 70 | for (Object obj : params) { 71 | sb.append(":" + obj.toString()); 72 | } 73 | return sb.toString(); 74 | } 75 | }; 76 | } 77 | 78 | /** 79 | * 实例化 CacheManager 对象,指定使用RedisCacheManager作缓存管理 80 | * 81 | * @return CacheManager 82 | */ 83 | @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { 84 | RedisCacheManager rcm = new RedisCacheManager(redisTemplate); 85 | // 设置缓存过期时间(单位:秒),60秒 86 | rcm.setDefaultExpiration(600); 87 | return rcm; 88 | } 89 | 90 | /** 91 | * 实例化 RedisTemplate 对象 92 | * 93 | * @return RedisTemplate 94 | */ 95 | @Bean public RedisTemplate functionDomainRedisTemplate() { 96 | RedisTemplate redisTemplate = new RedisTemplate<>(); 97 | redisTemplate.setConnectionFactory(redisConnectionFactory); 98 | initDomainRedisTemplate(redisTemplate); 99 | redisTemplate.afterPropertiesSet(); 100 | return redisTemplate; 101 | } 102 | 103 | /** 104 | * 设置数据存入 redis 的序列化方式 105 | *
redisTemplate 序列化默认使用的jdkSerializeable, 存储二进制字节码, 所以自定义序列化类 106 | * 107 | * @param redisTemplate 108 | */ 109 | private void initDomainRedisTemplate( 110 | RedisTemplate redisTemplate) { 111 | //key序列化方式;(不然会出现乱码;),但是如果方法上有Long等非String类型的话,会报类型转换错误; 112 | //所以在没有自己定义key生成策略的时候,以下这个代码建议不要这么写,可以不配置或者自己实现ObjectRedisSerializer 113 | //或者JdkSerializationRedisSerializer序列化方式; 114 | 115 | // 使用Jackson2JsonRedisSerialize 替换默认序列化 116 | Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer( 117 | Object.class); 118 | ObjectMapper om = new ObjectMapper(); 119 | om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 120 | om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 121 | jackson2JsonRedisSerializer.setObjectMapper(om); 122 | //// string结构的数据,设置value的序列化规则和 key的序列化规则 123 | //StringRedisSerializer解决key中午乱码问题。//Long类型不可以会出现异常信息; 124 | redisTemplate.setKeySerializer(new StringRedisSerializer()); 125 | //value乱码问题:Jackson2JsonRedisSerializer 126 | redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); 127 | 128 | //设置Hash结构的key和value的序列化方式 129 | //redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer); 130 | //redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); 131 | } 132 | 133 | /** 134 | * redis数据操作异常处理 135 | * 这里的处理:在日志中打印出错误信息,但是放行 136 | * 保证redis服务器出现连接等问题的时候不影响程序的正常运行,使得能够出问题时不用缓存 137 | * @return 138 | */ 139 | @Bean @Override public CacheErrorHandler errorHandler() { 140 | CacheErrorHandler cacheErrorHandler = new CacheErrorHandler() { 141 | @Override public void handleCacheGetError(RuntimeException e, 142 | Cache cache, Object key) { 143 | logger.error("redis异常:key=[{}]", key, e); 144 | } 145 | 146 | @Override public void handleCachePutError(RuntimeException e, 147 | Cache cache, Object key, Object value) { 148 | logger.error("redis异常:key=[{}]", key, e); 149 | } 150 | 151 | @Override public void handleCacheEvictError(RuntimeException e, 152 | Cache cache, Object key) { 153 | logger.error("redis异常:key=[{}]", key, e); 154 | } 155 | 156 | @Override public void handleCacheClearError(RuntimeException e, 157 | Cache cache) { 158 | logger.error("redis异常:", e); 159 | } 160 | }; 161 | return cacheErrorHandler; 162 | } 163 | 164 | /** 165 | * 实例化 ValueOperations 对象,可以使用 String 操作 166 | * 167 | * @param redisTemplate 168 | * @return 169 | */ 170 | @Bean public ValueOperations valueOperations( 171 | RedisTemplate redisTemplate) { 172 | return redisTemplate.opsForValue(); 173 | } 174 | 175 | /** 176 | * 实例化 HashOperations 对象,可以使用 Hash 类型操作 177 | * 178 | * @param redisTemplate 179 | * @return 180 | */ 181 | @Bean public HashOperations hashOperations( 182 | RedisTemplate redisTemplate) { 183 | return redisTemplate.opsForHash(); 184 | } 185 | 186 | /** 187 | * 实例化 ListOperations 对象,可以使用 List 操作 188 | * 189 | * @param redisTemplate 190 | * @return 191 | */ 192 | @Bean public ListOperations listOperations( 193 | RedisTemplate redisTemplate) { 194 | return redisTemplate.opsForList(); 195 | } 196 | 197 | /** 198 | * 实例化 SetOperations 对象,可以使用 Set 操作 199 | * 200 | * @param redisTemplate 201 | * @return 202 | */ 203 | @Bean public SetOperations setOperations( 204 | RedisTemplate redisTemplate) { 205 | return redisTemplate.opsForSet(); 206 | } 207 | 208 | /** 209 | * 实例化 ZSetOperations 对象,可以使用 ZSet 操作 210 | * 211 | * @param redisTemplate 212 | * @return 213 | */ 214 | @Bean public ZSetOperations zSetOperations( 215 | RedisTemplate redisTemplate) { 216 | return redisTemplate.opsForZSet(); 217 | } 218 | 219 | } 220 | -------------------------------------------------------------------------------- /wyait-redis/src/main/java/com/wyait/redis/controller/RedisCacheController.java: -------------------------------------------------------------------------------- 1 | package com.wyait.redis.controller; 2 | 3 | import com.wyait.redis.pojo.Cat; 4 | import com.wyait.redis.service.CatService; 5 | import com.wyait.redis.utils.RedisUtils; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.cache.annotation.CacheEvict; 8 | import org.springframework.cache.annotation.CachePut; 9 | import org.springframework.cache.annotation.Cacheable; 10 | import org.springframework.stereotype.Controller; 11 | import org.springframework.web.bind.annotation.*; 12 | 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | 16 | /** 17 | * @项目名称:wyait-manage 18 | * @包名:com.wyait.redis.controller 19 | * @类描述: 20 | * @创建人:wyait 21 | * @创建时间:2017-12-07 11:25 22 | * @version:V1.0 23 | */ 24 | @Controller 25 | @RequestMapping("redis") 26 | public class RedisCacheController { 27 | @Autowired 28 | private RedisUtils redisUtils; 29 | @Autowired 30 | private CatService catService; 31 | 32 | /** 33 | * 更新redis中的缓存数据 34 | * @param id 主键 35 | */ 36 | @CachePut(value = "catCache", key = "#root.caches[0].name + ':' + #id") 37 | @RequestMapping(value = "/addCat", method = RequestMethod.POST) 38 | @ResponseBody 39 | public Cat insert(@RequestParam("id") int id){ 40 | System.out.println("==========insert添加数据到redis中,请求参数:"+id); 41 | Cat cat=new Cat(); 42 | cat.setId(id); 43 | cat.setName("张三"); 44 | cat.setMobile("120"); 45 | cat.setAge(52); 46 | return cat; 47 | } 48 | 49 | /** 50 | * 通过缓存注解,添加数据到redis中 51 | *
实现数据缓存! 52 | * @param catId 对象 53 | */ 54 | @Cacheable(value = "catCache") 55 | @RequestMapping(value = "/getCat/{id}", method = RequestMethod.GET) 56 | @ResponseBody 57 | public Cat get(@PathVariable("id") int catId){ 58 | System.out.println("================getCat================="); 59 | return this.catService.getCat(catId); 60 | } 61 | 62 | /** 63 | * 更新redis中的缓存数据 64 | *
#root. 是spEL表达式 65 | *
如果参数是个对象,就通过“#对象.变量”获取到对应的key中需要的值;比如:#cat.id 66 | * @param id 主键 67 | */ 68 | @CachePut(value = "catCache", key = "#root.caches[0].name + ':' + #id") 69 | @RequestMapping(value = "/updateCat", method = RequestMethod.POST) 70 | @ResponseBody 71 | public Cat update(@RequestParam int id){ 72 | System.out.println("==========请求参数:"+id); 73 | return this.catService.updateCat(id); 74 | } 75 | 76 | /** 77 | * 删除redis中对应key的数据 78 | * @param id 79 | */ 80 | @CacheEvict(value="catCache") 81 | @RequestMapping(value="/deleteCat",method = RequestMethod.POST) 82 | public void deleteCat(@RequestParam("id") int id){ 83 | System.out.println("==============删除redis中数据====catId===="+id); 84 | this.catService.deleteCat(id); 85 | } 86 | 87 | 88 | /** 89 | * 添加数据到redis中 90 | */ 91 | @RequestMapping(value = "/addRedis", method = RequestMethod.GET) 92 | public void add(){ 93 | System.out.println("================addRedis================="); 94 | this.redisUtils.hmSet("a","test:aaa","123456"); 95 | Cat cat=new Cat(); 96 | cat.setName("wyait"); 97 | cat.setAge(5); 98 | cat.setMobile("1112222333354"); 99 | this.redisUtils.add("cat:1",cat); 100 | this.redisUtils.set("catSet:1",cat); 101 | this.redisUtils.zAdd("catZset:1",cat,100); 102 | List list=new ArrayList(); 103 | list.add(cat); 104 | this.redisUtils.lPush("catList:1",list); 105 | 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /wyait-redis/src/main/java/com/wyait/redis/pojo/Cat.java: -------------------------------------------------------------------------------- 1 | package com.wyait.redis.pojo; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * @项目名称:wyait-manage 7 | * @包名:com.wyait.redis.pojo 8 | * @类描述: 9 | * @创建人:wyait 10 | * @创建时间:2017-12-06 17:54 11 | * @version:V1.0 12 | */ 13 | public class Cat implements Serializable { 14 | 15 | private static final long serialVersionUID = 4114566985567376061L; 16 | 17 | private int id; 18 | 19 | private String name; 20 | 21 | private int age; 22 | 23 | private String mobile; 24 | 25 | public int getId() { 26 | return id; 27 | } 28 | 29 | public void setId(int id) { 30 | this.id = id; 31 | } 32 | 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | public int getAge() { 38 | return age; 39 | } 40 | 41 | public String getMobile() { 42 | return mobile; 43 | } 44 | 45 | public void setName(String name) { 46 | this.name = name; 47 | } 48 | 49 | public void setAge(int age) { 50 | this.age = age; 51 | } 52 | 53 | public void setMobile(String mobile) { 54 | this.mobile = mobile; 55 | } 56 | 57 | @Override public String toString() { 58 | return "Cat{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age 59 | + ", mobile='" + mobile + '\'' + '}'; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /wyait-redis/src/main/java/com/wyait/redis/service/CatService.java: -------------------------------------------------------------------------------- 1 | package com.wyait.redis.service; 2 | 3 | import com.wyait.redis.pojo.Cat; 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | * @项目名称:wyait-manage 8 | * @包名:com.wyait.redis.service 9 | * @类描述: 10 | * @创建人:wyait 11 | * @创建时间:2017-12-07 13:59 12 | * @version:V1.0 13 | */ 14 | @Service public class CatService { 15 | public Cat getCat(int catId) { 16 | Cat cat = new Cat(); 17 | if (catId > 0) { 18 | if (catId == 1) { 19 | cat.setId(1); 20 | cat.setAge(1); 21 | cat.setMobile("11123456666"); 22 | cat.setName("tomcat"); 23 | } else if (catId == 2) { 24 | cat.setId(2); 25 | cat.setAge(22); 26 | cat.setMobile("10000000006"); 27 | cat.setName("triger"); 28 | } 29 | } 30 | return cat; 31 | 32 | } 33 | 34 | public Cat updateCat(int id) { 35 | Cat cat = new Cat(); 36 | cat.setId(id); 37 | cat.setAge(100); 38 | cat.setMobile("120"); 39 | cat.setName("老胡"); 40 | return cat; 41 | } 42 | 43 | public void deleteCat(int id) { 44 | //TODO 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /wyait-redis/src/main/java/com/wyait/redis/utils/RedisUtils.java: -------------------------------------------------------------------------------- 1 | package com.wyait.redis.utils; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import java.util.Set; 6 | import java.util.concurrent.TimeUnit; 7 | 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.data.redis.core.HashOperations; 11 | import org.springframework.data.redis.core.ListOperations; 12 | import org.springframework.data.redis.core.RedisTemplate; 13 | import org.springframework.data.redis.core.SetOperations; 14 | import org.springframework.data.redis.core.ValueOperations; 15 | import org.springframework.data.redis.core.ZSetOperations; 16 | import org.springframework.stereotype.Component; 17 | import org.springframework.stereotype.Service; 18 | 19 | /** 20 | * @项目名称:wyait-redis 21 | * @类名称:RedisService 22 | * @类描述:redis配置类 23 | * @创建人:wyait 24 | * @创建时间:2017年12月3日14:31:45 25 | * @version:1.0.0 26 | */ 27 | @Component 28 | public class RedisUtils { 29 | @Autowired 30 | private RedisTemplate redisTemplate; 31 | 32 | /** 33 | * 添加String结构的数据到redis中 34 | * @param key 键:唯一标识,从redis中取值的时候用到。 35 | * @param value 值:保存的具体数据 36 | * @return true 成功; false 失败 37 | */ 38 | public boolean set(final String key, Object value) { 39 | boolean result = false; 40 | try { 41 | ValueOperations operations = redisTemplate 42 | .opsForValue(); 43 | operations.set(key, value); 44 | result = true; 45 | } catch (Exception e) { 46 | e.printStackTrace(); 47 | } 48 | return result; 49 | } 50 | 51 | /** 52 | * 添加String结构的数据到redis中,并指定保存数据的时间 53 | * @param key 键:唯一标识,从redis中取值的时候用到。 54 | * @param value 值:保存的具体数据 55 | * @param expireTime 过期时间:redis保存该数据的时间,超过该时间,将删除数据 56 | * @return true 成功; false 失败 57 | */ 58 | public boolean set(final String key, Object value, Long expireTime) { 59 | boolean result = false; 60 | try { 61 | ValueOperations operations = redisTemplate 62 | .opsForValue(); 63 | operations.set(key, value); 64 | redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); 65 | result = true; 66 | } catch (Exception e) { 67 | e.printStackTrace(); 68 | } 69 | return result; 70 | } 71 | 72 | /** 73 | * 根据keys,批量删除对应的key的数据 74 | * @param keys 键:(可变参数) 75 | */ 76 | public void remove(final String... keys) { 77 | for (String key : keys) { 78 | remove(key); 79 | } 80 | } 81 | 82 | /** 83 | * 模糊批量删除数据 84 | * @param pattern key的匹配规则,通配符:*:0到任意多个字 ;?:1个字符 85 | */ 86 | public void removePattern(final String pattern) { 87 | Set keys = redisTemplate.keys(pattern); 88 | if (keys.size() > 0) 89 | redisTemplate.delete(keys); 90 | } 91 | 92 | /** 93 | * 删除指定key的数据 94 | * @param key 键 95 | */ 96 | public void remove(final String key) { 97 | if (exists(key)) { 98 | redisTemplate.delete(key); 99 | } 100 | } 101 | 102 | /** 103 | * 判断redis中是否有指定key存在 104 | * @param key 105 | * @return true 有;false 没有 106 | */ 107 | public boolean exists(final String key) { 108 | return redisTemplate.hasKey(key); 109 | } 110 | 111 | /** 112 | * 根据指定的key获取value值 113 | * @param key 键 114 | * @return value 数据值 115 | */ 116 | public Object get(final String key) { 117 | Object result = null; 118 | ValueOperations operations = redisTemplate 119 | .opsForValue(); 120 | result = operations.get(key); 121 | return result; 122 | } 123 | 124 | /** 125 | * 添加哈希Hash结构的数据到redis中 126 | * @param key 键 127 | * @param hashKey 哈希key 128 | * @param value 数据值 129 | */ 130 | public void hmSet(String key, Object hashKey, Object value) { 131 | HashOperations hash = redisTemplate 132 | .opsForHash(); 133 | hash.put(key, hashKey, value); 134 | } 135 | 136 | /** 137 | * 通过key和哈希key获取数据 138 | * @param key 键 139 | * @param hashKey 哈希key 140 | * @return value 数据值 141 | */ 142 | public Object hmGet(String key, Object hashKey) { 143 | HashOperations hash = redisTemplate 144 | .opsForHash(); 145 | return hash.get(key, hashKey); 146 | } 147 | 148 | /** 149 | * 添加列表List数据到redis中 150 | * @param key 键 151 | * @param value 值 152 | */ 153 | public void lPush(String key, Object value) { 154 | ListOperations list = redisTemplate.opsForList(); 155 | list.rightPush(key, value); 156 | } 157 | 158 | /** 159 | * 根据key和索引获取部分列表List数据 160 | * @param key 键 161 | * @param start 开始索引 162 | * @param end 结束索引 163 | * @return value 值 164 | */ 165 | public List lRange(String key, long start, long end) { 166 | ListOperations list = redisTemplate.opsForList(); 167 | return list.range(key, start, end); 168 | } 169 | 170 | /** 171 | * 添加集合Set数据到redis中 172 | * @param key 键 173 | * @param value 值 174 | */ 175 | public void add(String key, Object value) { 176 | SetOperations set = redisTemplate.opsForSet(); 177 | set.add(key, value); 178 | } 179 | 180 | /** 181 | * 根据键key获取集合Set 182 | * @param key 键 183 | * @return value 值:Set 184 | */ 185 | public Set setMembers(String key) { 186 | SetOperations set = redisTemplate.opsForSet(); 187 | return set.members(key); 188 | } 189 | 190 | /** 191 | * 添加ZSet有序集合到redis中 192 | * @param key 键 193 | * @param value 值 194 | * @param scoure 长度? 195 | */ 196 | public void zAdd(String key, Object value, double scoure) { 197 | ZSetOperations zset = redisTemplate.opsForZSet(); 198 | zset.add(key, value, scoure); 199 | } 200 | 201 | /** 202 | * 根据key和索引获取部分有序集合ZSet数据 203 | * @param key 键 204 | * @param scoure 开始索引 205 | * @param scoure1 结束索引 206 | * @return value 值 207 | */ 208 | public Set rangeByScore(String key, double scoure, double scoure1) { 209 | ZSetOperations zset = redisTemplate.opsForZSet(); 210 | return zset.rangeByScore(key, scoure, scoure1); 211 | } 212 | } -------------------------------------------------------------------------------- /wyait-redis/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8066 2 | # redis 3 | ######################################################## 4 | ###REDIS (RedisProperties) redis基本配置 5 | ######################################################## 6 | # Redis数据库索引(默认为0) 7 | spring.redis.database=0 8 | # Redis服务器IP 9 | spring.redis.host=127.0.0.1 10 | # Redis密码(默认为空) 11 | spring.redis.password= 12 | # Redis端口号 13 | spring.redis.port=6379 14 | # 连接超时时间 单位 ms(毫秒) 15 | spring.redis.timeout=3000 16 | 17 | ######################################################## 18 | ###REDIS (RedisProperties) redis线程池设置 19 | ######################################################## 20 | # 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。 21 | spring.redis.pool.max-idle=8 22 | # 控制一个pool最少有多少个状态为idle(空闲的)的jedis实例,默认值也是0。 23 | spring.redis.pool.min-idle=2 24 | # 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。 25 | spring.redis.pool.max-active=20 26 | # 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException 27 | spring.redis.pool.max-wait=3000 28 | 29 | #################################################################################### 30 | ###REDIS (RedisProperties) redis哨兵设置,和上面的host和port不要同时配置 31 | #################################################################################### 32 | # Redis服务器master的名字 33 | #spring.redis.sentinel.master=master8026 34 | # redis-sentinel的配置地址和端口(注意:不是redis的地址和端口) 35 | #spring.redis.sentinel.nodes=10.189.80.25:26379,10.189.80.26:26379,10.189.80.26:26378 36 | ######################################################## 37 | ###REDIS (RedisProperties) redis 自定义参数 38 | ######################################################## 39 | #默认生命周期30天(单位:s秒) 40 | spring.redis.defaultExpiration = 2592000 41 | #服务器上下文路径 42 | spring.redis.contextPath = contextPath -------------------------------------------------------------------------------- /wyait-redis/src/test/java/com/wyait/redis/WyaitRedisApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.wyait.redis; 2 | 3 | import com.wyait.redis.utils.RedisUtils; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.junit4.SpringRunner; 9 | 10 | @RunWith(SpringRunner.class) 11 | //@RunWith(SpringJUnit4ClassRunner.class) 12 | @SpringBootTest(classes = WyaitRedisApplication.class) 13 | //@SpringBootTest 14 | public class WyaitRedisApplicationTests { 15 | @Autowired 16 | private RedisUtils redisUtils; 17 | @Test 18 | public void contextLoads() { 19 | System.out.println("==================================== test ========="); 20 | redisUtils.set("test","aaa"); 21 | System.out.println("==================================== end ========="); 22 | } 23 | 24 | } 25 | --------------------------------------------------------------------------------