mybatis-plus使用问题小结

 更新时间:2022年03月01日 11:37:19   作者:别动我的猫  
这篇文章主要介绍了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>

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

相关文章

  • Java使用正则表达式删除所有HTML标签的方法示例

    Java使用正则表达式删除所有HTML标签的方法示例

    这篇文章主要介绍了Java使用正则表达式删除所有HTML标签的方法,结合完整实例形式分析了java针对HTML页面元素script标签、style标签、html标签等的正则匹配相关操作技巧,需要的朋友可以参考下
    2017-06-06
  • IDEA配置Maven的超详细步骤

    IDEA配置Maven的超详细步骤

    Maven是一个能使我们的java程序开发节省时间和精力,是开发变得相对简单,还能使开发规范化的工具,下面这篇文章主要给大家介绍了关于IDEA配置Maven的超详细步骤,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2022-08-08
  • Java的Hibernate框架中Criteria查询使用的实例讲解

    Java的Hibernate框架中Criteria查询使用的实例讲解

    这篇文章主要介绍了Java的Hibernate框架中Criteria查询使用的实例讲解,Hibernate是Java的SSH三大web开发框架之一,需要的朋友可以参考下
    2016-01-01
  • springboot打成jar后获取classpath下文件失败的解决方案

    springboot打成jar后获取classpath下文件失败的解决方案

    这篇文章主要介绍了使用springboot打成jar后获取classpath下文件失败的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • 实现java文章点击量记录实例

    实现java文章点击量记录实例

    这篇文章主要为大家介绍了实现java文章点击量记录实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • Spring读取配置文件属性实现方法

    Spring读取配置文件属性实现方法

    这篇文章主要介绍了Spring读取配置文件属性实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • Spring框架七大模块简单介绍

    Spring框架七大模块简单介绍

    这篇文章主要介绍了Spring框架七大模块简单介绍,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • Spring Service功能作用详细讲解

    Spring Service功能作用详细讲解

    service层测试较简单,目前大多数测试主要是针对public方法进行的。依据测试方法划分,可以分为两种:基于mock的隔离测试和基于dbunit的普通测试
    2022-12-12
  • Java中synchronized关键字引出的多种锁 问题

    Java中synchronized关键字引出的多种锁 问题

    synchronized关键字是JAVA中常用的同步功能,提供了简单易用的锁功能。这篇文章主要介绍了Java中synchronized关键字引出的多种锁问题,需要的朋友可以参考下
    2019-07-07
  • springmvc 参数绑定总结

    springmvc 参数绑定总结

    本篇文章主要介绍了详解springmvc 参数绑定,详细的介绍了springmvc各种参数绑定的情况,具有一定的参考价值,有兴趣的可以了解一下。
    2017-03-03

最新评论