Springboot如何使用mybatis實(shí)現(xiàn)攔截SQL分頁
新建一個(gè)類MyPageInterceptor.java(注意在springboot中要添加注解@Component)
package com.grand.p1upgrade.mapper.test; import java.sql.Connection; import java.util.Map; import java.util.Properties; import org.apache.ibatis.executor.parameter.ParameterHandler; import org.apache.ibatis.executor.statement.StatementHandler; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.SystemMetaObject; import org.springframework.stereotype.Component; @Component @Intercepts({@Signature(type=StatementHandler.class,method="prepare",args={Connection.class,Integer.class})}) public class MyPageInterceptor implements Interceptor { private int page; private int size; @SuppressWarnings("unused") private String dbType; @SuppressWarnings("unchecked") @Override public Object intercept(Invocation invocation) throws Throwable { System.out.println("plugin is running..."); StatementHandler statementHandler = (StatementHandler)invocation.getTarget(); MetaObject metaObject = SystemMetaObject.forObject(statementHandler); while(metaObject.hasGetter("h")){ Object object = metaObject.getValue("h"); metaObject = SystemMetaObject.forObject(object); } while(metaObject.hasGetter("target")){ Object object = metaObject.getValue("target"); metaObject = SystemMetaObject.forObject(object); } MappedStatement mappedStatement = (MappedStatement)metaObject.getValue("delegate.mappedStatement"); String mapId = mappedStatement.getId(); if(mapId.matches(".+ByPager$")){ ParameterHandler parameterHandler = (ParameterHandler)metaObject.getValue("delegate.parameterHandler"); Map<String, Object> params = (Map<String, Object>)parameterHandler.getParameterObject(); page = (int)params.get("page"); size = (int)params.get("size"); String sql = (String) metaObject.getValue("delegate.boundSql.sql"); sql += " limit "+(page-1)*size +","+size; metaObject.setValue("delegate.boundSql.sql", sql); } return invocation.proceed(); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { String limit = properties.getProperty("limit","10"); this.page = Integer.parseInt(limit); this.dbType = properties.getProperty("dbType", "mysql"); } }
添加測(cè)試TestMapper.java
package com.grand.p1upgrade.mapper.test; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Mapper; @Mapper public interface TestMapper { public List<Map<String,Object>> findByPager(Map<String, Object> params); public long count(); }
TestMapper.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.grand.p1upgrade.mapper.test.TestMapper"> <select id="findByPager" resultType="java.util.HashMap"> select * from p1project.sys_user </select> <select id="count" resultType="long"> select count(1) from p1project.sys_user </select> </mapper>
在調(diào)用TestMapper.findByPager傳遞參數(shù)的時(shí)候?qū)age和size傳入即可
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot使用校驗(yàn)框架validation校驗(yàn)的示例
這篇文章主要介紹了springboot使用校驗(yàn)框架validation校驗(yàn)的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02Springboot項(xiàng)目打war包docker包找不到resource下靜態(tài)資源的解決方案
今天小編就為大家分享一篇關(guān)于Springboot項(xiàng)目打war包docker包找不到resource下靜態(tài)資源的解決方案,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03SpringMVC整合,出現(xiàn)注解沒有起作用的情況處理
這篇文章主要介紹了SpringMVC整合,出現(xiàn)注解沒有起作用的情況及處理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-05-05SpringBoot常用注解@RestControllerAdvice詳解
這篇文章主要介紹了SpringBoot常用注解@RestControllerAdvice詳解,@RestControllerAdvice是一個(gè)組合注解,由@ControllerAdvice、@ResponseBody組成,而@ControllerAdvice繼承了@Component,因此@RestControllerAdvice本質(zhì)上是個(gè)Component,需要的朋友可以參考下2024-01-01Java?Chassis3負(fù)載均衡選擇器技術(shù)解密
這篇文章主要為大家介紹了Java?Chassis3負(fù)載均衡選擇器技術(shù)解密,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01selenium高效應(yīng)對(duì)Web頁面元素刷新的實(shí)例講解
今天小編就為大家分享一篇selenium高效應(yīng)對(duì)Web頁面元素刷新的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05關(guān)于maven打包時(shí)的報(bào)錯(cuò): Return code is: 501 , ReasonPhrase:HTTPS Requ
這篇文章主要介紹了關(guān)于maven打包時(shí)的報(bào)錯(cuò): Return code is: 501 , ReasonPhrase:HTTPS Required,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Java如何導(dǎo)出多個(gè)excel并打包壓縮成.zip文件
本文介紹了Java如何導(dǎo)出多個(gè)excel文件并將這些文件打包壓縮成zip格式,首先,需要從數(shù)據(jù)庫中獲取數(shù)據(jù)并導(dǎo)出到指定位置形成excel文件,接著,將這些數(shù)據(jù)分散到不同的excel文件中,最后,使用相關(guān)的Java工具類對(duì)這些excel文件進(jìn)行打包壓縮2024-09-09