# linked-mybatis **Repository Path**: kzow3n/linked-mybatis ## Basic Information - **Project Name**: linked-mybatis - **Description**: Linked-Mybatis是一个MyBatis-Plus的增强工具,为实现 No XML, No Mapper 而生。Linked-Mybatis主要目标为通过函数式编程加Lambda表达式实现连表查询。原github仓库地址:https://github.com/kmp5/linked-mybatis - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: http://119.91.22.138:8089/ - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 1 - **Created**: 2022-02-18 - **Last Updated**: 2023-09-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: MyBatis, mybatis-plus, 连表查询 ## README # linked-mybatis [![Security Status](https://www.murphysec.com/platform3/v31/badge/1679675337990029312.svg)](https://www.murphysec.com/console/report/1679668707470688256/1679675337990029312) #### 介绍 - Linked-Mybatis(下面简称LM)是一个 MyBatis-Plus(下面简称MP)的增强工具,为实现 No XML, No Mapper 而生 - LM可以通过函数式编程加Lambda表达式实现连表查询 - LM提供了基于Redis的分布式缓存方案 - 目前支持数据库:Mysql、SqlServer、PgSql、达梦 - 作者:kzow3n,QQ邮箱:442764882@qq.com - 文档站点:http://119.91.22.138:8089/ - Maven仓库地址:https://mvnrepository.com/artifact/io.github.kmp5/linked-mybatis #### 快速上手 - 引入jar包 ```xml io.github.kmp5 linked-mybatis linked-mybatis.version ``` - 注入SqlSessionFactory、SqlSession ```java @Autowired protected SqlSessionFactory sqlSessionFactory; @Autowired protected SqlSession sqlSession; ``` #### 代码示例 - 基本连表查询 ```java LinkedQueryWrapper queryWrapper = new LinkedQueryWrapper(); queryWrapper .selectAll(1) .select(2, Teacher::getName, StudentModel::getHeadmasterName) .from(Student.class, 1) .InnerJoin(Teacher.class, 2) .on(1, Student::getHeadmasterId, 2, Teacher::getId) .eq(1, Student::getName, "LiHua") ; LinkedQueryExecutor queryExecutor = new LinkedQueryExecutorBuilder(sqlSessionFactory, sqlSession).build(); StudentModel studentModel = queryExecutor.forObject(StudentModel.class, queryWrapper); ``` - 分页查询 ```java LinkedQueryWrapper queryWrapper = new LinkedQueryWrapper(); queryWrapper .selectAll(1) .from(Student.class, 1) .InnerJoin(Teacher.class, 2) .on(1, Student::getHeadmasterId, 2, Teacher::getId) .eq(2, Teacher::getName, "Mr.Li") ; LinkedQueryExecutor queryExecutor = new LinkedQueryExecutorBuilder(sqlSessionFactory, sqlSession).build(); Page page = queryExecutor.forObjectPage(Student.class, queryWrapper, pageIndex, pageSize); ``` - 嵌套查询 ```java LinkedQueryWrapper queryWrapper = new LinkedQueryWrapper(); queryWrapper .selectAll(1) .from(Student.class, 1) .innerJoin(t -> t.from(Score.class, 1) .eq(1, Score::getNo, "001") .groupBy(1, Score::getStudentId) .having(t -> t.lt(p -> p.avg(1, Score::getScore), 60)) ) .on(1, Student::getId, 2, Score::getStudentId) ; LinkedQueryExecutor queryExecutor = new LinkedQueryExecutorBuilder(sqlSessionFactory, sqlSession).build(); List students = queryExecutor.forObjects(Student.class, queryWrapper); ``` - 复杂条件and/or ```java LinkedQueryWrapper queryWrapper = new LinkedQueryWrapper(); queryWrapper .selectAll(1) .from(Student.class, 1) .innerJoin(Score.class, 2) .on(1, Student::getId, 2, Score::getStudentId) .eq(2, Score::getNo, "001") .eq(2, Score::getType, "math") .and(q -> q.lt(2, Score::getScore, 60) .or() .ge(2, Score::getScore, 90) ) ; LinkedQueryExecutor queryExecutor = new LinkedQueryExecutorBuilder(sqlSessionFactory, sqlSession).build(); List students = queryExecutor.forObjects(Student.class, queryWrapper); ``` - 更多示例请浏览:http://119.91.22.138:8089/