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

wamp可以做视频网站吗石家庄网络推广优化

wamp可以做视频网站吗,石家庄网络推广优化,一篇网站设计小结,一级建造师找工作网站目录 一、简单分页查询——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/158386.html

相关文章:

  • 天津哪家做企业网站蜜雪冰城推广软文
  • 京东淘宝网站是怎么做的知乎推广优化
  • php网站开发步骤百度热词指数
  • jsp酒店预订网站开发济南百度开户电话
  • 暖色系网站模板免费发布活动的平台
  • 苏州大型网站建设公司接app推广的单子在哪接
  • 秦皇岛网站制作价格seo搜索引擎优化价格
  • 一男一女做那个的动漫视频网站首页
  • 松江做微网站百度一下你就知道下载安装
  • 织梦更换网站模板发稿吧
  • 门户系统设计成都做整站优化
  • 网站建实例建立个人网站
  • 上海网站建设网页制作app优化建议
  • 宁波怎么做网站排名优化宁波seo推广定制
  • 手机赌博澳门网站开发宁德市是哪个省
  • 全国十大网站建设公司排名网站推广怎样做
  • 怎样申请网站空间南昌seo推广
  • wordpress 编辑器增加翻译按钮seo线下培训课程
  • 编程猫官方网站入口厦门seo测试
  • 建公司网站哪家公司好百度推广怎么收费标准案例
  • 电子书网站 自己做网站制作定制
  • dw做网站设计去除痘痘怎么有效果
  • 专做品质游的网站北京seo推广公司
  • php怎么做网站后台赣州网站seo
  • 网站开发相关技术发展手游代理加盟哪个平台最强大
  • 建设摩托车官网商城2015优化百度seo
  • 上海建设网站浦东新区污水管网工程填写电话的广告
  • 自己做的网站怎么接入网页游戏长沙seo优化报价
  • 做经营行网站需要什么手续网站设计制作的服务怎么样
  • wordpress特色图片外链酒泉网站seo