mybatis-plus 联表查询,mybatisplus支持多表联查
目录
建库建表依赖配置代码测试1.静态查询2.动态查询1.不传条件2.传条件
建库建表
如果存在mp,删除数据库;创建数据库国会议员默认字符集utf8使用MP;如果存在" t _用户",则删除表;如果存在“t_blog”,则删除表;设置名称utf8mb4创建表` t _ user `(` id ` BIGINT(0)NOT NULL AUTO _ INCREMENT,` user _ name ` VARCHAR(64)NOT NULL COMMENT 用户名(不能重复), nick _ name VARCHAR(64)不为空注释昵称(可以重复), email` VARCHAR(64) COMMENT 邮箱, create _ time DATETIME(0)NULL默认当前时间戳注释创建时间,` UPDATE _ time ` DATETIME(0)NULL更新当前时间戳注释上的默认当前时间戳修改时间, deleted _ flag BIGINT(0)不为空默认0注释 0:未删除其他:已删除,使用索引的存储类型目前只有两种的主键(` id `),唯一键` index _ user _ name _ deleted_flag `(` user _ name `,` deleted _ flag `),键` index _ create _ time `(` create _ time `))ENGINE=InnoDB COMMENT=用户;CREATE TABLE ` t _ blog ` id ` BIGINT(0)NOT NULL AUTO _ INCREMENT,` user_id` BIGINT(0) NOT NULL,` user_name` VARCHAR(64) NOT NULL,` title` VARCHAR(256)字符集 utf8mb4 NOT NULL注释标题, description VARCHAR(256)字符集utf8mb4不为空注释摘要,内容长文本字符集utf8mb4不为空注释内容,KEY `index_user_id`(`user_id `),)ENGINE=InnoDB CHARACTER SET=utf8mb 4 COMMENT=博客;插入 t _用户值(1,刀,刀刃、abc@qq.com、2021-01-23 093:33:36、2021-01-23 09:36、0);插入 t _用户值(2,“天空”天蓝、123@qq.com、2021-01-24 18:12:21、2021-01-24 18:12:21、0);插入博客值(1,1,刀, Java中枚举的用法, 本文介绍爪哇的枚举类的使用, 2021-01-23 11:33:36, 2021-01-23 11:33:36, 0);插入博客值(2,1,刀, Java中泛型的用法, 本文介绍爪哇的泛型的使用。, 2021-01-28 23:37:37, 2021-01-28 23:37:37, 0);插入博客值(3,1,刀, Java的模拟的原理, 本文介绍爪哇的模拟的原理。, 2021-05-28 09:06:06, 2021-05-28 09:06:06, 0);插入博客值(4,1, kn
ife, Java中BigDecimal的用法, 本文介绍Java的BigDecimal的使用。, 2021-06-24 20:36:54, 2021-06-24 20:36:54, 0);INSERT INTO `t_blog` VALUES (5, 1, knife, Java中反射的用法, 本文介绍Java的反射的使用。, 2021-10-28 22:24:18, 2021-10-28 22:24:18, 0);INSERT INTO `t_blog` VALUES (6, 2, sky, Vue-cli的使用, Vue-cli是Vue的一个脚手架工具, Vue-cli可以用来创建vue项目, 2021-02-23 11:34:36, 2021-02-25 14:33:36, 0);INSERT INTO `t_blog` VALUES (7, 2, sky, Vuex的用法, Vuex是vue用于共享变量的插件, 一般使用vuex来共享变量, 2021-03-28 23:37:37, 2021-03-28 23:37:37, 0);
依赖
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>MyBatis-Plus_Multiple</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>MyBatis-Plus_Multiple</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.12.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.3</version> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <!-- 指定maven编译的jdk版本。若不指定,maven3默认用jdk 1.5 maven2默认用jdk1.3 --> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build></project>
配置
application.yml
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/mp?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false password: 222333 username: root #mybatis-plus配置控制台打印完整带参数SQL语句mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
MyBatis-Plus分页插件配置
package com.example.demo.config; import com.baomidou.mybatisplus.annotation.DbType;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;@Configurationpublic class MyBatisPlusConfig { /** * 分页插件 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; }}
代码
VO
package com.example.demo.business.blog.vo; import lombok.Data;import java.time.LocalDateTime;@Datapublic class BlogVO { private Long id; private Long userId; private String userName; /** * 标题 */ private String title; * 摘要 private String description; * 内容 private String content; * 创建时间 private LocalDateTime createTime; * 修改时间 private LocalDateTime updateTime; * 昵称(这个是t_user的字段) private String nickName;}
Mapper
package com.example.demo.business.blog.mapper; import com.baomidou.mybatisplus.core.conditions.Wrapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.baomidou.mybatisplus.core.metadata.IPage;import com.example.demo.business.blog.entity.Blog;import com.example.demo.business.blog.vo.BlogVO;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;import org.springframework.stereotype.Repository;import java.util.List;@Repositorypublic interface BlogMapper extends BaseMapper<Blog> { /** * 静态查询 */ @Select("SELECT t_user.user_name " + " FROM t_blog, t_user " + " WHERE t_blog.id = #{id} " + " AND t_blog.user_id = t_user.id") String findUserNameByBlogId(@Param("id") Long id); * 动态查询 @Select("SELECT * " + " ${ew.customSqlSegment} ") IPage<BlogVO> findBlog(IPage<BlogVO> page, @Param("ew") Wrapper wrapper);}
Controller
package com.example.demo.business.blog.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.example.demo.business.blog.mapper.BlogMapper;import com.example.demo.business.blog.vo.BlogVO;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.util.StringUtils;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@Api(tags = "自定义SQL")@RestController@RequestMapping("/blog")public class BlogController { @Autowired private BlogMapper blogMapper; @ApiOperation("静态查询") @GetMapping("staticQuery") public String staticQuery() { return blogMapper.findUserNameByBlogId(1L); } @ApiOperation("动态查询") @GetMapping("dynamicQuery") public IPage<BlogVO> dynamicQuery(Page<BlogVO> page, String nickName, String title) { QueryWrapper<BlogVO> queryWrapper = new QueryWrapper<>(); queryWrapper.like(StringUtils.hasText(nickName), "t_user.nick_name", nickName); queryWrapper.like(StringUtils.hasText(title), "t_blog.title", title); queryWrapper.eq("t_blog.deleted_flag", 0); queryWrapper.eq("t_user.deleted_flag", 0); queryWrapper.apply("t_blog.user_id = t_user.id"); return blogMapper.findBlog(page, queryWrapper);}
测试
访问knife4j页面:http://localhost:8080/doc.html
1.静态查询
2.动态查询
1.不传条件
结果:(可以查到所有数据)
后端输出
Creating a new SqlSessionSqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@60bbb9ec] was not registered for synchronization because synchronization is not activeJDBC Connection [HikariProxyConnection@1853643659 wrapping com.mysql.cj.jdbc.ConnectionImpl@6a43d29c] will not be managed by Spring==> Preparing: SELECT COUNT(*) AS total FROM t_blog, t_user WHERE (t_blog.deleted_flag = ? AND t_user.deleted_flag = ? AND t_blog.user_id = t_user.id)==> Parameters: 0(Integer), 0(Integer)<== Columns: total<== Row: 7<== Total: 1==> Preparing: SELECT * FROM t_blog, t_user WHERE (t_blog.deleted_flag = ? AND t_user.deleted_flag = ? AND t_blog.user_id = t_user.id) LIMIT ?==> Parameters: 0(Integer), 0(Integer), 10(Long)<== Columns: id, user_id, user_name, title, description, content, create_time, update_time, deleted_flag, id, user_name, nick_name, email, create_time, update_time, deleted_flag<== Row: 1, 1, knife, Java中枚举的用法, 本文介绍Java的枚举类的使用, <<BLOB>>, 2021-01-23 11:33:36, 2021-01-23 11:33:36, 0, 1, knife, 刀刃, abc@qq.com, 2021-01-23 09:33:36, 2021-01-23 09:33:36, 0<== Row: 2, 1, knife, Java中泛型的用法, 本文介绍Java的泛型的使用。, <<BLOB>>, 2021-01-28 23:37:37, 2021-01-28 23:37:37, 0, 1, knife, 刀刃, abc@qq.com, 2021-01-23 09:33:36, 2021-01-23 09:33:36, 0<== Row: 3, 1, knife, Java的HashMap的原理, 本文介绍Java的HashMap的原理。, <<BLOB>>, 2021-05-28 09:06:06, 2021-05-28 09:06:06, 0, 1, knife, 刀刃, abc@qq.com, 2021-01-23 09:33:36, 2021-01-23 09:33:36, 0<== Row: 4, 1, knife, Java中BigDecimal的用法, 本文介绍Java的BigDecimal的使用。, <<BLOB>>, 2021-06-24 20:36:54, 2021-06-24 20:36:54, 0, 1, knife, 刀刃, abc@qq.com, 2021-01-23 09:33:36, 2021-01-23 09:33:36, 0<== Row: 5, 1, knife, Java中反射的用法, 本文介绍Java的反射的使用。, <<BLOB>>, 2021-10-28 22:24:18, 2021-10-28 22:24:18, 0, 1, knife, 刀刃, abc@qq.com, 2021-01-23 09:33:36, 2021-01-23 09:33:36, 0<== Row: 6, 2, sky, Vue-cli的使用, Vue-cli是Vue的一个脚手架工具, <<BLOB>>, 2021-02-23 11:34:36, 2021-02-25 14:33:36, 0, 2, sky, 天蓝, 123@qq.com, 2021-01-24 18:12:21, 2021-01-24 18:12:21, 0<== Row: 7, 2, sky, Vuex的用法, Vuex是vue用于共享变量的插件, <<BLOB>>, 2021-03-28 23:37:37, 2021-03-28 23:37:37, 0, 2, sky, 天蓝, 123@qq.com, 2021-01-24 18:12:21, 2021-01-24 18:12:21, 0<== Total: 7Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@60bbb9ec]
2.传条件
只传:nickName:刀
结果
后端结果
Creating a new SqlSessionSqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@30026aab] was not registered for synchronization because synchronization is not activeJDBC Connection [HikariProxyConnection@2127441980 wrapping com.mysql.cj.jdbc.ConnectionImpl@6a43d29c] will not be managed by Spring==> Preparing: SELECT COUNT(*) AS total FROM t_blog, t_user WHERE (t_user.nick_name LIKE ? AND t_blog.deleted_flag = ? AND t_user.deleted_flag = ? AND t_blog.user_id = t_user.id)==> Parameters: %刀%(String), 0(Integer), 0(Integer)<== Columns: total<== Row: 5<== Total: 1==> Preparing: SELECT * FROM t_blog, t_user WHERE (t_user.nick_name LIKE ? AND t_blog.deleted_flag = ? AND t_user.deleted_flag = ? AND t_blog.user_id = t_user.id) LIMIT ?==> Parameters: %刀%(String), 0(Integer), 0(Integer), 10(Long)<== Columns: id, user_id, user_name, title, description, content, create_time, update_time, deleted_flag, id, user_name, nick_name, email, create_time, update_time, deleted_flag<== Row: 1, 1, knife, Java中枚举的用法, 本文介绍Java的枚举类的使用, <<BLOB>>, 2021-01-23 11:33:36, 2021-01-23 11:33:36, 0, 1, knife, 刀刃, abc@qq.com, 2021-01-23 09:33:36, 2021-01-23 09:33:36, 0<== Row: 2, 1, knife, Java中泛型的用法, 本文介绍Java的泛型的使用。, <<BLOB>>, 2021-01-28 23:37:37, 2021-01-28 23:37:37, 0, 1, knife, 刀刃, abc@qq.com, 2021-01-23 09:33:36, 2021-01-23 09:33:36, 0<== Row: 3, 1, knife, Java的HashMap的原理, 本文介绍Java的HashMap的原理。, <<BLOB>>, 2021-05-28 09:06:06, 2021-05-28 09:06:06, 0, 1, knife, 刀刃, abc@qq.com, 2021-01-23 09:33:36, 2021-01-23 09:33:36, 0<== Row: 4, 1, knife, Java中BigDecimal的用法, 本文介绍Java的BigDecimal的使用。, <<BLOB>>, 2021-06-24 20:36:54, 2021-06-24 20:36:54, 0, 1, knife, 刀刃, abc@qq.com, 2021-01-23 09:33:36, 2021-01-23 09:33:36, 0<== Row: 5, 1, knife, Java中反射的用法, 本文介绍Java的反射的使用。, <<BLOB>>, 2021-10-28 22:24:18, 2021-10-28 22:24:18, 0, 1, knife, 刀刃, abc@qq.com, 2021-01-23 09:33:36, 2021-01-23 09:33:36, 0<== Total: 5Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@30026aab]
到此这篇关于MyBatis-Plus多表联查(动态查询)的文章就介绍到这了,更多相关MyBatis-Plus多表联查内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。