spring boot使用spring缓存demo

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);
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 是一种由 Pivotal 团队推出的用于简化 Spring 应用程序创建、配置以及部署的框架。它提供了一套高效、灵活的工具集,旨在帮助开发者快速构建功能丰富的微服务应用程序。以下是对 Spring Boot 的详细介绍: ### 主要特点 1. **自动配置**:Spring Boot 提供了默认配置,减少了需要手动配置的地方,让开发者可以更专注于业务逻辑的实现。 2. **嵌入式服务器**:内置了 Tomcat 或 Jetty 等应用服务器,使得开发者无需额外配置即可运行应用程序。 3. **启动脚手架**:提供了`spring-boot-maven-plugin`或`spring-boot Gradle plugin`等插件,简化了项目的构建过程。 4. **依赖管理**:通过集中式的 `dependency management` 功能,开发者只需添加特定版本的 Spring Boot 依赖,就能避免版本冲突,并轻松更新到新版本。 5. **易于测试**:Spring Boot 支持内嵌测试容器,如 Lombok 和 Spring Test,方便进行单元测试和集成测试。 6. **外部化配置**:支持读取环境变量、properties 文件、YAML 文件等多种配置源,便于管理和维护。 7. **生产级特性**:包含了各种生产环境中常用的特性,例如监控、日志记录、健康检查、安全性和缓存等。 8. **社区活跃**:有大量的文档、教程和示例可供参考,同时有活跃的社区和技术支持。 ### 使用场景 Spring Boot 适用于各种规模的应用,从小型的单体应用到大型的微服务架构都适用。尤其适合那些希望快速搭建并部署应用的场景,比如快速迭代的小型项目、API 网关、后台管理系统、数据处理任务等。 ### 示例 下面是一个简单的 Spring Boot 应用启动示例: ```java package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 在这个示例中,我们声明了一个名为 `DemoApplication` 的主类,并使用了 `@SpringBootApplication` 注解,这个注解组合了 `@Configuration`, `@EnableAutoConfiguration`, 和 `@ComponentScan` 的作用,简化了配置步骤。 ### 相关问题: 1. Spring Boot 如何与其他框架结合使用,如 MyBatis 或 Thymeleaf? 2. Spring Boot 中如何配置和使用数据库连接池,如 HikariCP 或 Druid? 3. 如何在 Spring Boot 应用中实现JWT认证机制?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值