1. Create a new spring project and check the necessary component web DataJdbc mysql redis
2. Download redis, configure config file
Comment bind 127, allow all ip access
Protected-mode yes change to no to cancel the protection mechanism and allow external network access
daemonize no change yes background operation
Uncomment requirepass to set the password
Run redis-server.exe [configuration file path]
3. Configuration file properties, write MVC three layers
# configure data source spring.datasource.url=jdbc:mysql://localhost:3306/bjpowernode?serverTimezone=UTC &useSSL=false &characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # Configure MyBatis mybatis.mapper-locations=classpath:mapper/*.xml mybatis.configuration.map-underscore-to-camel-case=true #redis spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=123456 spring.redis.timeout=600000 #Timeout
Write redis configuration class
@Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } }
Write a three-tier architecture, postman to test the database
@RestController @RequestMapping("test") public class T { @Autowired EmpService empService; @GetMapping("t1") @ResponseBody public List<Emp> t1(){ List <Emp> e=empService.getemp(); return e; } }
@Service public interface EmpService { List<Emp> getemp(); }
@Service public class EmpServiceImpl implements EmpService { @Autowired EmpDAO empDAO; @Autowired RedisTemplate redisTemplate; @Override public List<Emp> getemp() { List<Emp> emps= (List<Emp>) redisTemplate.opsForValue().get("getemp"); if (emps!=null){ System.out.println("Have read the cache~~~~~~~~~"); return emps; } System.out.println("Expired no read cache~~~"); List<Emp> getemp = empDAO.getemp(); redisTemplate.opsForValue().set("getemp",getemp,10, TimeUnit.SECONDS);//Manually set the expiration time return get temp; } }
@Mapper //You can use @MapperScan("scan dao/mapper path") on the startup class public interface EmpDAO { List<Emp> getemp(); }
XML
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.demo2.dao.EmpDAO"> <select id="getemp" resultType="com.demo2.pojo.Emp"> SELECT * FROM emp </select> </mapper>
4. Additional: redis tools (optional) copied online
/** * @author yu_xuanyuan * @date 2022/9/23 10:16 */ @Component public class RedisUtil { @Autowired RedisTemplate<String, Object> redisTemplate; /** *setex * @param key key * @param value value * @param time expiration time */ public void setex(String key,Object value,long time){ redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } /** * set * String type set, no expiration time * @param key key * @param value value */ public void set(String key, Object value){ redisTemplate.opsForValue().set(key,value); } /** * Set key and value in batches * @param map key and value set */ public void mset(Map<String,Object> map){ redisTemplate.opsForValue().multiSet(map); } /** * Set if key does not exist * @param key key * @param value value * @return return success */ public Boolean setnx(String key, Object value){ return redisTemplate.opsForValue().setIfAbsent(key, value); } /** * Batch insert key, if key does not exist * @param map key and value set * @return success */ public Boolean msetnx(Map<String,Object> map){ return redisTemplate.opsForValue().multiSetIfAbsent(map); } /** * String type get * @param key key * @return returns the object corresponding to value */ public Object get(String key){ return redisTemplate.opsForValue().get(key); } /** * Delete the corresponding key * @param key key * @return Returns whether the deletion is successful */ public Boolean del(String key){ return redisTemplate.delete(key); } /** * Batch delete keys * @param keys collection of keys * @return Return the number of successful deletions */ public Long del(List<String> keys){ return redisTemplate.delete(keys); } /** * Set an expiration time for a key * @param key key * @param time expiration time * @return Returns whether the setting is successful */ public Boolean expire(String key, long time){ return redisTemplate. expire(key, time, TimeUnit. SECONDS); } /** * Return the expiration time of a key * @param key key * @return Returns the remaining expiration time of the key */ public Long ttl(String key){ return redisTemplate. getExpire(key); } /** * Return whether the key exists * @param key key * @return whether the key exists */ public Boolean exists(String key){ return redisTemplate.hasKey(key); } /** * Add the delta value to the value of the key * @param key key * @param delta parameter * @return return the value of key + delta */ public Long incrby(String key, long delta){ return redisTemplate.opsForValue().increment(key, delta); } /** * Subtract delta from the value of key * @param key key * @param delta parameter * @return return key - delta value */ public Long decrby(String key, long delta){ return redisTemplate.opsForValue().decrement(key, delta); } //hash type /** * set hash type * @param key key * @param hashKey hashKey * @param value value */ public void hset(String key, String hashKey, Object value){ redisTemplate.opsForHash().put(key, hashKey, value); } /** * set hash type, and set the expiration time * @param key key * @param hashKey hashKey * @param value value * @param time expiration time * @return return success */ public Boolean hset(String key, String hashKey, Object value, long time){ hset(key, hashKey, value); return expire(key, time); } /** * batch setting hash * @param key key * @param map collection of hashKey and value * @param time expiration time * @return success */ public Boolean hmset(String key, Map<String,Object> map, long time){ redisTemplate.opsForHash().putAll(key, map); return expire(key, time); } /** * Get the value of hash type * @param key key * @param hashKey hashKey * @return returns the corresponding value */ public Object hget(String key, String hashKey){ return redisTemplate.opsForHash().get(key, hashKey); } /** * Get all the hash values and hashKey under the key * @param key key * @return return data */ public Map<Object,Object> hgetall(String key){ return redisTemplate.opsForHash().entries(key); } /** * batch deletion * @param key key * @param hashKey hashKey array collection */ public void hdel(String key, Object... hashKey){ redisTemplate.opsForHash().delete(key, hashKey); } /** * Determine whether there is a hashKey * @param key key * @param hashKey hashKey * @return exists */ public Boolean hexists(String key, String hashKey){ return redisTemplate.opsForHash().hasKey(key, hashKey); } }