deason 4 years ago
parent
commit
bea6663174

+ 4 - 0
pom.xml

@@ -28,6 +28,10 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-jpa</artifactId>

+ 2 - 2
src/main/java/cn/com/qmth/examcloud/tool/ExamCloudToolApplication.java → src/main/java/cn/com/qmth/examcloud/tool/OeToolApplication.java

@@ -6,10 +6,10 @@ import org.springframework.scheduling.annotation.EnableAsync;
 
 @EnableAsync
 @SpringBootApplication
-public class ExamCloudToolApplication {
+public class OeToolApplication {
 
     public static void main(String[] args) {
-        SpringApplication.run(ExamCloudToolApplication.class, args);
+        SpringApplication.run(OeToolApplication.class, args);
     }
 
 }

+ 49 - 0
src/main/java/cn/com/qmth/examcloud/tool/config/RedisClient.java

@@ -0,0 +1,49 @@
+package cn.com.qmth.examcloud.tool.config;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Component;
+
+import java.util.concurrent.TimeUnit;
+
+@Component
+public class RedisClient {
+
+    @Autowired
+    private RedisTemplate<String, Object> redisTemplate;
+
+    public void set(String key, Object value) {
+        redisTemplate.opsForValue().set(key, value);
+    }
+
+    public void set(String key, Object value, long timeout) {
+        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
+    }
+
+    public void setForHash(String key, String hashKey, Object value, long timeout) {
+        redisTemplate.opsForHash().put(key, hashKey, value);
+        this.expire(key, timeout);
+    }
+
+    public <T> T get(String key) {
+        return (T) redisTemplate.opsForValue().get(key);
+    }
+
+    public <T> T getForHash(String key, String hashKey) {
+        return (T) redisTemplate.opsForHash().get(key, hashKey);
+    }
+
+    public void delete(String key) {
+        this.expire(key, 0);
+        // redisTemplate.delete(key);
+    }
+
+    public void deleteForHash(String key, String hashKey) {
+        redisTemplate.opsForHash().delete(key, hashKey);
+    }
+
+    public void expire(String key, long timeout) {
+        redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
+    }
+
+}

+ 40 - 0
src/main/java/cn/com/qmth/examcloud/tool/config/RedisConfig.java

@@ -0,0 +1,40 @@
+package cn.com.qmth.examcloud.tool.config;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+@Configuration
+public class RedisConfig {
+    private final static Logger log = LoggerFactory.getLogger(RedisConfig.class);
+
+    @Bean
+    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
+        log.info("RedisTemplate<String, Object> init... ");
+
+        ObjectMapper objectMapper = new ObjectMapper();
+        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
+        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
+        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
+
+        Jackson2JsonRedisSerializer<?> jacksonSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
+        jacksonSerializer.setObjectMapper(objectMapper);
+
+        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
+        redisTemplate.setValueSerializer(jacksonSerializer);
+        redisTemplate.setKeySerializer(new StringRedisSerializer());
+        redisTemplate.setConnectionFactory(redisConnectionFactory);
+        redisTemplate.afterPropertiesSet();
+        return redisTemplate;
+    }
+
+}

+ 20 - 0
src/main/java/cn/com/qmth/examcloud/tool/controller/IndexController.java

@@ -0,0 +1,20 @@
+package cn.com.qmth.examcloud.tool.controller;
+
+import cn.com.qmth.examcloud.tool.config.RedisClient;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class IndexController {
+
+    @Autowired
+    private RedisClient redisClient;
+
+    @GetMapping(value = "/")
+    public String index() {
+        String ddd = redisClient.get("$_SYS_PROP:oe.testDev.order");
+        return "ok";
+    }
+
+}

+ 2 - 0
src/main/java/cn/com/qmth/examcloud/tool/utils/JsonMapper.java

@@ -68,6 +68,8 @@ public class JsonMapper {
 
         //设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
         mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
+        //忽略无法转换的对象
+        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
 
         //设置默认日期格式
         mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

+ 5 - 0
src/main/resources/application.properties

@@ -12,6 +12,11 @@ spring.datasource.password=exam_cloud_dev
 spring.datasource.url=jdbc:mysql://${dsurl.host}:${dsurl.port}/${dsurl.database}?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2b8&rewriteBatchedStatements=true
 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
 spring.jpa.hibernate.ddl-auto=validate
+# redis config
+spring.redis.host=192.168.10.30
+spring.redis.port=6379
+spring.redis.password=examcloud
+spring.redis.database=0
 # logging config
 logging.level.org.springframework=WARN
 logging.level.org.hibernate=WARN

+ 3 - 3
src/test/java/cn/com/qmth/examcloud/tool/test/DemoTest.java → src/test/java/cn/com/qmth/examcloud/tool/test/OeToolTest.java

@@ -7,11 +7,11 @@ import org.springframework.test.context.junit4.SpringRunner;
 
 @SpringBootTest
 @RunWith(SpringRunner.class)
-public class DemoTest {
+public class OeToolTest {
 
     @Test
-    public void tempTest() {
+    public void demoTest() {
 
     }
 
-}
+}