广告
返回顶部
首页 > 资讯 > 精选 >mybatis-plus使用问题的示例分析
  • 860
分享到

mybatis-plus使用问题的示例分析

2023-06-29 08:06:13 860人浏览 八月长安
摘要

这篇文章主要为大家展示了“mybatis-plus使用问题的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“mybatis-plus使用问题的示例分析”这篇文章吧。一、多表联合分页查询多表

这篇文章主要为大家展示了“mybatis-plus使用问题的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“mybatis-plus使用问题的示例分析”这篇文章吧。

一、多表联合分页查询

多表联合查询结果集建议使用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;}

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;@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());        }    }}

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;@Servicepublic 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);    }}

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;public interface TContentMapper extends BaseMapper<TContent> {    IPage<TContentVo> findByPage(Page<TContentVo> page, @Param("contentVo") TContentVo contentVo);}

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")已添加

首先配置文件扫描,mapper-locations:classpath:/com/cjhx/tzld/mapper/xml*.xml</include>                </includes>            </resource>        </resources>    </build>

以上是“mybatis-plus使用问题的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网精选频道!

--结束END--

本文标题: mybatis-plus使用问题的示例分析

本文链接: https://www.lsjlt.com/news/323709.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • mybatis-plus使用问题的示例分析
    这篇文章主要为大家展示了“mybatis-plus使用问题的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“mybatis-plus使用问题的示例分析”这篇文章吧。一、多表联合分页查询多表...
    99+
    2023-06-29
  • Mybatis-Plus的示例分析
    这篇文章主要介绍了Mybatis-Plus的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Mybatis-Plus1.快速入门地址:安装 | MyBatis-Plus...
    99+
    2023-06-20
  • java中MyBatis-plus入门使用的示例分析
    小编给大家分享一下java中MyBatis-plus入门使用的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!一、初始化 SpringBoot 项目首先使用...
    99+
    2023-06-15
  • Mybatis Plus逆向工程的示例分析
    这篇文章主要介绍Mybatis Plus逆向工程的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、创建数据库注:给tbl_emp的表创建外键数据库以及表的建立:二、配置pom.xml 文件&nbs...
    99+
    2023-06-26
  • mybatis-plus查询源码的示例分析
    这篇文章主要介绍mybatis-plus查询源码的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!配置详情pom.xmldependency>     &...
    99+
    2023-06-29
  • Mybatis-Plus代码生成器的示例分析
    小编给大家分享一下Mybatis-Plus代码生成器的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧! 实战 数据库脚本 创建一张商品表test_goodsCREATE&nbs...
    99+
    2022-10-19
  • mybatis plus自动生成器的示例分析
    这篇文章将为大家详细讲解有关mybatis plus自动生成器的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。mybatis plus自动生成器解析使用这个可以超快速生成entity s...
    99+
    2023-06-29
  • Mybatis延迟加载问题的示例分析
    这篇文章主要介绍Mybatis延迟加载问题的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!延迟加载问题MyBatis针对关联表中的数据支持延迟加载。延迟加载其实就是将数据加载时机推迟,比如推迟嵌套查询的执行时...
    99+
    2023-06-15
  • Mybatis使用collection分页问题举例分析
    本篇内容介绍了“Mybatis使用collection分页问题举例分析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!原因引起该问题的原因是当...
    99+
    2023-06-21
  • mybatis-plus使用问题小结
    目录一、多表联合分页查询二、找不到mapper一、多表联合分页查询 1.多表联合查询结果集建议使用VO类,当然也可以使用resultMap package com.cjhx.tzld...
    99+
    2022-11-13
  • 基于mybatis-plus时间字段比较的示例分析
    这篇文章主要介绍了基于mybatis-plus时间字段比较的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。mybatis-plus 时间字段比较mybatis-plu...
    99+
    2023-06-20
  • Mybatis-Plus中SQL语句组拼原理的的示例分析
    这篇文章主要为大家展示了“Mybatis-Plus中SQL语句组拼原理的的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Mybatis-Plus中SQL语句组拼原理的的示例分析”这篇文章...
    99+
    2023-06-15
  • MyBatis-Plus 分页查询的实现示例
    方法: 使用selectPage()方法,第一个参数是传入分页方法(传入当前页和当前显示多少条数据),第二个参数是传入查询条件(如果查询全部的话,可以传null)。 前提: 表中的...
    99+
    2022-11-13
  • mybatis-plus QueryWrapper排序的坑案例分析
    这篇文章主要介绍了mybatis-plus QueryWrapper排序的坑案例分析的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇mybatis-plus QueryWrapper排序的坑案...
    99+
    2023-06-29
  • mysqldump问题的示例分析
    mysqldump问题的示例分析,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。导出:mysqldump数据库[表]>/t...
    99+
    2022-10-19
  • spring-mybatis与原生mybatis使用的示例分析
    小编给大家分享一下spring-mybatis与原生mybatis使用的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!原生mybatis使用方法:String resource = &...
    99+
    2023-05-30
    spring mybatis
  • Mybatis Plus使用条件构造器增删改查功能的示例分析
    这篇文章主要介绍Mybatis Plus使用条件构造器增删改查功能的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!java后端层级结构Controller 接口层接口层比较好理解,它是面向web网络的接口,使...
    99+
    2023-06-15
  • Mybatis-plus支持Gbase8s分页的实现示例
    目录需求实现方法法2实现20210423修改完善20210519完善20210528完善需求 实现mybatis-plus对gbase8s的分页效果支持,使切换数据库(如oracle...
    99+
    2022-11-12
  • 使用Mybatis-Plus时的SqlSessionFactory问题及处理
    目录使用Mybatis-Plus的SqlSessionFactory问题贴一下这两个类的源码,看一眼就明白了还有MybatisSqlSessionFactoryBean的spring...
    99+
    2022-11-12
  • mybatis属性的示例分析
    这篇文章给大家分享的是有关mybatis属性的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。前言MyBatis是基于“数据库结构不可控”的思想建立的,也就是我们希望数据库...
    99+
    2022-10-18
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作