当前位置: 首页 > 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.sadfv.cn/news/153147/

相关文章:

  • 浙江建设职业技术学院塘栖校区网站免费推广手段有哪些
  • 网站建设单位是什么意思网站做的支付宝接口
  • h5网站开发平台如何在百度上建网站
  • 枣庄建网站的公司电子商务网站建设应用
  • 网站开发要求让别人做网站注意事项
  • 网站分析表鄂尔多斯做网站
  • 网站视觉设计方案外贸响应式网站
  • 怎么开网站做网红办网站怎么赚钱
  • 视频网站怎么做算法网站的标准
  • 网站开发如何实现数据库的链接大连城市建设网站
  • 正安网站建设湘潭交通网站
  • 个人免费网站创建手机号交易网站源码
  • 武义做网站做ui要上那些网站
  • 网站优化标签photoshop制作网站海报
  • 普陀网站建设手机建网站推广
  • 平台网站很难做餐饮设计公司名字
  • 东莞seo网站优化方式太仓公司网站建设电话
  • 专门做简历的网站软件大连投诉网站
  • 自己做的网站怎么植入erp比较好的ui设计网站
  • 网站防护空间网站开发需解决的难题
  • 网站建设经典文章网站透明效果
  • 便民网站开发怎么做qq代刷网站
  • 学校网站制作方案ppt设计接单
  • 企业网站建设用什么网站百度忽然搜索不到
  • tp框架做餐饮网站山西推广型网站制作
  • wordpress 多站点教程宁夏网站制作哪家好
  • 溧阳企业网站建设wordpress文章页面修改
  • 国土网站建设自查报告网站线框图软件
  • APP加网站建设预算多少钱金色 网站 模板
  • 长安高端装备网站设计公司seo是什么?