mybatis-Plus联表查询及分页 一、准备工作1、数据库结构以及数据2、依赖3、配置类让mybatis-plus-join在DataScopeSqlInjector中生效4、启动类排除MPJSqlInjector.class
mybatis-plus作为mybatis的增强工具,它的出现极大的简化了开发中的数据库操作,但是长久以来,它的联表查询能力一直被大家所诟病。一旦遇到left join或right join的左右连接,你还是得老老实实的打开xml文件,手写上一大段的sql语句。
直到前几天,偶然碰到了这么一款叫做mybatis-plus-join的工具(后面就简称mpj了),使用了一下,不得不说真香!彻底将我从xml地狱中解放了出来,终于可以以类似mybatis-plus中QueryWrapper的方式来进行联表查询了,话不多说,我们下面开始体验。
CREATE TABLE `op_product` ( `id` int(11) NOT NULL AUTO_INCREMENT, `type` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;INSERT INTO `test_yjdsns`.`op_product`(`id`, `type`) VALUES (1, '苹果');CREATE TABLE `op_product_info` ( `id` int(11) NOT NULL AUTO_INCREMENT, `product_id` int(11) NOT NULL, `name` varchar(255) DEFAULT NULL, `price` decimal(10,2) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;INSERT INTO `test_yjdsns`.`op_product_info`(`id`, `product_id`, `name`, `price`) VALUES (1, 1, '苹果13', 8.00);INSERT INTO `test_yjdsns`.`op_product_info`(`id`, `product_id`, `name`, `price`) VALUES (2, 1, '苹果15', 9.00);
com.GitHub.yulichang mybatis-plus-join 1.2.4 com.baomidou mybatis-plus-boot-starter 3.5.1
@Configurationpublic class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //分页插件 interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.Mysql)); return interceptor; } @Bean @Primary public mysqlInjector myLogicSqlInjector() { return new MySqlInjector(); }}
修改DataScopeSqlInjector中的继承类为:MPjsqlInjector
public class MySqlInjector extends MPJSqlInjector { @Override public List getMethodList(Class> mapperClass) { //将原来的保持 List methodList = super.getMethodList(mapperClass); //多表查询sql注入 从连表插件里移植过来的 methodList.add(new SelectJoinOne()); methodList.add(new SelectJoinList()); methodList.add(new SelectJoinPage()); methodList.add(new SelectJoinMap()); methodList.add(new SelectJoinMaps()); methodList.add(new SelectJoinMapsPage()); return methodList; }}
@SpringBootApplication(exclude = {MPJSqlInjector.class})
载入自定义配置类
@Configuration
@MapperScan可以选择tk下的路径
import tk.mybatis.spring.annotation.MapperScan;
@Data@NoArgsConstructor@AllArgsConstructor@TableName("op_product")public class OpProduct implements Serializable { private static final long serialVersionUID = -3918932563888251866L; @TableId(value = "ID", type = IdType.AUTO) private Long id; @TableField("TYPE") private String type;}
@Data@NoArgsConstructor@AllArgsConstructor@TableName("op_product_info")public class OpProductInfo implements Serializable { private static final long serialVersionUID = 4186082342917210485L; @TableId(value = "ID", type = IdType.AUTO) private Long id; @TableField("PRODUCT_ID") private Long productId; @TableField("NAME") private String name; @TableField("PRICE") private Double price;}
@Data@NoArgsConstructor@AllArgsConstructorpublic class ProductDTO implements Serializable { private static final long serialVersionUID = -2281333877153304329L; private Long id; private String type; private String name; private Double price;}
public interface OpProductInfoMapper extends MPJBaseMapper {}
public interface OpProductMapper extends MPJBaseMapper {}
Mapper接口改造完成后,我们把它注入到Service中,虽然说我们要完成3张表的联表查询,但是以OpProduct作为主表的话,那么只注入这一个对应的OpProductMapper就可以,非常简单。
public interface OpProductService extends MPJBaseService { List queryAllProduct();}
@Service@Slf4j@AllArgsConstructorpublic class OpProductServiceImpl extends MPJBaseServiceImpl implements OpProductService { @Resource private OpProductMapper opProductMapper; @Override public List queryAllProduct() { MPJLambdaWrapper mpjLambdaWrapper = new MPJLambdaWrapper() .selectAll(OpProduct.class)//查询表1的全部字段 .selectAll(OpProductInfo.class)//查询表2的全部字段 .leftJoin(OpProductInfo.class, OpProductInfo::getProductId, OpProduct::getId);//左查询表2条件为表二的productId=表一的id List list = opProductMapper.selectJoinList(ProductDTO.class, mpjLambdaWrapper); return list; }}
@SpringBootTest@Slf4jpublic class MybatisJoinTests { @Autowired private OpProductService opProductService; @Test void test1() { List productDTOS = opProductService.queryAllProduct(); log.info(productDTOS.toString()); }}
接下来的MPJLambdaWrapper就是构建查询条件的核心了,看一下我们在上面用到的几个方法:
除此之外,还可以正常调用mybatis-plus中的各种原生方法,文档中还提到,默认主表别名是t,其他的表别名以先后调用的顺序使用t1、t2、t3以此类推。
和mybatis-plus非常类似,除了LamdaWrapper外还提供了普通QueryWrapper的写法,举例代码:
public void getOrderSimple() { List list = xxxxxMapper.selectJoinList(xxxxx.class, new MPJqueryWrapper() .selectAll(xxxxx.class) .select("t2.unit_price","t2.name as product_name") .select("t1.name as user_name") .leftJoin("t_user t1 on t1.id = t.user_id") .leftJoin("t_product t2 on t2.id = t.product_id") .eq("t.status", "3") ); log.info(list.toString());}
或者
MPJLambdaWrapper mpjLambdaWrapper = new MPJLambdaWrapper() .selectAll(OpProduct.class)//查询表1的全部字段 .selectAs(OpProductInfo::getId,"ProductInfoId")//起别名 .selectAs(OpProductInfo::getName,ProductDTO::getName)//起别名 .selectAs(OpProductInfo::getPrice,ProductDTO::getPrice)//起别名 .leftJoin(OpProductInfo.class, OpProductInfo::getProductId, OpProduct::getId);//左查询表2条件为表二的productId=表一的id List list = opProductMapper.selectJoinList(ProductDTO.class, mpjLambdaWrapper); return list;
public IPage queryPageProduct(Integer pageNo, Integer pageCount) { MPJLambdaWrapper mpjLambdaWrapper = new MPJLambdaWrapper() .selectAll(OpProduct.class)//查询表1的全部字段 .selectAll(OpProductInfo.class)//查询表2的全部字段 .leftJoin(OpProductInfo.class, OpProductInfo::getProductId, OpProduct::getId);//左查询表2条件为表二的productId=表一的id IPage page = opProductMapper.selectJoinPage(new Page(pageNo, pageCount), ProductDTO.class, mpjLambdaWrapper); return page; }
来源地址:https://blog.csdn.net/weixin_46146718/article/details/125279384
--结束END--
本文标题: MyBatis-Plus联表查询及分页
本文链接: https://lsjlt.com/news/372154.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0