当前位置: 首页 > wzjs >正文

wamp可以做视频网站吗短链接生成

wamp可以做视频网站吗,短链接生成,wordpress 主题使用,广州专业网站制作设计目录 一、简单分页查询——limit 1 基于注解的简单分页查询 2 基于Mapper.xml的复杂分页 二、PageHelper 三、基于mp实现分页 一、简单分页查询——limit 1 基于注解的简单分页查询 Mapper接口 import org.apache.ibatis.annotations.Param; import org.apache.ibatis.…

目录

一、简单分页查询——limit

1 基于注解的简单分页查询

2 基于Mapper.xml的复杂分页

二、PageHelper

三、基于mp实现分页


一、简单分页查询——limit

1 基于注解的简单分页查询

Mapper接口

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;public interface UserMapper {// 分页查询用户@Select("SELECT * FROM user LIMIT #{offset}, #{limit}")List<User> selectByPage(@Param("offset") int offset, @Param("limit") int limit);// 查询总记录数@Select("SELECT COUNT(*) FROM user")int getTotalCount();
}

Service类

public class UserService {@Autowiredprivate UserMapper userMapper;public Page<User> getPage(int currentPage, int pageSize) {int total = userMapper.getTotalCount(); // 获取总记录数int totalPage = (total + pageSize - 1) / pageSize; // 计算总页数int offset = (currentPage - 1) * pageSize; // 计算偏移量List<User> users = userMapper.selectByPage(offset, pageSize); // 获取分页数据return new Page<>(currentPage, pageSize, total, totalPage, users);}
}

Controller类

Page<User> page = userService.getPage(1, 10); // 获取第 1 页,每页 10 条记录

2 基于Mapper.xml的复杂分页

(1)定义Page类——封装分页结果

public class Page {private int currentPage; // 当前页码private int pageSize; // 每页显示的记录数// 构造方法public Page(int currentPage, int pageSize) {this.currentPage = currentPage;this.pageSize = pageSize;
}// 计算偏移量public int getOffset() {return (currentPage - 1) * pageSize;}
}

(2)定义PageResult类——封装查询结果

import java.util.List;public class PageResult<T> {private List<T> data; // 分页数据private int total; // 总记录数private int totalPage; // 总页数private int currentPage; // 当前页码private int pageSize; // 每页显示的记录数// 构造方法public PageResult(List<T> data, int total, int currentPage, int pageSize) {this.data = data;this.total = total;this.currentPage = currentPage;this.pageSize = pageSize;this.totalPage = (total + pageSize - 1) / pageSize; // 计算总页数}}

(3)Mapper接口

import org.apache.ibatis.annotations.Param;
import java.util.List;public interface UserMapper {// 分页查询用户List<User> selectByPage(@Param("condition") String condition, @Param("offset") int offset, @Param("limit") int limit);// 查询总记录数int getTotalCount(@Param("condition") String condition);
}

(4)xxxMapper.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.mapper.UserMapper"><!-- 分页查询 --><select id="selectByPage" resultType="User">SELECT * FROM userWHERE name LIKE CONCAT('%', #{condition}, '%')LIMIT #{offset}, #{limit}</select><!-- 查询总记录数 --><select id="getTotalCount" resultType="int">SELECT COUNT(*) FROM userWHERE name LIKE CONCAT('%', #{condition}, '%')</select></mapper>

(5)Service层

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserService {@Autowiredprivate UserMapper userMapper;// 获取分页数据public PageResult<User> getPage(String condition, Page page) {// 查询总记录数int total = userMapper.getTotalCount(condition);// 查询分页数据List<User> users = userMapper.selectByPage(condition, page.getOffset(), page.getPageSize());// 封装分页结果return new PageResult<>(users, total, page.getCurrentPage(), page.getPageSize());}
}

(6)Controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/users")public PageResult<User> getUsers(@RequestParam String condition,@RequestParam int currentPage,@RequestParam int pageSize) {Page page = new Page(currentPage, pageSize); // 创建分页对象return userService.getPage(condition, page); // 调用 Service 层方法}
}

二、PageHelper

(1)引入依赖

<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.2.0</version>
</dependency>

(2)配置 MyBatis

在 mybatis-config.xml 中配置 PageHelper 插件:

<configuration><plugins><plugin interceptor="com.github.pagehelper.PageHelper"><property name="helperDialect" value="mysql"/>  <!-- 配置数据库方言 --><property name="reasonable" value="true"/><property name="supportMethodsArguments" value="true"/></plugin></plugins>
</configuration>

(4)使用PageHelper实现分页

无查询条件:

Mapper接口:

import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Select;import java.util.List;public interface UserMapper {@Select("SELECT id, username, age FROM user")Page<User> getAllUsers();  // 返回 Page 类型,PageHelper 会自动处理分页
}

Service层:

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public PageInfo<User> getUsersByPage(int pageNum, int pageSize) {// 使用 PageHelper.startPage 开始分页PageHelper.startPage(pageNum, pageSize);// 查询数据List<User> userList = userMapper.getAllUsers();// 使用 PageInfo 包装查询结果,返回分页信息return new PageInfo<>(userList);}
}

Controller层:

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/users")public PageInfo<User> getUsers(@RequestParam int pageNum, @RequestParam int pageSize) {return userService.getUsersByPage(pageNum, pageSize);}
}

有查询条件:

public interface UserMapper {List<User> selectAll(@Param("condition") String condition);
}
<?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.mapper.UserMapper"><select id="selectAll" resultType="User">SELECT * FROM userWHERE name LIKE CONCAT('%', #{condition}, '%')</select></mapper>
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;public class UserService {@Autowiredprivate UserMapper userMapper;public PageInfo<User> getPage(String condition, int currentPage, int pageSize) {// 使用 PageHelper 插件开始分页PageHelper.startPage(currentPage, pageSize);// 调用 Mapper 方法查询数据List<User> users = userMapper.selectAll(condition);// 使用 PageInfo 封装分页信息return new PageInfo<>(users);}
}

 在控制器或其他地方调用分页方法,传入查询条件、当前页码和每页显示的记录数。

PageInfo<User> pageInfo = userService.getPage("张三", 1, 10); // 查询条件为 "张三",获取第 1 页,每页 10 条记录
List<User> users = pageInfo.getList(); // 获取分页数据
int total = (int) pageInfo.getTotal(); // 获取总记录数
int pages = pageInfo.getPages(); // 获取总页数

三、基于mp实现分页

(1)在项目的 pom.xml 文件中添加 MyBatis-Plus 的依赖。如果你已经使用了 MyBatis-Plus,则可以跳过这一步。

(2) 配置分页插件

在 MyBatis-Plus 的配置类中,配置分页插件 PaginationInterceptor

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MyBatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor());return interceptor;}
}

(3)定义 Mapper 接口

 在 MyBatis-Plus 中,Mapper 接口继承自 BaseMapper,并可以使用 BaseMapper 提供的分页查询方法。

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.User;public interface UserMapper extends BaseMapper<User> {// MyBatis-Plus 的 BaseMapper 已经提供了分页查询方法,无需额外定义
}

(4)实现Service层

在 Service 层中,使用 MyBatis-Plus 提供的分页查询方法。

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;@Service
public class UserService extends ServiceImpl<UserMapper, User> {// 使用 MyBatis-Plus 的分页查询方法public IPage<User> getPage(Page<User> page, String condition) {// 使用条件构造器构建查询条件QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.like("name", condition);// 调用 MyBatis-Plus 的分页查询方法return userMapper.selectPage(page, queryWrapper);}
}

(5)Controller层

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/users")public IPage<User> getUsers(@RequestParam String condition,@RequestParam int current,@RequestParam int size) {// 创建分页对象Page<User> page = new Page<>(current, size);// 调用 Service 层方法return userService.getPage(page, condition);}
}
http://www.dtcms.com/wzjs/444130.html

相关文章:

  • 织梦网站版本宁波seo网络优化公司
  • 网站色彩搭配技巧国际新闻界期刊
  • 模板下载免费网站南宁seo计费管理
  • 网站详情一般是什么公司做seo的概念
  • 简洁的一家设计公司网站作品展示网页模板html5+css3全站下载免费网页制作网站
  • 品牌设计公司网站源码佛山网站建设正规公司
  • 朝阳网站建设培训搜索引擎网站推广如何优化
  • 做软件挣钱的网站教育培训机构前十名
  • 大岭山网站仿做被忽悠去做网销了
  • iis 设置网站不能访问百度站长工具添加不了站点
  • 专业做鞋子网站百度推广助手电脑版
  • 亚马逊跨境电商平台介绍搜索seo优化
  • 长春建站公司创建网站教程
  • 新郑做网站优化seo交流
  • 企业网站百度认证seo在线诊断工具
  • 网站制作技术使用说明百度百度一下你就知道主页
  • 购物网站开发教程视频推介网
  • 那个网站都有做莱的图片可口可乐搜索引擎营销案例
  • 全国疫情中高风险地区名单最新seo蜘蛛屯
  • 做58一样的网站品牌策划方案
  • 深圳品牌网站建设服务费用跨境电商平台哪个最好最可靠
  • 网站建设自建服务器西安网站建设
  • 做优化的网站用什么空间好痘痘怎么去除有效果
  • 寺庙做网站小程序开发流程详细
  • 个性化定制客户和网站建设软文写作的技巧
  • 营销型网站设计招聘网络推广怎么做好
  • 行业网站建设运营保定seo网络推广
  • 网站建设技术可行性分析爱链接购买链接
  • 旅行社手机网站建设哪里有正规的电商培训班
  • 教育行业网站模板宣传软文是什么意思