1 新建springboot项目
2 相关注解
@EnableCaching 在启动类上加上注解启动缓存
#作用在你要缓存的数据上
@Cacheable(key="#id",cacheNames="com.sxt.service.impl.MenuServiceImpl")
@Cacheput 解决脏读
@CachEvict(解决脏读)
@Cacheconfig(全局的配置缓存)
3 修改yml
#
spring:
#数据源
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.222.132:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
type: org.springframework.jdbc.datasource.DriverManagerDataSource
#redis
redis:
host: 192.168.222.131
password: 123456
port: 6379
jedis:
pool:
max-active: 20
max-idle: 8
min-idle: 0
max-wait: 2000
#mybatis
mybatis:
mapper-locations:
- classpath:mapper/*Mapper.xml
configuration:
log-prefix: mybatis
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
4 User类
package com.example.demo.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
/**
* 用户编号
*/
private Integer id;
/**
* 用户姓名
*/
private String name;
/**
* 用户地址
*/
private String address;
/**
* 出生时间
*/
private Date birth;
/**
* 是否删除1删除0未删除
*/
private Integer flag;
private static final long serialVersionUID = 1L;
}
5 创建UserMaper
package com.example.demo.mapper;
import com.example.demo.domain.User;
import java.util.List;
public interface UserMapper {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
List<User> queryAllUser();
}
6 UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.demo.domain.User">
<!--@mbg.generated-->
<!--@Table sys_user-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="address" jdbcType="VARCHAR" property="address" />
<result column="birth" jdbcType="TIMESTAMP" property="birth" />
<result column="flag" jdbcType="INTEGER" property="flag" />
</resultMap>
<sql id="Base_Column_List">
<!--@mbg.generated-->
id, `name`, address, birth, flag
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--@mbg.generated-->
select
<include refid="Base_Column_List" />
from sys_user
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
<!--@mbg.generated-->
delete from sys_user
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.example.demo.domain.User" useGeneratedKeys="true">
<!--@mbg.generated-->
insert into sys_user (`name`, address, birth,
flag)
values (#{name,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}, #{birth,jdbcType=TIMESTAMP},
#{flag,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.example.demo.domain.User" useGeneratedKeys="true">
<!--@mbg.generated-->
insert into sys_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null">
`name`,
</if>
<if test="address != null">
address,
</if>
<if test="birth != null">
birth,
</if>
<if test="flag != null">
flag,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="address != null">
#{address,jdbcType=VARCHAR},
</if>
<if test="birth != null">
#{birth,jdbcType=TIMESTAMP},
</if>
<if test="flag != null">
#{flag,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.example.demo.domain.User">
<!--@mbg.generated-->
update sys_user
<set>
<if test="name != null">
`name` = #{name,jdbcType=VARCHAR},
</if>
<if test="address != null">
address = #{address,jdbcType=VARCHAR},
</if>
<if test="birth != null">
birth = #{birth,jdbcType=TIMESTAMP},
</if>
<if test="flag != null">
flag = #{flag,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.example.demo.domain.User">
<!--@mbg.generated-->
update sys_user
set `name` = #{name,jdbcType=VARCHAR},
address = #{address,jdbcType=VARCHAR},
birth = #{birth,jdbcType=TIMESTAMP},
flag = #{flag,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="queryAllUser" resultMap="BaseResultMap">
<!--@mbg.generated-->
select
<include refid="Base_Column_List" />
from sys_user
</select>
</mapper>
7 UserService
package com.example.demo.service;
import com.example.demo.domain.User;
import java.util.List;
public interface UserService{
int deleteByPrimaryKey(Integer id);
User insert(User user);
User selectByPrimaryKey(Integer id);
User updateByPrimaryKey(User user);
List<User> queryAllUser();
}
8 UserSerivceImpl
package com.example.demo.service.impl;
import com.example.demo.domain.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
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 java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@CacheEvict(cacheNames = "user",key = "#id")
@Override
public int deleteByPrimaryKey(Integer id) {
return this.userMapper.deleteByPrimaryKey(id);
}
// @CachePut(cacheNames = "user",key = "#user.id")
@CachePut(cacheNames = "user",key = "#result.id+'-'+#result.flag")
@Override
public User insert(User user) {
this.userMapper.insert(user);
return user;
}
@Cacheable(cacheNames = "user",key = "#id")
@Override
public User selectByPrimaryKey(Integer id) {
return this.userMapper.selectByPrimaryKey(id);
}
@CachePut(cacheNames = "user",key = "#user.id")
@Override
public User updateByPrimaryKey(User user) {
int index = this.userMapper.updateByPrimaryKey(user);
return user;
}
@Override
public List<User> queryAllUser() {
return this.userMapper.queryAllUser();
}
}
9 修改启动类
9 测试
package com.example.demo;
import com.example.demo.domain.User;
import com.example.demo.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Date;
@SpringBootTest
class ApplicationTests {
@Autowired
private UserService userService;
@Test
void contextLoads() {
User user = new User(1,"小明","武汉",new Date(),1);
userService.insert(user);
System.out.println("操作成功");
}
@Test
void update() {
User user = new User(2,"小明","武汉北京",new Date(),0);
userService.updateByPrimaryKey(user);
System.out.println("操作成功");
}
@Test
void query() {
User user = userService.selectByPrimaryKey(2);
System.out.println(user);
System.out.println("操作成功");
}
@Test
void delete(){
this.userService.deleteByPrimaryKey(2);
System.out.println("操作成功");
}
}