124.【SpringBoot 源码刨析C】(九)

本文涉及的产品
云原生内存数据库 Tair,内存型 2GB
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Redis 版,经济版 1GB 1个月
简介: 124.【SpringBoot 源码刨析C】

Mapper接口

package com.jsxs.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jsxs.bean.Admin;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
 * @Author Jsxs
 * @Date 2023/7/17 11:01
 * @PackageName:com.jsxs.mapper
 * @ClassName: AdminMapper
 * @Description: TODO  继承接口之后我们不需要再编写 CRUD 就自带的已经存在了
 * @Version 1.0
 */
@Mapper
@Repository
public interface AdminMapper extends BaseMapper<Admin> {   // 需要继承BaseMapper,因为这个父类有很多已经分装好的方法
}

Service 接口

package com.jsxs.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jsxs.bean.Admin;
/**
 * @Author Jsxs
 * @Date 2023/7/18 11:26
 * @PackageName:com.jsxs.service
 * @ClassName: AdminService
 * @Description: TODO  我们业务层的接口只需要实现IService<Admin>
 * @Version 1.0
 */
public interface AdminService extends IService<Admin> {
}

业务层实现类接口:

package com.jsxs.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsxs.bean.Admin;
import com.jsxs.mapper.AdminMapper;
import com.jsxs.service.AdminService;
import org.springframework.stereotype.Service;
import java.io.Serializable;
/**
 * @Author Jsxs
 * @Date 2023/7/18 11:27
 * @PackageName:com.jsxs.service.impl
 * @ClassName: AdminServiceImpl
 * @Description: TODO  业务层的实现类要继承ServiceImpl<要实现表的BaseMapper,操作表对应的实体类名> 且 要实现业务层的接口
 * @Version 1.0
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper,Admin> implements AdminService {
}

测试类

package com.jsxs.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsxs.bean.Admin;
import com.jsxs.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import static org.junit.jupiter.api.Assertions.*;
/**
 * @Author Jsxs
 * @Date 2023/7/18 11:44
 * @PackageName:com.jsxs.service.impl
 * @ClassName: AdminServiceImplTest
 * @Description: TODO
 * @Version 1.0
 */
@SpringBootTest
@Slf4j
class AdminServiceImplTest {
    @Resource
    AdminService adminService;
    @Test
    void test(){
        System.out.println(adminService.getById(1));
        Page<Admin> page = new Page<>(1, 2); //当前页(初始页为1),一页几条
        IPage<Admin> iPage = adminService.page(page, null);  // Page分页的实列
        log.info("全部数据为: {}",iPage.getRecords()); // 分页遍历的话,遍历这个
        log.info("当前业为: {}",iPage.getCurrent());
        log.info("总多少页: {}",iPage.getPages());
        log.info("总多少条: {}",iPage.getTotal());
    }
}

假如说查询到的数据都为0,那么就是没有配置分页插件

package com.jsxs.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @Author Jsxs
 * @Date 2023/7/18 13:05
 * @PackageName:com.jsxs.config
 * @ClassName: MybatisPlusConfig
 * @Description: TODO
 * @Version 1.0
 */
@Configuration
public class MybatisPlusConfig {
    //  分页插件
    @Bean
    public PaginationInterceptor paginationinterceptor(){
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        return paginationInterceptor;
    }
}

为何Controller中调用接口就能实现接口实现类里的方法?

  1. 如何在删除之后仍然在原来的页面 ?

我们只需要在前端的删除按钮上绑定上当前页的页码,然后在后端进行页码的重定向跳转。

前端的操作:

后端的操作

5.整合Redis 非SQL数据库

Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理。它支持字符串、哈希表、列表、集合、有序集合,位图,hyperloglogs等数据类型。内置复制、Lua脚本、LRU收回、事务以及不同级别磁盘持久化功能,同时通过Redis Sentinel提供高可用,通过Redis Cluster提供自动分区。

(1).Redis 自动配置
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

自动配置:

  • RedisAutoConfiguration 自动配置类。RedisProperties 属性类 --> spring.redis.xxx是对redis的配置
  • 连接工厂是准备好的。LettuceConnectionConfiguration、JedisConnectionConfiguration (默认配置好了客户端)
  • 自动注入了RedisTemplate<Object, Object> : xxxTemplate;
  • 自动注入了StringRedisTemplate;k:v都是String
  • key:value
  • 底层只要我们使用 StringRedisTemplate、RedisTemplate就可以操作redis

redis环境搭建

如果我们使用window的话,我们在启动redis服务端的时候需要指定使用哪个配置 redis.windows-service.confredis.windows.conf。在这里我们使用redis.windows.conf设定了密码。

两种打开方式:

  1. 键入“cmd”切到安装目录后输出redis-server.exe redis.windows.conf,回车,就可以了。
  2. 在redis安装目录下新建文件startup.bat后,右击“编辑”,或者先用记事本建立该文件,再把扩展名改一下,文件里面写上:redis-server.exe redis.windows.conf。保存,以后再运行就直接运行这个文件,不要再直接运行redis-server.exe了,就可以了

(2).RedisTemplate与Lettuce
spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password: xxxxx
package com.jsxs.mapper;
import com.alibaba.fastjson2.JSON;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import javax.annotation.Resource;
import static org.junit.jupiter.api.Assertions.*;
/**
 * @Author Jsxs
 * @Date 2023/7/18 11:03
 * @PackageName:com.jsxs.mapper
 * @ClassName: AdminMapperTest
 * @Description: TODO
 * @Version 1.0
 */
@SpringBootTest
class AdminMapperTest {
    @Resource
    RedisTemplate redisTemplate;
    @Test
    void test2(){
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        opsForValue.set(JSON.toJSONString("hello5"),JSON.toJSONString("hello5"));
        System.out.println(opsForValue.get("hello5"));
    }
}

需要进行序列化处理

没有对 redisTemplate 进行序列化处理,java 代码会将 key 转化成对应的十六进制的数值进行操作.

注意事项: 我们不管通过 Java代码/Redis客户端 进行设置值还是获取值,我们都需要对其进行JSON(如果不是java会获取不到值)处理并且序列化(序列化可以避免默认的16进制)

package com.jsxs.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import java.net.UnknownHostException;
/**
 * @Author Jsxs
 * @Date 2023/7/20 13:56
 * @PackageName:com.jsxs.config
 * @ClassName: RedisConfig
 * @Description: TODO
 * @Version 1.0
 */
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        // 将template 泛型设置为 <String, Object>
        RedisTemplate<String, Object> template = new RedisTemplate();
        // 连接工厂,不必修改
        template.setConnectionFactory(redisConnectionFactory);
        /*
         * 序列化设置
         */
        // key、hash的key 采用 String序列化方式
        template.setKeySerializer(RedisSerializer.string());
        template.setHashKeySerializer(RedisSerializer.string());
        // value、hash的value 采用 Jackson 序列化方式
        template.setValueSerializer(RedisSerializer.json());
        template.setHashValueSerializer(RedisSerializer.json());
        template.afterPropertiesSet();
        return template;
    }
}

假如说key键值对的值是用的JOSN字符串,那么Java端获取的值就是JSON字符串。如果键值对的值用的是非JSON字符串,那么Java后端获取的值就是非JSON字符串。

总结: 以后开发全部使用JSON字符串

(3).切换至jedis

客户端类型有: lettuce 和 jedis 这两种。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--        导入jedis-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
redis:
    host: 127.0.0.1
    port: 6379
    password: 121788
    client-type: jedis  #指定客户端的类型为jedis
@Test
    void test3(){
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.auth("121788");
        System.out.println(jedis.ping());
        jedis.set("username1","123456");
        System.out.println(jedis.get("username1"));
    }

总结: 不管是 Letture 还是Jedis客户端,这两个客户端都需要用到序列化和 JSON

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的房屋租赁App的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的房屋租赁App的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的房屋租赁App的详细设计和实现(源码+lw+部署文档+讲解等)
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的武汉市公交路线查询系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的武汉市公交路线查询系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的旅游攻略系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的旅游攻略系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue个人博客系统设计和实现(源码+LW+部署讲解)
基于SpringBoot+Vue个人博客系统设计和实现(源码+LW+部署讲解)
18 7
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的成人教育APP的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的成人教育APP的详细设计和实现(源码+lw+部署文档+讲解等)
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue个人健康管理网站设计和实现(源码+LW+部署讲解)
基于SpringBoot+Vue个人健康管理网站设计和实现(源码+LW+部署讲解)
15 7
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的大学生勤工助学管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的大学生勤工助学管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的汉服交易小程序的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue的汉服交易小程序的详细设计和实现(源码+lw+部署文档+讲解等)
18 7
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的志愿服务管理系统设计和实现(源码+LW+部署讲解)
基于SpringBoot+Vue的志愿服务管理系统设计和实现(源码+LW+部署讲解)
26 6
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的公园管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的公园管理系统的详细设计和实现(源码+lw+部署文档+讲解等)