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

python免费自学网站中国建设银行官网的网站首页

python免费自学网站,中国建设银行官网的网站首页,百度云wordpress教程视频,济南华企立方 网站前言 MybatisPlus的分页插件有一点非常不好#xff0c;就是要传入一个IPage#xff0c;别看这个IPage没什么大不了的#xff0c;最多多写一两行代码#xff0c;可这带来一个问题#xff0c;即使用xml的查询没法直接取对象里面变量的值了#xff0c;得Param指定xml中的变…前言 MybatisPlus的分页插件有一点非常不好就是要传入一个IPage别看这个IPage没什么大不了的最多多写一两行代码可这带来一个问题即使用xml的查询没法直接取对象里面变量的值了得Param指定xml中的变量名才行得写#{search.name}而不是#{name}这也太不优雅了可以说是相当的不优雅 我之前就想过一些办法解决这个问题比如使用PageHelper这玩意更不省心Page只会被第一次查询消费而在我项目中分页有一大堆的前置查询比如权限查询最多是否存在类查询较多以及其他一些前置查询业务。这往往会使得PageHelper被提前消费列表依旧返回所有内容这问题经常让人猝不及防让程序猿苦不堪言。因此PageHelper方案也被我放弃了最终还是打算自己实现一个分页插件替换MP自己的分页插件。 设计思路 作为一名曾经的Android前端程序猿Context模式对我来说再熟悉不过了可以说是形影不离即将几乎所有页面要用到的信息都放置到Context上下文中那我对于后端请求来说不也可以这么做吗将所有接口请求以及过程相关信息放到Context创建的对象中对象放到线程中随用随取只要拿到Context意味着拿到了一切跟Android的Context一样当然这玩意必须结合MP的分页插件和PageHelper的优点避免其自身的缺陷。 效果展示 图上为Kotlin代码Android程序猿必备实现分页仅需2行 第一行开启分页说明下一个请求是需要执行分页的 第二行进行查询结果返回的只是一个List分页信息呢全保存在Context对象中了。 返回结果如上图所示为了节约服务器带宽我这边的返回参数全部使用单个字母表示其中p就是page信息pnpageNumpspageSizetctotalCounttptotalPage 当然这玩意和PageHelper一样只能负责一次分页查询当然一个接口也只需要一次分页查询 不服来辩 直接上代码 代码分为前中后三个部分 前期准备Context 准备Context阶段我是在Aspect中进行的切面为Controller方法在执行Controller方法前初始化一个Context对象并将其放到map中Key为当前Thread对象Value为Context这里的代码过于复杂且涉及到token校验这里我就不放完整的出来了以免我的服务器遭到攻击。 val context Context()val thread Thread.currentThread()threadContextMap[thread] context 反正大概就这意思Context中当然也包含了所有入参信息包括了pageNum、pageSize、totalCount、totalPage等等。 中期准备xml、分页插件 由于项目中大量查询都是基于xml的包含很多子查询和join查询不可能都用QueryWrapper查询因此xml的简洁化是必须的。我这里用的示例查询xml为 select idfindByList resultTypecom.itdct.server.admin.example.vo.ExampleListVoselect t.* from test_example as twhereif testname ! null and name ! and t.name #{name}/ifif testnumber ! nulland t.number #{number}/ifif testkeyword ! null and keyword ! and t.name like concat(%,#{keyword},%)/ifif teststartTime ! nulland t.create_time gt; #{startTime}/ifif testendTime ! nulland t.create_time lt; #{endTime}/if/whereif testorderBy nullorder by t.create_time desc/ifif testorderBy ! nullorder by ${orderBy}/if/select 查询的Mapper为 fun findByList(query: ExampleQo): ListExampleListVo 可以发现查询方法不包含任何Paramif中的变量也没有xxx.fieldName甚至用ctrl左键点击#{变量}还能跳转到类中相应的成员变量这就是我想要实现的效果。 然后就是分页插件了这个插件我还是基于原来的MP的分页插件只需要对其进行稍加修改即可为我所用。 package com.itdct.server.admin.config;import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.core.metadata.OrderItem; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.PluginUtils; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.pagination.DialectModel; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.IDialect; import com.itdct.server.common.dto.Context;import org.apache.ibatis.cache.CacheKey; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.ParameterMapping; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds;import java.sql.SQLException; import java.util.List; import java.util.Map;/*** author DCT* version 1.0* date 2023/11/10 14:53:24* description*/ public class ContextPaginationInnerInterceptor extends PaginationInnerInterceptor {protected MapThread, Context threadContextMap;public ContextPaginationInnerInterceptor(DbType dbType) {super(dbType);}public ContextPaginationInnerInterceptor(IDialect dialect) {super(dialect);}public ContextPaginationInnerInterceptor(DbType dbType, MapThread, Context threadContextMap) {super(dbType);this.threadContextMap threadContextMap;}Overridepublic boolean willDoQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {// INFO: DCT: 2023/12/5 获取到当前线程的上下文对象Context context threadContextMap.get(Thread.currentThread());if (context null) {return true;}// INFO: DCT: 2023/12/5 不启动分页直接跳过boolean startPage context.isStartPage();if (!startPage) {return true;}// INFO: DCT: 2023/12/5 这个page就是MP的分页Page Page page context.getPage();if (page null) {return true;}long size page.getSize();if (size 0) {return true;}// INFO: DCT: 2023/12/5 以下为原来的MP分页插件代码 BoundSql countSql;MappedStatement countMs buildCountMappedStatement(ms, page.countId());if (countMs ! null) {countSql countMs.getBoundSql(parameter);} else {countMs buildAutoCountMappedStatement(ms);String countSqlStr autoCountSql(page, boundSql.getSql());PluginUtils.MPBoundSql mpBoundSql PluginUtils.mpBoundSql(boundSql);countSql new BoundSql(countMs.getConfiguration(), countSqlStr, mpBoundSql.parameterMappings(), parameter);PluginUtils.setAdditionalParameter(countSql, mpBoundSql.additionalParameters());}CacheKey cacheKey executor.createCacheKey(countMs, parameter, rowBounds, countSql);ListObject result executor.query(countMs, parameter, rowBounds, resultHandler, cacheKey, countSql);long total 0;if (CollectionUtils.isNotEmpty(result)) {// 个别数据库 count 没数据不会返回 0Object o result.get(0);if (o ! null) {total Long.parseLong(o.toString());}}page.setTotal(total);long totalPage total / page.getSize();if (total % page.getSize() ! 0) {totalPage;}page.setPages(totalPage);return continuePage(page);}Overridepublic void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {Context context threadContextMap.get(Thread.currentThread());if (context null) {return;}boolean startPage context.isStartPage();if (!startPage) {return;}// INFO: DCT: 2023/12/5 这个page就是MP的分页Page Page page context.getPage();if (page null) {return;}long size page.getSize();if (size 0) {return;}// 处理 orderBy 拼接boolean addOrdered false;String buildSql boundSql.getSql();ListOrderItem orders page.orders();if (CollectionUtils.isNotEmpty(orders)) {addOrdered true;buildSql this.concatOrderBy(buildSql, orders);}// size 小于 0 且不限制返回值则不构造分页sqlLong _limit page.maxLimit() ! null ? page.maxLimit() : maxLimit;if (page.getSize() 0 null _limit) {if (addOrdered) {PluginUtils.mpBoundSql(boundSql).sql(buildSql);}return;}handlerLimit(page, _limit);IDialect dialect findIDialect(executor);final Configuration configuration ms.getConfiguration();DialectModel model dialect.buildPaginationSql(buildSql, page.offset(), page.getSize());PluginUtils.MPBoundSql mpBoundSql PluginUtils.mpBoundSql(boundSql);ListParameterMapping mappings mpBoundSql.parameterMappings();MapString, Object additionalParameter mpBoundSql.additionalParameters();model.consumers(mappings, configuration, additionalParameter);mpBoundSql.sql(model.getDialectSql());mpBoundSql.parameterMappings(mappings);// INFO: DCT: 2023/12/5 利用完后置为false context.setStartPage(false);} }完整代码如上面所示其中绝大部分都是MP原来的分页插件里的代码我只是对其稍加修改而已。 后期返回给前端 有了Context对象真的可以为所欲为哦successPage方法如下 public T RespPageVoT successPage(ListT pageData) {Context context getContext();Page page context.getPage();if (page ! null) {return new RespPageVoT(pageData, page.getCurrent(), page.getSize(), page.getTotal(), page.getPages());} else {log.warn(page is null!);return new RespPageVoT(pageData, 0L, 0L, 0L, 0L);}}public Context getContext() {Context context threadContextMap.get(Thread.currentThread());return context;} 处于BaseService的代码还是Java写的没有全面Kotlin化由于Context对象中存有MP的Page对象因此可以直接从Page对象中拿到上次执行的分页数据直接放入返回参即可。 小结 至此升级版分页插件和使用就此完成上面代码其实也只是我自己项目的一小部分而已起到的也只是一个抛砖引玉的作用欢迎大家在评论区与我讨论交流我会尝试将这个插件做得更好更加优雅。
http://www.huolong8.cn/news/153147/

相关文章:

  • 设计一个个人网站的具体步骤图书销售网站网页设计模板
  • 网站全屏图片怎么做的简述网站开发平台及常用开发工具
  • 广州网站开发设计佛山市住房建设局网站
  • 网站手机版开发做网站 工商 非法经营
  • 甘肃省建设银行校园招聘网站wordpress主题 亚马逊
  • 广告公司网站设计策划重庆seo优化公司哪家好
  • 网站搭建心得体会大连头条热点新闻
  • 网站用图怎么做文件小质量高生态建设网站
  • 免费的求职简历模板网站软件优化网站
  • 上海市城乡建设管理局网站佛山房地产网站建设
  • 西昌网站开发公司wordpress主题库
  • 建信建设投资有限公司网站wordpress wp_get_post_tags
  • 品牌网网站建设用软件建网站
  • 方城网站制作网站建设公司好发信息网
  • 合肥专业做网站公司北京网站建设定制型报价
  • 企业网站建设比较调查怎么写上什么网站做会计教育
  • 网站开发方式有哪些网上全网推广
  • 宿迁网站建设报价jsp做的网站可以用的
  • 遵义网站设计制作网站建设网站的原则
  • 网站认证免费wordpress外贸网站好用的模板
  • 哈尔滨网站域名部门seo怎么做自己的网站
  • 网站的目的和意义wordpress 百度翻译插件
  • 网站建设单词个人注册一个小公司要多少钱
  • 算命网站建设网页设计制作论文
  • 中国交通建设监理协会网站打不开微信小程序网站建设定制
  • 做网站怎么加入索引功能怎样做公司网站建设
  • wordpress导航网站wordpress点击网页效果
  • 做去自己的网站nft制作网站
  • 做网站需要什么特色怎么推广自己的微信号
  • 做十个网站买一个app需要多少钱