在https://www.jianshu.com/p/893570a174ad Java操作Redis的基础上添加代码
1、pom文件中添加mysql相关jar
2、application.yml中添加mysql连接信息
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=false
username: root
password: root
3、UserMapper.java
public interface UserMapper {
@Select("select * from user_t")
List findUser();
@Update("update user_t set userName=#{userName} where userId=#{userId}")
void update(UserEntity userEntity);
}
4、UserController.java
public class UserController {
@Autowired
private UserMapperuserMapper;
@RequestMapping("/findUser")
@Cacheable(cacheNames ="userAll", key ="'findUser'")
public List findUser() {
return userMapper.findUser();
}
@RequestMapping("/updateUser")
@CacheEvict(cacheNames ="userAll", key ="'findUser'")
public void updateUser(UserEntity userEntity){
userMapper.update(userEntity);
}
}
5、启动类App.java
@SpringBootApplication
@MapperScan("com.test.mapper")
@EnableCaching
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class);
}
}