pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tansun</groupId>
<artifactId>cache-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<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-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件 application.properties
spring.datasource.url=jdbc:mysql://192.168.229.132:3306/football?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
控制器
package com.tansun.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.tansun.entity.User;
import com.tansun.service.UserService;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/save")
public Object save(User user) {
return userService.save(user);
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable("id")String id) {
userService.delete(id);
return "ok";
}
@GetMapping("/findOne/{id}")
public Object findOne(@PathVariable("id")String id) {
return userService.findOne(id);
}
}
实体类
package com.tansun.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class User {
@Id
private String id;
private String name;
private Integer age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User(String id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public User() {}
}
service接口及实现
package com.tansun.service;
import com.tansun.entity.User;
public interface UserService {
User save(User user);
void delete(String id);
User findOne(String id);
}
package com.tansun.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.tansun.entity.User;
import com.tansun.repository.UserRepository;
import com.tansun.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
@CachePut(value="person", key="#user.id")
public User save(User user) {
return userRepository.save(user);
}
@Override
@CacheEvict(value="person")
public void delete(String id) {
userRepository.delete(id);
}
@Override
@Cacheable(value="person", key="#id")
public User findOne(String id) {
return userRepository.findOne(id);
}
}
dao
package com.tansun.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.tansun.entity.User;
public interface UserRepository extends JpaRepository<User, String> {
}
主方法
package com.tansun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@EnableCaching
@SpringBootApplication
public class CacheTestApplication {
public static void main(String[] args) {
SpringApplication.run(CacheTestApplication.class, args);
}
}