后端开发总是要保证数据返回的速度越快越好,可是数据库查问就是那么个速度,通过优化 SQL 和数据库配置都不是最正当的办法。所以明天咱们来看一下缓存。
Cache
话说缓存,咱们总是第一工夫想到 redis,可是最要害的是 redis 须要本人启动客户端,这就比拟麻烦了。咱们只是须要缓存简略的数据怎么办?springboot 为咱们想到了这个问题,于是默认增加了缓存 ConcurrentMapCacheManager。
1. 新建 SpringBoot 我的项目
不再赘述,参考之前的文章即可
2. 批改 pom.xml 文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
这里咱们应用 jpa。
3. 新建数据库表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '姓名',
`address` varchar(512) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '地址',
`age` int NULL DEFAULT NULL COMMENT '年龄',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
4. 写代码啦
4.1 新建实体类 User
/**
* 实体类
* @author zhouzhaodong
*/
@Entity
public class User implements Serializable {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int id;
private String name;
private String address;
private int age;
public int getId() {return id;}
public void setId(int id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public String getAddress() {return address;}
public void setAddress(String address) {this.address = address;}
public int getAge() {return age;}
public void setAge(int age) {this.age = age;}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
", age=" + age +
'}';
}
}
4.2 新建数据库拜访层继承 JpaRepository
/**
* 数据库拜访层
* @author zhouzhaodong
*/
public interface UserRepository extends JpaRepository<User, Integer> {
/**
* 依据 id 更新
*
* @param id
* @param name
* @param address
* @param age
*/
@Modifying
@Query(value = "update user u set u.name = :name, u.address = :address, u.age = :age where u.id = :id", nativeQuery = true)
User updateById(@Param("id") Integer id,
@Param("name") String name,
@Param("address") String address,
@Param("age") int age);
}
4.3 定义业务层类
/**
* Service
* @author zhouzhaodong
*/
@Service
public interface UserService {
/**
* 新增
* @param user
* @return
*/
User insert(User user);
/**
* 删除
* @param id
* @return
*/
void delete(int id);
/**
* 更新
* @param user
* @return
*/
void update(User user);
/**
* 依据 id 查问
* @param id
* @return
*/
Object findById(int id);
}
/**
* 实现类
* @author zhouzhaodong
*/
@Service
public class UserServiceImpl implements UserService {
@Resource
UserRepository userRepository;
@Override
@CachePut(value = "user", key = "#result.id")
public User insert(User user) {return userRepository.save(user);
}
@Override
@CacheEvict(value = "user", key = "#id")
public void delete(int id) {userRepository.deleteById(id);
}
@Override
@CachePut(value = "user", key = "#user.id")
public User update(User user) {return userRepository.updateById(user.getId(), user.getName(), user.getAddress(), user.getAge());
}
@Override
@Cacheable(value = "user", key = "#id")
public Object findById(int id) {return userRepository.findById(id);
}
}
4.4 管制层
/**
* 管制层
* @author zhouzhaodong
*/
@RestController
public class UserController {
@Resource
UserService userService;
@RequestMapping("/save")
public User saveUser(User user){return userService.insert(user);
}
@RequestMapping("/delete")
public void deleteUser(int id){userService.delete(id);
}
@RequestMapping("/update")
public User updateUser(User user){return userService.update(user);
}
@RequestMapping("/find")
public Object findById(int id){return userService.findById(id);
}
}
4.5 启动类增加注解
/**
* 启动类
* @author zhouzhaodong
*/
@SpringBootApplication
@EnableCaching
public class CacheApplication {public static void main(String[] args) {SpringApplication.run(CacheApplication.class, args);
}
}
4.6 application 配置文件
server:
port: 8989 # 端口号
spring:
datasource:
url: jdbc:mysql://ip 地址:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8&rewriteBatchedStatements=true
username: 用户名
password: 明码
jpa:
show-sql: true # 打印 sql
properties:
hibernate:
format_sql: true # 打印 sql
5. post 测试
首先新增数据,而后进行查问就会发现并不走数据库查问,控制台不打印 sql,缓存胜利。
具体代码请查看:
https://github.com/zhouzhaodo…
集体博客:
http://www.zhouzhaodong.xyz/