一 操作案例
1.1 工程结构
1.2 pom文件的配置
<!--spring boot的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql的依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--spring boot的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 常规依赖 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!--SpringBoot与Redis整合依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<!--jedis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
<!--lettuce-->
<!--<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.2.1.RELEASE</version>
</dependency>-->
<!--swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- redisson 分布式锁-->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.11.2</version>
</dependency>
<!--guava Google 开源的 Guava 中自带的布隆过滤器-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
<!--persistence-->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<!--通用Mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
主要依赖
1.3 dao层
@org.apache.ibatis.annotations.Mapper
public interface CustomerDao extends Mapper<Customer> {
}
1.4 service层
1.接口
public interface CustormerService {
public void addCustomerData(Customer customer);
}
2.实现类
@Service
public class CustomerServiceImpl implements CustormerService {
public static final String CACHA_KEY_CUSTOMER = "customer:";
@Autowired
private CustomerDao customerDao;
@Autowired
private RedisTemplate redisTemplate;
public void addCustomerData(Customer customer){
//1.先插入mysql中
int k= customerDao.insertSelective(customer);
if(k>0){
//2.从mysql中查询一次
Customer result=customerDao.selectByPrimaryKey(customer.getId());
//3.新增到redis中
String key=CACHA_KEY_CUSTOMER+customer.getId();
redisTemplate.opsForValue().set(key,result);
}
}
}
1.5 controller层
@RestController
@Slf4j
public class CustomerController {
@Resource
private CustormerService customerSerivce;
@ApiOperation("数据库初始化2条Customer记录插入")
@RequestMapping(value = "/customer/add",method = RequestMethod.GET)
public void addCustomer()
{
for (int i = 1; i < 3; i++) {
Customer customer = new Customer();
customer.setId(i);
customer.setCname("customer"+i);
customer.setAge(new Random().nextInt(30)+1);
customer.setPhone("1381111XXXX");
customer.setSex((byte) new Random().nextInt(2));
customer.setBirth(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
customerSerivce.addCustomerData(customer);
}
}
}
1.6 启动类
1.7 测试类
1.初始化数据库
CREATE TABLE `t_customer` (
`id` bigint(11) NOT NULL,
`cname` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`phone` varchar(255) DEFAULT NULL,
`sex` varchar(255) DEFAULT NULL,
`birth` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.页面访问
3.查看数据库:
4.查看redis
二 实现查询操作
2.1 service
@Override
public Customer findDataByNoFilter(Integer id) {
//1.先查询redis
String key=CACHA_KEY_CUSTOMER+id;
Customer customer=(Customer) redisTemplate.opsForValue().get(key);
if(customer==null){
System.out.println("刚开始redis不存在。。。。。。");
//2.redis为空,查询mysql
customer=customerDao.selectByPrimaryKey(id);
if(customer!=null){
//3.mysql中数据存在, 把mysq查询出来的数据回写redis,保持一致性
redisTemplate.opsForValue().set(key,customer);
}
}
return customer;
}
}
2.2 controller
@ApiOperation("单个customer查询操作,按照customerid查询")
@RequestMapping(value = "/customer/{id}",method = RequestMethod.GET)
public Customer findCustomerById(@PathVariable Integer id)
{
return customerSerivce.findDataByNoFilter(id);
}
2.3 测试