经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
mybatis-plus使用问题小结
来源:jb51  时间:2022/3/1 13:39:37  对本文有异议

一、多表联合分页查询

1.多表联合查询结果集建议使用VO类,当然也可以使用resultMap

  1. package com.cjhx.tzld.entity.vo;
  2.  
  3. import com.baomidou.mybatisplus.annotation.IdType;
  4. import com.baomidou.mybatisplus.annotation.TableField;
  5. import com.baomidou.mybatisplus.annotation.TableId;
  6. import com.cjhx.tzld.entity.TContent;
  7. import com.fasterxml.jackson.annotation.JsonFormat;
  8. import io.swagger.annotations.ApiModel;
  9. import io.swagger.annotations.ApiModelProperty;
  10. import lombok.Data;
  11. import org.springframework.format.annotation.DateTimeFormat;
  12. import java.util.Date;
  13. @Data
  14. @ApiModel(value="TContentVo", description="内容池多表联合数据对象")
  15. public class TContentVo extends TContent {
  16. @ApiModelProperty(value = "编号")
  17. private Integer cid;
  18. @ApiModelProperty(value = "内容标题")
  19. private String title;
  20. @ApiModelProperty(value = "作者Id")
  21. @TableField("authorId")
  22. private Integer authorId;
  23. @ApiModelProperty(value = "时间")
  24. @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") //返回时间类型
  25. @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") //接收时间类型
  26. private Date time;
  27. @ApiModelProperty(value = "内容")
  28. private String content;
  29. @ApiModelProperty(value = "作者姓名")
  30. private String author;
  31. @ApiModelProperty(value = "话题")
  32. private String topic;
  33. @ApiModelProperty(value = "模块编号")
  34. private int moduleNum;
  35. @ApiModelProperty(value = "模块")
  36. private String module;
  37. public TContentVo() {
  38. }
  39. public TContentVo(Integer cid, String title, Date time, String content, String author, String topic, int moduleNum) {
  40. this.cid = cid;
  41. this.title = title;
  42. this.time = time;
  43. this.content = content;
  44. this.author = author;
  45. this.topic = topic;
  46. this.moduleNum = moduleNum;
  47. }

2.controller

  1. package com.cjhx.tzld.controller;
  2.  
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.cjhx.tzld.common.Result;
  7. import com.cjhx.tzld.entity.TContent;
  8. import com.cjhx.tzld.entity.TContentRelationFund;
  9. import com.cjhx.tzld.entity.TTopicPk;
  10. import com.cjhx.tzld.entity.vo.TContentVo;
  11. import com.cjhx.tzld.service.TContentService;
  12. import io.swagger.annotations.*;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RequestMethod;
  16. import org.springframework.web.bind.annotation.RequestParam;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import javax.annotation.Resource;
  19. import java.util.Date;
  20. import java.util.List;
  21. /**
  22. * @since 2022-02-28
  23. */
  24. @RestController
  25. @RequestMapping("/content")
  26. @Api("内容池模块")
  27. public class ContentController {
  28. @Resource
  29. private TContentService contentService;
  30. @ApiImplicitParams({
  31. @ApiImplicitParam(name = "cid",value = "cid",dataType = "int",defaultValue = "0",required = false),
  32. @ApiImplicitParam(name = "title",value = "标题",dataType = "String",defaultValue = "",required = false),
  33. @ApiImplicitParam(name = "author",value = "作者姓名",dataType = "String",defaultValue = "",required = false),
  34. @ApiImplicitParam(name = "time",value = "发布时间",dataType = "Date",defaultValue = "",required = false),
  35. @ApiImplicitParam(name = "content",value = "内容",dataType = "String",defaultValue = "",required = false),
  36. @ApiImplicitParam(name = "topic",value = "话题",dataType = "String",defaultValue = "",required = false),
  37. @ApiImplicitParam(name = "moduleNum",value = "投放模块 1热点速递 2基会直达",dataType = "int",defaultValue = "",required = false),
  38. @ApiImplicitParam(name = "pageIndex",value = "页码",dataType = "int",defaultValue = "1",required = false),
  39. @ApiImplicitParam(name = "pageSize",value = "每页数量",dataType = "int",defaultValue = "10",required = false)
  40. })
  41. @ApiResponses({
  42. @ApiResponse(code = 200,message = "OK",response = TContent.class)
  43. @ApiOperation(value="分页获取内容接口(Web端)", notes="支持多条件查询",httpMethod = "GET")
  44. @RequestMapping(value = "/getContentPage",method = RequestMethod.GET)
  45. public Result getContentPage(@RequestParam(defaultValue = "0",required = false) int cid,
  46. @RequestParam(defaultValue = "",required = false) String title,
  47. @RequestParam(defaultValue = "",required = false) String author,
  48. @RequestParam(required = false) Date time,
  49. @RequestParam(defaultValue = "",required = false) String content,
  50. @RequestParam(defaultValue = "",required = false) String topic,
  51. @RequestParam(defaultValue = "0",required = false) int moduleNum,
  52. @RequestParam(defaultValue = "1",required = false) int pageIndex,
  53. @RequestParam(defaultValue = "10",required = false) int pageSize) throws Exception{
  54. try {
  55. IPage<TContentVo> byPage = contentService.findByPage(new Page<TContentVo>(pageIndex, pageSize),new TContentVo(cid, title, time, content, author, topic, moduleNum));
  56. return Result.success(byPage);
  57. }catch (Exception e){
  58. return Result.serviceFail(e.getMessage());
  59. }
  60. }
  61. }

3.service

  1. package com.cjhx.tzld.service.impl;
  2.  
  3. import com.baomidou.mybatisplus.core.conditions.Wrapper;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.baomidou.mybatisplus.core.metadata.IPage;
  6. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7. import com.cjhx.tzld.common.PageUtil;
  8. import com.cjhx.tzld.entity.TContent;
  9. import com.cjhx.tzld.entity.vo.TContentVo;
  10. import com.cjhx.tzld.mapper.TContentMapper;
  11. import com.cjhx.tzld.service.TContentService;
  12. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  13. import org.springframework.stereotype.Service;
  14. import javax.annotation.Resource;
  15. import java.util.Date;
  16. /**
  17. * @since 2022-02-28
  18. */
  19. @Service
  20. public class TContentServiceImpl extends ServiceImpl<TContentMapper, TContent> implements TContentService {
  21. @Resource
  22. private TContentMapper tContentMapper;
  23. @Override
  24. public IPage<TContentVo> findByPage(Page<TContentVo> page, TContentVo contentVo) {
  25. return tContentMapper.findByPage(page,contentVo);
  26. }
  27. }

4.mapper

  1. package com.cjhx.tzld.mapper;
  2.  
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.cjhx.tzld.entity.TContent;
  7. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  8. import com.cjhx.tzld.entity.vo.TContentVo;
  9. import org.apache.ibatis.annotations.Param;
  10. /**
  11. * @since 2022-02-28
  12. */
  13. public interface TContentMapper extends BaseMapper<TContent> {
  14. IPage<TContentVo> findByPage(Page<TContentVo> page, @Param("contentVo") TContentVo contentVo);
  15. }

5.mapper.xml,注意入参contentVo

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.cjhx.tzld.mapper.TContentMapper">
  4.  
  5. <select id="findByPage" resultType="com.cjhx.tzld.entity.vo.TContentVo" parameterType="com.cjhx.tzld.entity.vo.TContentVo">
  6. SELECT t.`cid`,t.`authorId`,a.`name`,t.`title`,t.`time`,t.`content`,p.`topic`,h.`title` AS `module`
  7. FROM `t_content` t
  8. LEFT JOIN `t_author` a ON a.`aid`=t.`authorId`
  9. LEFT JOIN `t_topic_pk` p ON p.`cid` = t.`cid`
  10. LEFT JOIN `t_hot_express` h ON h.`cid` = t.`cid`
  11. UNION ALL
  12. SELECT t.`cid`,t.`authorId`,a.`name`,t.`title`,t.`time`,t.`content`,p.`topic`,f.`title` AS `module`
  13. LEFT JOIN `t_fund_point` f ON f.`cid` = t.`cid`
  14. <where>
  15. 1=1
  16. <if test="contentVo.cid > 0"> and cid = #{contentVo.cid}</if>
  17. <if test="contentVo.title != null and contentVo.title != ''"> and t.title like concat('%', #{contentVo.title}, '%')</if>
  18. <if test="contentVo.author != null and contentVo.author != ''"> and a.author like concat('%', #{contentVo.author}, '%')</if>
  19. <if test="contentVo.time != null"> and t.time =${contentVo.time}</if>
  20. <if test="contentVo.content != null and contentVo.content != ''"> and t.content like concat('%', #{contentVo.content}, '%')</if>
  21. <if test="contentVo.topic != null and contentVo.topic != ''"> and p.topic like concat('%', #{contentVo.topic}, '%')</if>
  22. <if test="contentVo.moduleNum == 1"> and f.currentState = -1</if>
  23. <if test="contentVo.moduleNum == 2"> and h.currentState = -1</if>
  24. </where>
  25. order by time desc
  26. </select>
  27. </mapper>

二、找不到mapper

首先排除@MapperScan("com.cjhx.tzld.mapper")已添加

1.首先配置文件扫描,mapper-locations:classpath:/com/cjhx/tzld/mapper/xml/*.xml

  1. mybatis-plus:
  2. configuration:
  3. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  4. mapper-locations: classpath:/com/cjhx/tzld/mapper/xml/*.xml

2.在pom.xml的<build>添加xml资源

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. </plugin>
  7. </plugins>
  8. <resources>
  9. <!--引入mapper对应的xml文件-->
  10. <resource>
  11. <directory>src/main/java</directory>
  12. <includes>
  13. <include>**/*.xml</include>
  14. </includes>
  15. </resource>
  16. </resources>
  17. </build>

到此这篇关于mybatis-plus使用问题汇总的文章就介绍到这了,更多相关mybatis-plus使用内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号