spring-boot构建项目
1、依赖包
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
</dependencies>
2、application.yml 配置你的redis数据库
redis:
host: 10.118.62.19
port: 6379
database: 9
password: 123456
jedis:
pool:
max-active: 100
max-idle: 100
max-wait: 1000ms
min-idle: 10
3、在启动类添加@EnableCaching
@EnableCaching
public class SpringbootRedisCacheApplication {
@Bean
public RedisCacheConfiguration redisCacheConfiguration(){
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
return configuration;
}
public static void main(String[] args) {
SpringApplication.run(SpringbootRedisCacheApplication.class, args);
}
}
4、创建类EmpService
@Service
public class EmpService {
/**
*
* @param empId
* @return
* Cacheable会将方法的返回值序列化后存储到redis中key就是参数执行的字符串
* Cacheable的用途就是在执行方法前检查对应的key是否存在 ,存在则直接从redis中取出 不执行方法中代码
* condition 代表条件成立的时候才会执行方法中的代码
*/
@Cacheable(value = "emp1",key = "#empId",condition = "#empId !=1000")
public Emp findById(Integer empId){
System.out.println("执行了empById方法:"+empId);
return new Emp(empId,"laohan",new Date(),1000f,"研发");
}
@Cacheable(value = "emp:rank:salary")
public List<Emp> getEmpList(){
System.out.println("缓存数据!");
List list = new ArrayList();
for(int i=0;i<10;i++){
list.add(new Emp(i,"emp"+i,new Date(),200f,"部门"));
}
return list;
}
//CachePut不管redis是否存在key都对数据进行更新
@CachePut(value = "emp",key = "#emp.empNo")
public Emp create(Emp emp){
System.out.println("正在创建"+emp.getEmpNo()+"员工信息");
return emp;
}
@CachePut(value = "emp",key = "#emp.empNo")
public Emp update(Emp emp){
System.out.println("正在更新"+emp.getEmpNo()+"员工信息");
return emp;
}
//删除缓存 key 与redis对应
@CacheEvict(value="emp" , key = "#empNo")
public void delete(Integer empNo){
System.out.println("删除" + empNo + "员工信息");
}
}
5、测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRedisCacheApplicationTests {
@Resource
private EmpService empService;
@Test
public void testEmp(){
Emp emp = empService.findById(1000);
System.out.println(emp.toString());
}
@Test
public void testGet(){
List<Emp> empList = empService.getEmpList();
System.out.println(empList);
}
@Test
public void testCreate(){
empService.create(new Emp(1002,"emp"+new Date().getTime(),new Date(),1234f,"市场"));
//调用更新用户名
empService.update(new Emp(1002,"u-emp"+new Date().getTime(),new Date(),1234f,"市场"));
}
@Test
public void testDel(){
System.out.println("删除员工信息");
empService.delete(1002);
}
}
6、实体类Emp
public class Emp implements Serializable {
private Integer empNo;
private String name;
private Date birthday;
private Float salary;
private String department;
public Emp() {
}
public Emp(Integer empNo, String name, Date birthday, Float salary, String department) {
this.empNo = empNo;
this.name = name;
this.birthday = birthday;
this.salary = salary;
this.department = department;
}
public Integer getEmpNo() {
return empNo;
}
public void setEmpNo(Integer empNo) {
this.empNo = empNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Float getSalary() {
return salary;
}
public void setSalary(Float salary) {
this.salary = salary;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public String toString() {
return "Emp{" +
"empNo=" + empNo +
", name='" + name + '\'' +
", birthday=" + birthday +
", salary=" + salary +
", department='" + department + '\'' +
'}';
}
}