一.Mybatis动态分页
什么是动态分页:
MyBatis是Java中一种持久层框架,它提供了许多数据库操作的便利性。在使用MyBatis进行数据查询时,动态分页是一种常见的需求。
动态分页是指根据用户的请求动态生成数据库查询语句,以满足不同的分页需求。具体来说,动态分页通过在查询语句中添加limit和offset来实现。limit表示每页查询的记录数,offset表示查询结果的偏移量。
在MyBatis中,可以使用动态SQL语句来实现动态分页。动态SQL语句是一种可以根据条件决定是否包含某一段SQL语句的技术。MyBatis提供了一些标签和函数来支持动态SQL语句的编写,比如if、choose、when、otherwise等。
使用MyBatis实现动态分页的步骤如下:
在SQL映射文件中定义查询语句,根据需要使用动态SQL语句。
在查询语句中使用limit和offset来实现分页。
在Java代码中调用MyBatis的分页方法,传入分页参数。
MyBatis会根据传入的分页参数动态生成查询语句,返回分页结果。
总结来说,MyBatis的动态分页可以根据用户的需求动态生成查询语句,实现灵活的数据分页操作。
导入pom依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zking</groupId> <artifactId>mybatis1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>mybatis1 Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- ********************** junit单元测试依赖 ********************** --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <!-- <scope>test</scope>--> </dependency> <!-- ********************** Java Servlet API ********************** --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0</version> <scope>provided</scope> </dependency> <!-- ********************** Mybatis依赖 ********************** --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!-- ********************** Mysql JDBC驱动 ********************** --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> <!-- ********************** 日志配置 ********************** --> <!--记得修改mybatis.cfg.xml添加如下内容--> <!--<setting name="logImpl" value="LOG4J2"/>--> <!--核心log4j2jar包--> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.9.1</version> </dependency> <!--web工程需要包含log4j-web,非web工程不需要--> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-web</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies> <build> <finalName>mybatis1</finalName> <resources> <!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题--> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <!--解决mybatis-generator-maven-plugin运行时没有将jdbc.properites文件放入target文件夹的问题--> <resource> <directory>src/main/resources</directory> <includes> <include>jdbc.properties</include> <include>*.xml</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <dependencies> <!--使用Mybatis-generator插件不能使用太高版本的mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> </dependencies> <configuration> <overwrite>true</overwrite> </configuration> </plugin> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </build> </project>
配置拦截器
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 引入外部配置文件 --> <properties resource="jdbc.properties"/> <settings> <setting name="logImpl" value="LOG4J2"/> </settings> <!-- 别名 --> <typeAliases> <!--<typeAlias type="com.javaxl.model.Book" alias="Book"/>--> </typeAliases> <!--配置拦截器--> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins> <!-- 配置mybatis运行环境 --> <environments default="development"> <environment id="development"> <!-- type="JDBC" 代表使用JDBC的提交和回滚来管理事务 --> <transactionManager type="jdbc"/> <!-- mybatis提供了3种数据源类型,分别是:POOLED,UNPOOLED,JNDI --> <!-- POOLED 表示支持JDBC数据源连接池 --> <!-- UNPOOLED 表示不支持数据源连接池 --> <!-- JNDI 表示支持外部数据源连接池 --> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/zking/mapper/BookMapper.xml"/> </mappers> </configuration>
编写Bookmapper文件
配置pageBean文件
package com.zking.utils; import java.io.Serializable; import java.util.Map; import javax.servlet.http.HttpServletRequest; public class PageBean implements Serializable { private static final long serialVersionUID = 2422581023658455731L; //页码 private int page=1; //每页显示记录数 private int rows=10; //总记录数 private int total=0; //是否分页 private boolean isPagination=true; //上一次的请求路径 private String url; //获取所有的请求参数 private Map<String,String[]> map; public PageBean() { super(); } //设置请求参数 public void setRequest(HttpServletRequest req) { String page=req.getParameter("page"); String rows=req.getParameter("rows"); String pagination=req.getParameter("pagination"); this.setPage(page); this.setRows(rows); this.setPagination(pagination); this.url=req.getContextPath()+req.getServletPath(); this.map=req.getParameterMap(); } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Map<String, String[]> getMap() { return map; } public void setMap(Map<String, String[]> map) { this.map = map; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public void setPage(String page) { if(null!=page&&!"".equals(page.trim())) this.page = Integer.parseInt(page); } public int getRows() { return rows; } public void setRows(int rows) { this.rows = rows; } public void setRows(String rows) { if(null!=rows&&!"".equals(rows.trim())) this.rows = Integer.parseInt(rows); } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public void setTotal(String total) { this.total = Integer.parseInt(total); } public boolean isPagination() { return isPagination; } public void setPagination(boolean isPagination) { this.isPagination = isPagination; } public void setPagination(String isPagination) { if(null!=isPagination&&!"".equals(isPagination.trim())) this.isPagination = Boolean.parseBoolean(isPagination); } /** * 获取分页起始标记位置 * @return */ public int getStartIndex() { //(当前页码-1)*显示记录数 return (this.getPage()-1)*this.rows; } /** * 末页 * @return */ public int getMaxPage() { int totalpage=this.total/this.rows; if(this.total%this.rows!=0) totalpage++; return totalpage; } /** * 下一页 * @return */ public int getNextPage() { int nextPage=this.page+1; if(this.page>=this.getMaxPage()) nextPage=this.getMaxPage(); return nextPage; } /** * 上一页 * @return */ public int getPreivousPage() { int previousPage=this.page-1; if(previousPage<1) previousPage=1; return previousPage; } @Override public String toString() { return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", isPagination=" + isPagination + "]"; } }
配置BookBiz接口类
package com.zking.biz; import com.zking.model.Book; import com.zking.utils.PageBean; import org.apache.ibatis.annotations.Param; import java.util.List; import java.util.Map; public interface BookBiz { int deleteByPrimaryKey(Integer bid); int insert(Book record); int insertSelective(Book record); Book selectByPrimaryKey(Integer bid); int updateByPrimaryKeySelective(Book record); int updateByPrimaryKey(Book record); List<Book> selectByBids(List bids); List<Book> like1( String bname); List<Book> like2( String bname); List<Book> like3( String bname); List<Book> list01(); List<Book> list02(); Map list03(Map map); List<Map> list04(Map map); List bname1 (Integer bid); List<String> bname2( String bname); List<Book> like4(String bname, PageBean pageBean); }
配置BookBizImpl实现接口类
编写实现类demo
测试结果
不走插件,不会分页
二.Mybatis的特殊字符
pojo/entity/model:描述数据库表对应的实体类
vo:view object 视图对象:专门用来展示的 Java.util.Map vo.OrderItemVo Order order
dto:接受参数的
编写一个BookDto
package com.zking.dto; import com.zking.model.Book; /** * @author bing人 * @site * @company xy集团 * @create 2023-08-24 16:13 */ public class BookDto extends Book { private float min; private float max; public float getMin() { return min; } public void setMin(float min) { this.min = min; } public float getMax() { return max; } public void setMax(float max) { this.max = max; } }
编写BookMapper.xml
大于最小值小于最大值 ,一般都用第二种,输出结果都相同
编写BookMapper
package com.zking.mapper; import com.zking.dto.BookDto; import com.zking.model.Book; import org.apache.ibatis.annotations.Param; import java.util.List; import java.util.Map; public interface BookMapper { int deleteByPrimaryKey(Integer bid); int insert(Book record); int insertSelective(Book record); Book selectByPrimaryKey(Integer bid); int updateByPrimaryKeySelective(Book record); int updateByPrimaryKey(Book record); List<Book> selectByBids(@Param("bids") List bids); List<Book> like1(@Param("bname") String bname); List<Book> like2(@Param("bname") String bname); List<Book> like3(@Param("bname") String bname); List<Book> list01(); List<Book> list02(); Map list03(Map map); List<Map> list04(Map map); List<Book> bname1(@Param("bid") Integer bid); List<String> bname2(@Param("bname") String bname); List<Book> like4(@Param("bname") String bname); List<Book> querByMinMax(BookDto bookDto); }
编写接口类
编写接口实现类
编写测试类