mybatis-plus教程,mybatis mybatis plus

  mybatis-plus教程,mybatis mybatis plus

  

目录

一、多表联合分页查询二、找不到制图人

 

  

一、多表联合分页查询

1.多表联合查询结果集建议使用维多利亚皇家勋章类,当然也可以使用结果图

 

  包com。cjhx。tzld。实体。VO;导入com。窦米宝。mybatisplus。注释。id类型;导入com。窦米宝。mybatisplus。注释。表格字段;导入com。窦米宝。mybatisplus。注释。tableid导入com。cjhx。tzld。实体。测试内容;导入com。更快的XML。杰克逊。注释。JSON格式;导入io。昂首阔步。注释。API模型;导入io。昂首阔步。注释。API模型属性;进口龙目岛。数据;导入org。spring框架。格式。注释。日期时间格式;导入Java。util。日期;@ Data @ API模型(value= TContentVo ,description=内容池多表联合数据对象)公共类TContentVo扩展了t content { @ API model property(value=编号)私有整数cid@ApiModelProperty(值=内容标题)私串标题;@ApiModelProperty(value=作者Id) @TableField(authorId )私有整数作者id;@ApiModelProperty(value=时间)@ JSON格式(模式= yyyy-MM-DD hh :MM : ss ,时区=GMT 8) //返回时间类型@ datetime格式(pattern= yyyy-MM-DD hh :MM : ss )//接收时间类型私人约会时间;@ApiModelProperty(value=内容)私有字符串内容;@ApiModelProperty(value=作者姓名)私串作者;@ApiModelProperty(value=话题)私串话题;@ApiModelProperty(value=模块编号)private int module num @ ApiModelProperty(value=模块)私有字符串模块;public TContentVo(){ } public TContentVo(整数cid,字符串标题,日期时间,字符串内容,字符串作者,字符串主题,int模块号){ this。cid=cidthis.title=titlethis.time=时间;this.content=内容;this.author=作者;this.topic=主题;这个。模块号=模块号;}2 .控制器

  包com。cjhx。tzld。控制器;导入com。窦米宝。mybatisplus。核心。条件。查询。查询包装器;导入com。窦米宝。mybatisplus。核心。元数据。ipage导入com。窦米宝。mybatisplus。扩展。插件。分页。页面;导入com。cjhx。tzld。常见。结果;导入com。cjhx。tzld。实体。测试内容;导入com。cjhx。tzld。实体。tcontentrelationfund导入com。cjhx。tzld。实体。ttopicpk导入com。cjhx。tzld。实体。VO。t conten

  tVo;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 */@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); }}

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使用内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!

 

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: