java 项目 自动生成代码 redis缓存

前提观看  :

redis-2 Redis的使用连接 java 以及 缓存

使用 grade 表 和 student 表  自动生成之后 根据 grade  表 查询所有信息

一、步骤

1、先在GradeController 层 添加

@Resource
private IGradeService gradeService;

查找所有的数据
@GetMapping("/findAll")
 public List<Grade> findAll3() {
 return gradeService.findAll();
}

点击 红色 的 findAll()    进入Service 层 生成

然后点击  findAll()  生成 实现类

生成

然后点击findAll()  回车   在 mapper 层生成  List<Grade> findAll();

然后点击 findAll()  生成 xml 文件   写 sql 语句  写之前先配置 关联信息

写 sql 语句

<mapper namespace="com.example.sbredis.mapper.GradeMapper">

<!--    一般情况下  一对多  或  多对一     使用 resultMap  -->
    <resultMap id="getAllGradeAndStu" type="com.example.sbredis.entity.Grade">
        <id property="cid" column="cid"></id>
        <id property="cname" column="cname"></id>
        <collection property="students" ofType="com.example.sbredis.entity.Student" autoMapping="true">
            <id column="sname" property="sname"></id>
            <id column="id" property="id"></id>
        </collection>
    </resultMap>
    <select id="findAll" resultMap="getAllGradeAndStu">
        select g.cid, g.cname, s.id, s.sname
        from grade g,
             student s
        where g.cid = s.cid

    </select>
</mapper>

在 Gradecontroller 写代码

package com.example.sbredis.controller;

import com.example.sbredis.entity.Grade;
import com.example.sbredis.service.IGradeService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 单朝阳
 * @since 2023-10-28
 */
@RestController
@RequestMapping("/grade")
public class GradeController {

    @Resource
    private IGradeService gradeService;


    /**
     * 查询班级信息 找出所有学生的信息
     */
    // 查询所有学生的信息

//    @Resource
//    private IGradeService gradeService;

    //  查询出数据
//    @GetMapping("/findAll")
//    public List<Grade> findAll3() {
//        return gradeService.findAll();
//    }



//    //  进行分页
//    @GetMapping("/findAll")
//    public List<Grade> findAll3() {
//        List<Grade> all = gradeService.findAll();
//        取出第二页数据 , 每页显示 两条 数据
    //    忽略  1   2      显示  3   4
//        skip 跳过 两条数据  limit 限制为每页 2 条数据
//        //  collect(Collectors.toList())  将 流 转换为集合
//        // n  为 每页的第几条数据
//        List<Grade> collect = all.stream().skip(2).limit(2).collect(Collectors.toList());
//        return collect;
//    }


    //  进行分页
    @GetMapping("/findAll")
    public List<Grade> findAll3(Integer page,Integer pagesize) {
        List<Grade> all = gradeService.findAll();
        //  取出第二页数据 , 每页显示 1 条 数据
        //  skip 跳过 第 1 条数据  limit 限制为每页显示 1 条数据
        //  collect(Collectors.toList())  将 流 转换为集合
        // n  为 每页的第几条数据
        int i = (page - 1) * pagesize;
        List<Grade> collect = all.stream().skip(i).limit(pagesize).collect(Collectors.toList());
        return collect;
    }
}

在 实现类 开启缓存

 

显示第一页   每页一条数据

显示第二页数据  每页显示一条数据

显示第一页数据   每页显示两条数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机械能实验

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值