Spring boot整合Spring Data Jpa,Redis和Spring Cache入门练习

这两天一直在学习Spring Cache,下面就简单总结一下。
首先,从创建项目开始:我使用的是Spring tool suit创建的Spring boot + Spring Data Jpa 项目,这里就不详细讲了。项目简单整合Redis和Spring Cache。
下面是主配置文件application.properties的配置:

  • spring.datasource.url = jdbc:mysql://localhost:3306/test
    spring.datasource.username = root spring.datasource.password = root
    spring.datasource.driverClassName = com.mysql.jdbc.Driver

    Specify the DBMS

    spring.jpa.database = MYSQL

    Show or not log for each sql query

    spring.jpa.show-sql = true

    Hibernate ddl auto (create, create-drop, update)

    spring.jpa.hibernate.ddl-auto = update

    Naming strategy

    spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

    dialet

    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

    Redis default database index is 0

    spring.redis.database=0

    Redis server address

    spring.redis.host=localhost

    Redis server port

    spring.redis.port=6379

    Redis password

    spring.redis.password=**

User类:

@Entity
@Table(name = "user")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String username;
    private String password;
    private String email;
    ...省略setter和getter
}

启动类,注意一定要加上@EnableCaching这个注解。

@SpringBootApplication
@EnableCaching
public class SpringDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringDemoApplication.class, args);
    }
}

UserRepository实现JpaRepository, PagingAndSortingRepository两个接口。

UserServiceImpl类实现IUserService 接口:

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    @CachePut(cacheNames = "user")
    public User save(User user) {
        return userRepository.save(user);
    }

    @Override
    @Cacheable(value =  "user", key = "#id")
    public User findById(Long id) {
        Optional op = userRepository.findById(id);
        User user = op.get();
        return user;
    }

    @Override
    @Cacheable(value =  "user", key = "#id")
    public User findByUsername(String username) {
        return userRepository.findByUsername(username);
    }

    @Override
    @Cacheable(cacheNames = "userList")
    public List findAll() {
        return userRepository.findAll();
    }

    @CacheEvict(value = "user",allEntries=true)
    public void clearCache() {}

    @CacheEvict(value = "user")
    public void clean(Long id) {}

}

ServiceTest类:

@SpringBootTest
@RunWith(SpringRunner.class)
public class ServiceTest {

    @Autowired
    private IUserService userService;

    @Test
    public void clean() {
        userService.clearCache();
    }

    @Test
    public void cleanById() {
        userService.clean(1L);
    }

    @Test
    public void get() {
        User user = userService.findById(1L);
        System.out.println(user);
    }
}

好了,代码大概就这样。
下面就是一些坑了:
1、@EnableCaching这个在启动类那里不要忘记添加了;
2、pojo里的类如果要缓存一定要实现Serializable序列化接口;
3、@Cacheable的value不能为空,key可以不指定;
4、@CacheEvict(value = “user”,allEntries=true)的意思是清除user下的所有缓存,allEntries默认为false;
5、如果享有目地清缓存,要:

@CacheEvict(value = "user")
    public void clean(Long id) {}
}

OK,菜鸟一只,才参悟Spring Cache两天,理解得比较浅,望见谅,后续再慢慢添加。

你可能感兴趣的:(Spring,boot相关)