Example Analysis of mybatis-plus usage problems
This article mainly shows you the "sample analysis of mybatis-plus usage problems", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of mybatis-plus usage problems".
1. Multi-table joint paging query
1. Multi-table federated query result set is recommended to use the VO class, of course, you can also use 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= "content pool multi-table federated data objects") public class TContentVo extends TContent {@ ApiModelProperty (value= "number") private Integer cid; @ ApiModelProperty (value= "content title") private String title; @ ApiModelProperty (value= "author Id") @ TableField ("authorId") private Integer authorId @ ApiModelProperty (value = "time") @ JsonFormat (pattern= "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") / / return time type @ DateTimeFormat (pattern= "yyyy-MM-dd HH:mm:ss") / / receive time type private Date time; @ ApiModelProperty (value = "content") private String content; @ ApiModelProperty (value = "author's name") private String author @ ApiModelProperty (value = "topic") private String topic; @ ApiModelProperty (value = "module number") private int moduleNum; @ ApiModelProperty (value = "module") 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 ("content Pool Module") public class ContentController {@ Resource private TContentService contentService @ ApiImplicitParams ({@ ApiImplicitParam (name = "cid", value = "cid", dataType = "int", defaultValue = "0", required = false), @ ApiImplicitParam (name = "title", value = "title", dataType = "String", defaultValue = ", required = false), @ ApiImplicitParam (name =" author ", value =" author's name ", dataType =" String ", defaultValue =", required = false) @ ApiImplicitParam (name = "time", value = "release time", dataType = "Date", defaultValue = "", required = false), @ ApiImplicitParam (name = "content", value = "content", dataType = "String", defaultValue = "", required = false), @ ApiImplicitParam (name = "topic", value = "topic", dataType = "String", defaultValue = "", required = false), @ ApiImplicitParam (name = "moduleNum") Value = "delivery module 1 hot courier 2 direct", dataType = "int", defaultValue = "", required = false), @ ApiImplicitParam (name = "pageIndex", value = "page number", dataType = "int", defaultValue = "1", required = false), @ ApiImplicitParam (name = "pageSize", value = "quantity per page", dataType = "int", defaultValue = "10") Required = false)}) @ ApiResponses ({@ ApiResponse (code = 200jingmessage = "OK", response = TContent.class) @ ApiOperation (value= "paged content acquisition API (Web side)", notes= "supports multi-conditional query", 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 byPage = contentService.findByPage (new Page (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 implements TContentService {@ Resource private TContentMapper tContentMapper; @ Override public IPage findByPage (Page 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 {IPage findByPage (Page page, @ Param ("contentVo") TContentVo contentVo);}
5.mapper.xml, pay attention to the input parameter contentVo
SELECT t.`cid`, t.`authorId`, a.`name`, t.`title`, t.`time`, t.`content`, p.`topic`, h.`title`AS `module`module`tcontent`t LEFT JOIN `tauthor`an ON a.`aid` = t.`authorId`LEFT JOIN `troomtopicpk`p ON p.`cid` = t.`cid`LEFT JOIN `troomhots`express`ON h.`cid`, t.`authorId`, a.name`, t.`title`, t.`time`, t.`content`, p.`topic` F. `title`AS `module` LEFT JOIN `troomfund`f ON f.cid` = t.`cid`1room1 and cid = # {contentVo.cid} and t.title like concat ('%', # {contentVo.title},'%') and a.author like concat ('%', # {contentVo.author}) '%') and t.time = ${contentVo.time} and t.content like concat ('%', # {contentVo.content},'%') and p.topic like concat ('%', # {contentVo.topic},'%') and f.currentState =-1 and h.currentState =-1 order by time desc II, mapper not found
First rule out @ MapperScan ("com.cjhx.tzld.mapper") has been added
1. First configure file scanning, 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
two。 Add xml resources in pom.xml
Org.springframework.boot spring-boot-maven-plugin src/main/java * * / *. Xml above are all the contents of the article "sample Analysis of mybatis-plus usage problems" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!