springboot -redis使用十
2018-04-25 本文已影响7人
AmeeLove
依赖
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置
#redis
spring.redis.host=10.18.3.175
spring.redis.port=6379
spring.redis.database=8
spring.redis.pool.max-active=512
spring.redis.pool.max-idle=8
spring.redis.pool.max-wait=10000
package com.ghgcn.mysb.chapter03.service.impl;
import com.ghgcn.mysb.chapter03.entity.Student;
import com.ghgcn.mysb.chapter03.mapper.StudentMapper;
import com.ghgcn.mysb.chapter03.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements IStudentService {
@Autowired
private StudentMapper studentMapper;
@Cacheable
public List<Student> findAllStudents(){
return studentMapper.findAllStudents();
}
@Cacheable(value ="student" )
public Student findStudentById(Integer stuId){
return studentMapper.findStudentById(stuId);
}
@CachePut(value ="student" )
public int insertStudent(Student stu){
return studentMapper.insertStudent(stu);
}
@CachePut(value ="student" )
public int updateStudent(Student stu){
return studentMapper.updateStudent(stu);
}
@Cacheable
public List<Student> getStudents(){
return studentMapper.getStudents();
}
}
测试
import com.ghgcn.mysb.chapter03.Chpater03Application;
import com.ghgcn.mysb.chapter03.entity.Student;
import com.ghgcn.mysb.chapter03.mapper.StudentMapper;
import com.ghgcn.mysb.chapter03.service.IStudentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Chpater03Application.class)
public class Test1 {
@Autowired
private StudentMapper studentMapper;
@Autowired
private IStudentService studentService;
@Test
public void testStuMapper() {
System.err.println("studentMapper "+studentMapper);
}
@Test
public void testStuMapperGetALL() {
System.err.println("studentMapper "+studentService.findAllStudents());
}
@Test
public void testStuMapperGetId() {
Student student = studentService.findStudentById(9);
System.err.println("student "+student);
Student student2 = studentService.findStudentById(9);
System.err.println("student2 "+student);
}
@Test
public void testStuMapperInster() {
Student student = new Student();
student.setDob(new Date());
student.setName("测试inster1");
student.setEmail("test1@qq.com");
System.err.println("studentMapper "+studentService.insertStudent(student));
}
@Test
public void testStuMapperUpdate() {
Student student =studentService.findStudentById(9);
student.setDob(new Date());
student.setName("update1修改");
student.setEmail("update1修改@qq.com");
System.err.println("studentMapper "+studentService.updateStudent(student));
}
}
结果
image.png