本案例需要用到的软件有redis和oracle
pom.xml如下
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</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-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 封装了json -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
<!-- 热部署插件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.properties 如下
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.username=liubin_test spring.datasource.password=1105915292 spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl server.port=8080 server.servlet.context-path=/study spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.database=0 spring.redis.password=1105915292 spring.cache.cache-names=c1
Controller
@RestController @CacheConfig(cacheNames = "c1") public class TestController { @Autowired EuserDao dao; @Cacheable(value= "data", key="#userId") @GetMapping("getByUserId/{userId}") public Map<String, Object> queryByUser(@PathVariable("userId") Long userId) { return dao.queryByuserId(userId); } @RequestMapping("updateName/{userName}/{userId}") @CacheEvict(value="data" , key="#userId") public void updateName(@PathVariable("userName")String userName,@PathVariable("userId")Long userId) { dao.updateName(userName,userId); } }
Dao
@Service public class EuserDao { @Autowired JdbcTemplate jdbcTemplate; public HashMap<String,Object> queryByuserId(Long userId){ HashMap<String,Object> queryForObject2 = jdbcTemplate.queryForObject("SELECT USER_NAME,POLICY_CODE,COMM_NAME FROM E_USER WHERE USER_ID=?", new GenericMapper(),userId); return queryForObject2; } public void updateName(String userName, Long userId) { jdbcTemplate.update("UPDATE E_USER SET USER_NAME=? WHERE USER_ID=?",new PreparedStatementSetter() { @Override public void setValues(PreparedStatement ps) throws SQLException { ps.setString(1, userName); ps.setLong(2, userId); } }); } }
public class GenericMapper implements RowMapper<HashMap<String, Object>> { @Override public HashMap<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException { ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); HashMap<String, Object> map = new HashMap<String, Object>(); for (int i = 1; i <= columnCount; i++) { String columnName = metaData.getColumnLabel(i); String value = rs.getString(columnName); map.put(translateName(columnName), value); } return map; } //字段名转换 USER_NAME ===> userName public String translateName(String columnName) { String newName = columnName.toLowerCase(); if (newName.contains("_")) { StringBuffer sbf = new StringBuffer(); String[] nameArr = newName.split("_"); int size = nameArr.length; for (int i = 0; i < size; i++) { String nameSub = nameArr[i]; if (i==0) { sbf.append(nameSub); }else { sbf.append(nameSub.substring(0, 1).toUpperCase()+nameSub.substring(1)); } } newName = sbf.toString(); } return newName; } }
启动类
@SpringBootApplication @EnableCaching public class SpringCacheApplication { public static void main(String[] args) { SpringApplication.run(SpringCacheApplication.class, args); } }
然后就可以自己查询缓存是否使用.