mybatis-plus使用问题小结
作者:别动我的猫 发布时间:2023-10-30 06:45:58
标签:mybatis-plus,使用
一、多表联合分页查询
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>
来源:https://www.cnblogs.com/zeussbook/p/15949379.html


猜你喜欢
- 前言本文主要给大家分享了Android仿网易新闻图片详情下滑隐藏效果的相关内容,分享出来供需要的朋友参考学习,下面话不多说了,来一起看看详细
- 本文实例为大家分享了java文件读写工具类的具体代码,供大家参考,具体内容如下import java.io.BufferedInputStr
- 一:背景1. 讲故事最近发现 C#7 之后的 is 是越来越看不懂了,乍一看花里胡哨的,不过当我静下心来仔细研读,发现这 is 是越来越短小
- # 看题目是不是很绕,这个我也不知道怎么才能更简单的表达了# 先看代码:public class Common {public static
- // 声明LocationManager对象 LocationManager loctionManager; // 通过系统服务,取得Loc
- 今天在工作中突然遇到这个问题,开始郁闷得不行,查阅了很多资料才解决。话不多少先上图①解决连接超时问题1:在Linux下输入命令ifconfi
- 前言spring 对bean的创建过程做了很完整的封装。但是提供了非常多的扩展接口,供我们使用。这一节主要是实现spring提供的获取 be
- 开发环境使用jdk1.8.0_60,把springboot 项目打成war包后,部署到apache-tomcat-7.0.68时报错如下,换
- 本文实例讲述了C#文件分割的方法。分享给大家供大家参考。具体如下:1. 小文件分割(适用于小于等于64M的文件):using System;
- 废话不多说了,直接给大家贴代码了,具体代码如下<?xml version="1.0" encoding="
- 一、概述简单理解为 异步消息插队并优先执行。场景:排队买票先来了一个普通用户来排队,买完票走了。后面又来了一个VIP用户A来买票 就一直站在
- 本文实例讲述了C#时间戳基本用法。分享给大家供大家参考。具体如下:一、C#如何生成一个时间戳/// <summary> ///
- 我们经常会将数据源放在DataTable里面,但是有时候也需要移除不想要的行,下面的代码告诉你们DataTable dts;DataRow[
- 我们有一个TextView,其里面的内容是可以通过代码动态改变的,我们想用一张图片作为TextView的背景,实现类似于手机QQ对话中的气泡
- 在考虑类初始化时,我们都知道进行子类初始化时,如果父类没有初始化要先初始化子类。然而事情并没有一句话这么简单。首先看看Java中初始化触发的
- 整理文档,搜刮出一个Android图片实现压缩处理的实例代码,稍微整理精简一下做下分享。详解:1.获取本地图片File文件 获取Bitma
- 一、简介使用了static 修饰符的方法为静态方法,反之则是非静态方法。 静态方法是一种特殊的成员方法,它不属于类的某一个具体的实
- 文件流输出文件名中文不显示response返回文件流 用response.setHeader(“Content-disp
- 1、xml代码:<?xml version="1.0" encoding="utf-8"?&g
- 1.ReadWriteLock介绍为什么我们有了Lock,还要用ReadWriteLock呢。我们对共享资源加锁之后,所有的线程都将会等待。