当前位置:   article > 正文

springboot 整合redis_illegal char <:> at index 9: classpath:apiclient_k

illegal char <:> at index 9: classpath:apiclient_key.pem

 项目目录:

application.yml

  1. spring:
  2. datasource:
  3. url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
  4. username: root
  5. password: root
  6. driver-class-name: com.mysql.jdbc.Driver
  7. test-while-idle: true
  8. test-on-borrow: true
  9. validation-query: SELECT 1 FROM DUAL
  10. time-between-eviction-runs-millis: 300000
  11. min-evictable-idle-time-millis: 1800000
  12. freemarker:
  13. charset: UTF-8
  14. cache: false
  15. expose-request-attributes: false
  16. request-context-attribute: request
  17. expose-session-attributes: false
  18. content-type: text/html
  19. template-loader-path: classpath:/templates
  20. suffix: .ftl
  21. check-template-location: true
  22. redis:
  23. database: 1
  24. host: 127.0.0.1
  25. port: 6379
  26. jedis:
  27. pool:
  28. max-active: 8
  29. max-wait: -1
  30. max-idle: 8
  31. min-idle: 0
  32. timeout: 10000

 pom:

  1. <dependencies>
  2. <!-- sprinboot web -->
  3. <dependency>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-web</artifactId>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.mybatis.spring.boot</groupId>
  9. <artifactId>mybatis-spring-boot-starter</artifactId>
  10. <version>1.1.1</version>
  11. </dependency>
  12. <!-- mysql 依赖 -->
  13. <dependency>
  14. <groupId>mysql</groupId>
  15. <artifactId>mysql-connector-java</artifactId>
  16. </dependency>
  17. <!-- 引入freemarker包 -->
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-freemarker</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-data-redis</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>commons-lang</groupId>
  28. <artifactId>commons-lang</artifactId>
  29. <version>2.6</version>
  30. </dependency>
  31. <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
  32. <dependency>
  33. <groupId>redis.clients</groupId>
  34. <artifactId>jedis</artifactId>
  35. <version>3.0.1</version>
  36. </dependency>
  37. </dependencies>
MybatisRedisCache类:
  1. import com.mayikt.api.utils.SerializeUtil;
  2. import org.apache.ibatis.cache.Cache;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import redis.clients.jedis.Jedis;
  6. import redis.clients.jedis.JedisPool;
  7. import java.util.concurrent.locks.ReadWriteLock;
  8. import java.util.concurrent.locks.ReentrantReadWriteLock;
  9. /**
  10. * mybatis二级缓存整合Redis
  11. */
  12. public class MybatisRedisCache implements Cache {
  13. private static Logger logger = LoggerFactory.getLogger(MybatisRedisCache.class);
  14. private Jedis redisClient = createReids();
  15. private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
  16. private String id;
  17. public MybatisRedisCache(final String id) {
  18. if (id == null) {
  19. throw new IllegalArgumentException("Cache instances require an ID");
  20. }
  21. logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>MybatisRedisCache:id=" + id);
  22. this.id = id;
  23. }
  24. public String getId() {
  25. return this.id;
  26. }
  27. public int getSize() {
  28. return Integer.valueOf(redisClient.dbSize().toString());
  29. }
  30. public void putObject(Object key, Object value) {
  31. logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>putObject:" + key + "=" + value);
  32. redisClient.set(SerializeUtil.serialize(key.toString()), SerializeUtil.serialize(value));
  33. }
  34. public Object getObject(Object key) {
  35. Object value = SerializeUtil.unserialize(redisClient.get(SerializeUtil.serialize(key.toString())));
  36. logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>getObject:" + key + "=" + value);
  37. return value;
  38. }
  39. public Object removeObject(Object key) {
  40. return redisClient.expire(SerializeUtil.serialize(key.toString()), 0);
  41. }
  42. public void clear() {
  43. redisClient.flushDB();
  44. }
  45. public ReadWriteLock getReadWriteLock() {
  46. return readWriteLock;
  47. }
  48. protected static Jedis createReids() {
  49. JedisPool pool = new JedisPool("127.0.0.1", 6379);
  50. return pool.getResource();
  51. }
  52. }

 entity:

  1. public class OrderEntity implements Serializable {
  2. private int id;
  3. private String orderName;
  4. private String orderDes;
  5. /**
  6. * @return the id
  7. */
  8. public int getId() {
  9. return id;
  10. }
  11. /**
  12. * @param id
  13. * the id to set
  14. */
  15. public void setId(int id) {
  16. this.id = id;
  17. }
  18. /**
  19. * @return the orderName
  20. */
  21. public String getOrderName() {
  22. return orderName;
  23. }
  24. /**
  25. * @param orderName
  26. * the orderName to set
  27. */
  28. public void setOrderName(String orderName) {
  29. this.orderName = orderName;
  30. }
  31. /**
  32. * @return the orderDes
  33. */
  34. public String getOrderDes() {
  35. return orderDes;
  36. }
  37. /**
  38. * @param orderDes
  39. * the orderDes to set
  40. */
  41. public void setOrderDes(String orderDes) {
  42. this.orderDes = orderDes;
  43. }
  44. }

mapper:

  1. import com.mayikt.api.cache.MybatisRedisCache;
  2. import com.mayikt.api.entity.OrderEntity;
  3. import org.apache.ibatis.annotations.CacheNamespace;
  4. import org.apache.ibatis.annotations.Insert;
  5. import org.apache.ibatis.annotations.Select;
  6. import java.util.List;
  7. @CacheNamespace(implementation = MybatisRedisCache.class)
  8. public interface OrderMapper {
  9. @Insert("insert order_info values (null,#{orderName},#{orderDes})")
  10. public int addOrder(OrderEntity OrderEntity);
  11. @Select("SELECT * FROM order_info;")
  12. public List<OrderEntity> findByOrder();
  13. }
RedisToken:
  1. import org.apache.commons.lang.StringUtils;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.data.redis.core.StringRedisTemplate;
  4. import org.springframework.stereotype.Component;
  5. import java.util.UUID;
  6. import java.util.concurrent.TimeUnit;
  7. @Component
  8. public class RedisToken {
  9. @Autowired
  10. private StringRedisTemplate stringRedisTemplate;
  11. /**
  12. * 获取Token
  13. *
  14. * @return
  15. */
  16. public String getToken() {
  17. //1. 使用uuid生成Token
  18. String token = UUID.randomUUID().toString().replace("-", "");
  19. //2. 将Token存放到Redis中
  20. setString(token, token, 7200l);
  21. return token;
  22. }
  23. public Boolean findByToken(String token) {
  24. if (StringUtils.isEmpty(token)) {
  25. return false;
  26. }
  27. String redisToken = getString(token);
  28. if(StringUtils.isEmpty(redisToken)){
  29. return false;
  30. }
  31. delKey(redisToken);
  32. return true;
  33. }
  34. private void setString(String key, Object data, Long timeout) {
  35. if (data instanceof String) {
  36. String value = (String) data;
  37. stringRedisTemplate.opsForValue().set(key, value);
  38. }
  39. if (timeout != null) {
  40. stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
  41. }
  42. }
  43. private String getString(String key) {
  44. return stringRedisTemplate.opsForValue().get(key);
  45. }
  46. private void delKey(String key) {
  47. stringRedisTemplate.delete(key);
  48. }
  49. }

Conroller:

  1. @Controller
  2. public class ApiOrderService {
  3. @Autowired
  4. private OrderMapper orderMapper;
  5. @Autowired
  6. private RedisToken redisToken;
  7. // @RequestMapping("/addOrder")
  8. // public String addOrder(OrderEntity orderEntity) {
  9. // int reuslt = orderMapper.addOrder(orderEntity);
  10. // return reuslt > 0 ? "success" : "fail";
  11. // }
  12. @RequestMapping("/")
  13. public String index(HttpServletRequest request) {
  14. // 1.页面中存放该Token
  15. String token = redisToken.getToken();
  16. request.setAttribute("token", token);
  17. return "index";
  18. }
  19. @RequestMapping("/addOrder")
  20. public String addOrder(OrderEntity orderEntity, String token) {
  21. Boolean isToken = redisToken.findByToken(token);
  22. if (!isToken) {
  23. return "repeat";
  24. }
  25. int result = orderMapper.addOrder(orderEntity);
  26. return result > 0 ? "success" : "fail";
  27. }
  28. @RequestMapping("/getOrderList")
  29. @ResponseBody
  30. public Object getOrderList() {
  31. return orderMapper.findByOrder();
  32. }
  33. }
SerializeUtil:
  1. import java.io.ByteArrayInputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.ObjectInputStream;
  4. import java.io.ObjectOutputStream;
  5. public class SerializeUtil {
  6. public static byte[] serialize(Object object) {
  7. ObjectOutputStream oos = null;
  8. ByteArrayOutputStream baos = null;
  9. try {
  10. // 序列化
  11. baos = new ByteArrayOutputStream();
  12. oos = new ObjectOutputStream(baos);
  13. oos.writeObject(object);
  14. byte[] bytes = baos.toByteArray();
  15. return bytes;
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. return null;
  20. }
  21. public static Object unserialize(byte[] bytes) {
  22. ByteArrayInputStream bais = null;
  23. try {
  24. // 反序列化
  25. bais = new ByteArrayInputStream(bytes);
  26. ObjectInputStream ois = new ObjectInputStream(bais);
  27. return ois.readObject();
  28. } catch (Exception e) {
  29. }
  30. return null;
  31. }
  32. }

认准蚂蚁课堂:http://www.mayikt.com/ 很多免费有用干货

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/190240
推荐阅读
相关标签
  

闽ICP备14008679号