mybatis-plus使用问题小结
更新时间:2022年3月1日 13:10 点击:266 作者:别动我的猫
一、多表联合分页查询
1.多表联合查询结果集建议使用VO类,当然也可以使用resultMap
package com.cjhx.tzld.entity.vo; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.cjhx.tzld.entity.TContent; import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; @Data @ApiModel(value="TContentVo", description="内容池多表联合数据对象") public class TContentVo extends TContent { @ApiModelProperty(value = "编号") private Integer cid; @ApiModelProperty(value = "内容标题") private String title; @ApiModelProperty(value = "作者Id") @TableField("authorId") private Integer authorId; @ApiModelProperty(value = "时间") @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") //返回时间类型 @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") //接收时间类型 private Date time; @ApiModelProperty(value = "内容") private String content; @ApiModelProperty(value = "作者姓名") private String author; @ApiModelProperty(value = "话题") private String topic; @ApiModelProperty(value = "模块编号") private int moduleNum; @ApiModelProperty(value = "模块") private String module; public TContentVo() { } public TContentVo(Integer cid, String title, Date time, String content, String author, String topic, int moduleNum) { this.cid = cid; this.title = title; this.time = time; this.content = content; this.author = author; this.topic = topic; this.moduleNum = moduleNum; }
2.controller
package com.cjhx.tzld.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.cjhx.tzld.common.Result; import com.cjhx.tzld.entity.TContent; import com.cjhx.tzld.entity.TContentRelationFund; import com.cjhx.tzld.entity.TTopicPk; import com.cjhx.tzld.entity.vo.TContentVo; import com.cjhx.tzld.service.TContentService; import io.swagger.annotations.*; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.Date; import java.util.List; /** * @since 2022-02-28 */ @RestController @RequestMapping("/content") @Api("内容池模块") public class ContentController { @Resource private TContentService contentService; @ApiImplicitParams({ @ApiImplicitParam(name = "cid",value = "cid",dataType = "int",defaultValue = "0",required = false), @ApiImplicitParam(name = "title",value = "标题",dataType = "String",defaultValue = "",required = false), @ApiImplicitParam(name = "author",value = "作者姓名",dataType = "String",defaultValue = "",required = false), @ApiImplicitParam(name = "time",value = "发布时间",dataType = "Date",defaultValue = "",required = false), @ApiImplicitParam(name = "content",value = "内容",dataType = "String",defaultValue = "",required = false), @ApiImplicitParam(name = "topic",value = "话题",dataType = "String",defaultValue = "",required = false), @ApiImplicitParam(name = "moduleNum",value = "投放模块 1热点速递 2基会直达",dataType = "int",defaultValue = "",required = false), @ApiImplicitParam(name = "pageIndex",value = "页码",dataType = "int",defaultValue = "1",required = false), @ApiImplicitParam(name = "pageSize",value = "每页数量",dataType = "int",defaultValue = "10",required = false) }) @ApiResponses({ @ApiResponse(code = 200,message = "OK",response = TContent.class) @ApiOperation(value="分页获取内容接口(Web端)", notes="支持多条件查询",httpMethod = "GET") @RequestMapping(value = "/getContentPage",method = RequestMethod.GET) public Result getContentPage(@RequestParam(defaultValue = "0",required = false) int cid, @RequestParam(defaultValue = "",required = false) String title, @RequestParam(defaultValue = "",required = false) String author, @RequestParam(required = false) Date time, @RequestParam(defaultValue = "",required = false) String content, @RequestParam(defaultValue = "",required = false) String topic, @RequestParam(defaultValue = "0",required = false) int moduleNum, @RequestParam(defaultValue = "1",required = false) int pageIndex, @RequestParam(defaultValue = "10",required = false) int pageSize) throws Exception{ try { IPage<TContentVo> byPage = contentService.findByPage(new Page<TContentVo>(pageIndex, pageSize),new TContentVo(cid, title, time, content, author, topic, moduleNum)); return Result.success(byPage); }catch (Exception e){ return Result.serviceFail(e.getMessage()); } } }
3.service
package com.cjhx.tzld.service.impl; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.cjhx.tzld.common.PageUtil; import com.cjhx.tzld.entity.TContent; import com.cjhx.tzld.entity.vo.TContentVo; import com.cjhx.tzld.mapper.TContentMapper; import com.cjhx.tzld.service.TContentService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Date; /** * @since 2022-02-28 */ @Service public class TContentServiceImpl extends ServiceImpl<TContentMapper, TContent> implements TContentService { @Resource private TContentMapper tContentMapper; @Override public IPage<TContentVo> findByPage(Page<TContentVo> page, TContentVo contentVo) { return tContentMapper.findByPage(page,contentVo); } }
4.mapper
package com.cjhx.tzld.mapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.cjhx.tzld.entity.TContent; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.cjhx.tzld.entity.vo.TContentVo; import org.apache.ibatis.annotations.Param; /** * @since 2022-02-28 */ public interface TContentMapper extends BaseMapper<TContent> { IPage<TContentVo> findByPage(Page<TContentVo> page, @Param("contentVo") TContentVo contentVo); }
5.mapper.xml,注意入参contentVo
<?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.cjhx.tzld.mapper.TContentMapper"> <select id="findByPage" resultType="com.cjhx.tzld.entity.vo.TContentVo" parameterType="com.cjhx.tzld.entity.vo.TContentVo"> SELECT t.`cid`,t.`authorId`,a.`name`,t.`title`,t.`time`,t.`content`,p.`topic`,h.`title` AS `module` FROM `t_content` t LEFT JOIN `t_author` a ON a.`aid`=t.`authorId` LEFT JOIN `t_topic_pk` p ON p.`cid` = t.`cid` LEFT JOIN `t_hot_express` h ON h.`cid` = t.`cid` UNION ALL SELECT t.`cid`,t.`authorId`,a.`name`,t.`title`,t.`time`,t.`content`,p.`topic`,f.`title` AS `module` LEFT JOIN `t_fund_point` f ON f.`cid` = t.`cid` <where> 1=1 <if test="contentVo.cid > 0"> and cid = #{contentVo.cid}</if> <if test="contentVo.title != null and contentVo.title != ''"> and t.title like concat('%', #{contentVo.title}, '%')</if> <if test="contentVo.author != null and contentVo.author != ''"> and a.author like concat('%', #{contentVo.author}, '%')</if> <if test="contentVo.time != null"> and t.time =${contentVo.time}</if> <if test="contentVo.content != null and contentVo.content != ''"> and t.content like concat('%', #{contentVo.content}, '%')</if> <if test="contentVo.topic != null and contentVo.topic != ''"> and p.topic like concat('%', #{contentVo.topic}, '%')</if> <if test="contentVo.moduleNum == 1"> and f.currentState = -1</if> <if test="contentVo.moduleNum == 2"> and h.currentState = -1</if> </where> order by time desc </select> </mapper>
二、找不到mapper
首先排除@MapperScan("com.cjhx.tzld.mapper")已添加
1.首先配置文件扫描,mapper-locations:classpath:/com/cjhx/tzld/mapper/xml/*.xml
mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl mapper-locations: classpath:/com/cjhx/tzld/mapper/xml/*.xml
2.在pom.xml的<build>添加xml资源
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <!--引入mapper对应的xml文件--> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
到此这篇关于mybatis-plus使用问题汇总的文章就介绍到这了,更多相关mybatis-plus使用内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!
原文出处:https://www.cnblogs.com/zeussbook/p/15949379.html
相关文章
- 有时为了网站安全和版权问题,会对自己写的php源码进行加密,在php加密技术上最常用的是zend公司的zend guard 加密软件,现在我们来图文讲解一下。 下面就简单说说如何...2016-11-25
- 这篇文章主要介绍了mybatis-plus 表名添加前缀的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-08-26
- 这篇文章主要介绍了mybatis-plus 返回部分字段的解决方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-10-02
- ps软件是现在很多人都会使用到的,HSL面板在ps软件中又有着非常独特的作用。这次文章就给大家介绍下ps怎么使用HSL面板,还不知道使用方法的下面一起来看看。  ...2017-07-06
- 这篇文章主要介绍了MyBatis-Plus自动填充功能失效导致的原因及解决,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-04
- 这篇文章主要介绍了mybatis-plus 处理大数据插入太慢的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-18
- 许多的朋友对于Plesk控制面板应用不是非常的了解特别是英文版的Plesk控制面板,在这里小编整理了一些关于Plesk控制面板常用的使用方案整理,具体如下。 本文基于Linu...2016-10-10
使用insertAfter()方法在现有元素后添加一个新元素
复制代码 代码如下: //在现有元素后添加一个新元素 function insertAfter(newElement, targetElement){ var parent = targetElement.parentNode; if (parent.lastChild == targetElement){ parent.appendChild(newEl...2014-05-31jQuery 1.9使用$.support替代$.browser的使用方法
jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support 。 在更新的 2.0 版本中,将不再支持 IE 6/7/8。 以后,如果用户需要支持 IE 6/7/8,只能使用 jQuery 1.9。 如果要全面支持 IE,并混合...2014-05-31使用percona-toolkit操作MySQL的实用命令小结
1.pt-archiver 功能介绍: 将mysql数据库中表的记录归档到另外一个表或者文件 用法介绍: pt-archiver [OPTION...] --source DSN --where WHERE 这个工具只是归档旧的数据,不会对线上数据的OLTP查询造成太大影响,你可以将...2015-11-24- 大概有如下步骤 新建项目Bejs 新建文件package.json 新建文件Gruntfile.js 命令行执行grunt任务 一、新建项目Bejs源码放在src下,该目录有两个js文件,selector.js和ajax.js。编译后代码放在dest,这个grunt会...2014-06-07
如何使用php脚本给html中引用的js和css路径打上版本号
在搜索引擎中搜索关键字.htaccess 缓存,你可以搜索到很多关于设置网站文件缓存的教程,通过设置可以将css、js等不太经常更新的文件缓存在浏览器端,这样访客每次访问你的网站的时候,浏览器就可以从浏览器的缓存中获取css、...2015-11-24- 一、下载 mysqlsla [root@localhost tmp]# wget http://hackmysql.com/scripts/mysqlsla-2.03.tar.gz--19:45:45-- http://hackmysql.com/scripts/mysqlsla-2.03.tar.gzResolving hackmysql.com... 64.13.232.157Conn...2015-11-24
- C#注释的一些使用方法浅谈,需要的朋友可以参考一下...2020-06-25
- 这篇文章主要介绍了解决mybatis-plus 查询耗时慢的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-07-04
使用mybatis-plus报错Invalid bound statement (not found)错误
这篇文章主要介绍了使用mybatis-plus报错Invalid bound statement (not found)错误,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-02安装和使用percona-toolkit来辅助操作MySQL的基本教程
一、percona-toolkit简介 percona-toolkit是一组高级命令行工具的集合,用来执行各种通过手工执行非常复杂和麻烦的mysql和系统任务,这些任务包括: 检查master和slave数据的一致性 有效地对记录进行归档 查找重复的索...2015-11-24- 目前,JSON已经成为最流行的数据交换格式之一,各大网站的API几乎都支持它。我写过一篇《数据类型和JSON格式》,探讨它的设计思想。今天,我想总结一下PHP语言对它的支持,这是开发互联网应用程序(特别是编写API)必须了解的知识...2015-10-30
- 基本思路: 通过使用jquery选择器得到对应表单的jquery对象,然后使用attr方法修改对应的action 示例程序一: 默认情况下,该表单会提交到page_one.html 点击button之后,表单的提交地址就会修改为page_two.html 复制...2014-06-07
- 无限级分类在开发中经常使用,例如:部门结构、文章分类。无限级分类的难点在于“输出”和“查询”,例如 将文章分类输出为<ul>列表形式; 查找分类A下面所有分类包含的文章。1.实现原理 几种常见的实现方法,各有利弊。其中...2015-10-23