mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
first
This commit is contained in:
commit
e74639c879
78
mp-test.sql
Normal file
78
mp-test.sql
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
Navicat Premium Data Transfer
|
||||||
|
|
||||||
|
Source Server : rm-bp1f1jf1i3x66k1p6go.mysql.rds.aliyuncs.com
|
||||||
|
Source Server Type : MySQL
|
||||||
|
Source Server Version : 50725
|
||||||
|
Source Host : rm-bp1f1jf1i3x66k1p6go.mysql.rds.aliyuncs.com:3306
|
||||||
|
Source Schema : mp-test
|
||||||
|
|
||||||
|
Target Server Type : MySQL
|
||||||
|
Target Server Version : 50725
|
||||||
|
File Encoding : 65001
|
||||||
|
|
||||||
|
Date: 27/01/2021 15:29:40
|
||||||
|
*/
|
||||||
|
|
||||||
|
SET NAMES utf8mb4;
|
||||||
|
SET FOREIGN_KEY_CHECKS = 0;
|
||||||
|
|
||||||
|
-- ----------------------------
|
||||||
|
-- Table structure for area
|
||||||
|
-- ----------------------------
|
||||||
|
DROP TABLE IF EXISTS `area`;
|
||||||
|
CREATE TABLE `area` (
|
||||||
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
`province` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
|
||||||
|
`city` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
|
||||||
|
`area` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
|
||||||
|
`postcode` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`id`) USING BTREE
|
||||||
|
) ENGINE = InnoDB AUTO_INCREMENT = 10004 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
|
||||||
|
|
||||||
|
-- ----------------------------
|
||||||
|
-- Records of area
|
||||||
|
-- ----------------------------
|
||||||
|
INSERT INTO `area` VALUES (10001, '北京', '北京', '东城区', '10001');
|
||||||
|
INSERT INTO `area` VALUES (10002, '上海', '上海', '浦东新区', '20001');
|
||||||
|
INSERT INTO `area` VALUES (10003, '浙江省', '杭州市', '西湖区', '30001');
|
||||||
|
|
||||||
|
-- ----------------------------
|
||||||
|
-- Table structure for user
|
||||||
|
-- ----------------------------
|
||||||
|
DROP TABLE IF EXISTS `user`;
|
||||||
|
CREATE TABLE `user` (
|
||||||
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
|
||||||
|
`sex` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
|
||||||
|
`head_img` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
|
||||||
|
PRIMARY KEY (`id`) USING BTREE
|
||||||
|
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
|
||||||
|
|
||||||
|
-- ----------------------------
|
||||||
|
-- Records of user
|
||||||
|
-- ----------------------------
|
||||||
|
INSERT INTO `user` VALUES (1, '张三', '男', 'http://url-zhangsan');
|
||||||
|
INSERT INTO `user` VALUES (2, '李四', '女', 'http://url-lisi');
|
||||||
|
|
||||||
|
-- ----------------------------
|
||||||
|
-- Table structure for user_address
|
||||||
|
-- ----------------------------
|
||||||
|
DROP TABLE IF EXISTS `user_address`;
|
||||||
|
CREATE TABLE `user_address` (
|
||||||
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
`user_id` int(11) NULL DEFAULT NULL,
|
||||||
|
`area_id` int(11) NULL DEFAULT NULL,
|
||||||
|
`tel` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
|
||||||
|
`address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`id`) USING BTREE
|
||||||
|
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
|
||||||
|
|
||||||
|
-- ----------------------------
|
||||||
|
-- Records of user_address
|
||||||
|
-- ----------------------------
|
||||||
|
INSERT INTO `user_address` VALUES (1, 1, 10001, '13111111111', '天安门');
|
||||||
|
INSERT INTO `user_address` VALUES (2, 1, 10002, '13222222222', '东方明珠');
|
||||||
|
INSERT INTO `user_address` VALUES (3, 2, 10003, '13333333333', '雷峰塔');
|
||||||
|
|
||||||
|
SET FOREIGN_KEY_CHECKS = 1;
|
101
pom.xml
Normal file
101
pom.xml
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.2.7.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.example</groupId>
|
||||||
|
<artifactId>demo</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>mp</name>
|
||||||
|
<description>Demo project for Spring Boot</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>1.2.75</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>8.0.23</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>druid-spring-boot-starter</artifactId>
|
||||||
|
<version>1.2.4</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>cglib</groupId>
|
||||||
|
<artifactId>cglib</artifactId>
|
||||||
|
<version>3.3.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.16</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||||
|
<version>3.4.2</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-generator</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.junit.vintage</groupId>
|
||||||
|
<artifactId>junit-vintage-engine</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>${artifactId}</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<skipTests>true</skipTests>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
13
src/main/java/com/example/mp/MpApplication.java
Normal file
13
src/main/java/com/example/mp/MpApplication.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package com.example.mp;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class MpApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(MpApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.example.mp.business.config;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* mybatis-plus配置
|
||||||
|
*
|
||||||
|
* @author yulichang
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class MybatisPlusConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启用mybatis-plus分页插件
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public MybatisPlusInterceptor paginationInterceptor() {
|
||||||
|
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||||
|
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
|
||||||
|
return interceptor;
|
||||||
|
}
|
||||||
|
}
|
25
src/main/java/com/example/mp/business/dto/UserDTO.java
Normal file
25
src/main/java/com/example/mp/business/dto/UserDTO.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package com.example.mp.business.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class UserDTO {
|
||||||
|
/** user */
|
||||||
|
private Integer id;
|
||||||
|
/** user */
|
||||||
|
private String name;
|
||||||
|
/** user */
|
||||||
|
private String sex;
|
||||||
|
/** user */
|
||||||
|
private String headImg;
|
||||||
|
/** user_address */
|
||||||
|
private String tel;
|
||||||
|
/** user_address */
|
||||||
|
private String address;
|
||||||
|
/** area */
|
||||||
|
private String province;
|
||||||
|
/** area */
|
||||||
|
private String city;
|
||||||
|
/** area */
|
||||||
|
private String area;
|
||||||
|
}
|
23
src/main/java/com/example/mp/business/entity/AreaEntity.java
Normal file
23
src/main/java/com/example/mp/business/entity/AreaEntity.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package com.example.mp.business.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseEntity;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("area")
|
||||||
|
public class AreaEntity extends MyBaseEntity {
|
||||||
|
|
||||||
|
@TableId
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
private String province;
|
||||||
|
|
||||||
|
private String city;
|
||||||
|
|
||||||
|
private String area;
|
||||||
|
|
||||||
|
private String postcode;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.example.mp.business.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseEntity;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("user_address")
|
||||||
|
public class UserAddressEntity extends MyBaseEntity {
|
||||||
|
|
||||||
|
@TableId
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
private Integer userId;
|
||||||
|
|
||||||
|
private Integer areaId;
|
||||||
|
|
||||||
|
private String tel;
|
||||||
|
|
||||||
|
private String address;
|
||||||
|
}
|
20
src/main/java/com/example/mp/business/entity/UserEntity.java
Normal file
20
src/main/java/com/example/mp/business/entity/UserEntity.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package com.example.mp.business.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseEntity;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("user")
|
||||||
|
public class UserEntity extends MyBaseEntity {
|
||||||
|
|
||||||
|
@TableId
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private String sex;
|
||||||
|
|
||||||
|
private String headImg;
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.example.mp.business.mapper;
|
||||||
|
|
||||||
|
import com.example.mp.business.entity.AreaEntity;
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface AreaMapper extends MyBaseMapper<AreaEntity> {
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.example.mp.business.mapper;
|
||||||
|
|
||||||
|
import com.example.mp.business.entity.UserAddressEntity;
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface UserAddressMapper extends MyBaseMapper<UserAddressEntity> {
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.example.mp.business.mapper;
|
||||||
|
|
||||||
|
import com.example.mp.business.entity.UserEntity;
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface UserMapper extends MyBaseMapper<UserEntity> {
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.example.mp.business.service;
|
||||||
|
|
||||||
|
import com.example.mp.business.entity.AreaEntity;
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseService;
|
||||||
|
|
||||||
|
public interface AreaService extends MyBaseService<AreaEntity> {
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.example.mp.business.service;
|
||||||
|
|
||||||
|
import com.example.mp.business.entity.UserAddressEntity;
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseService;
|
||||||
|
|
||||||
|
public interface UserAddressService extends MyBaseService<UserAddressEntity> {
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.example.mp.business.service;
|
||||||
|
|
||||||
|
import com.example.mp.business.entity.UserEntity;
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseService;
|
||||||
|
|
||||||
|
public interface UserService extends MyBaseService<UserEntity> {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.example.mp.business.service.impl;
|
||||||
|
|
||||||
|
import com.example.mp.business.entity.AreaEntity;
|
||||||
|
import com.example.mp.business.mapper.AreaMapper;
|
||||||
|
import com.example.mp.business.service.AreaService;
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AreaServiceImpl extends MyBaseServiceImpl<AreaMapper, AreaEntity> implements AreaService {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.example.mp.business.service.impl;
|
||||||
|
|
||||||
|
import com.example.mp.business.entity.UserAddressEntity;
|
||||||
|
import com.example.mp.business.mapper.UserAddressMapper;
|
||||||
|
import com.example.mp.business.service.UserAddressService;
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserAddressServiceImpl extends MyBaseServiceImpl<UserAddressMapper, UserAddressEntity> implements UserAddressService {
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.example.mp.business.service.impl;
|
||||||
|
|
||||||
|
import com.example.mp.business.entity.UserEntity;
|
||||||
|
import com.example.mp.business.mapper.UserMapper;
|
||||||
|
import com.example.mp.business.service.UserService;
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserServiceImpl extends MyBaseServiceImpl<UserMapper, UserEntity> implements UserService {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.example.mp.mybatis.plus.base;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 所有数据库对应的entity类的父类
|
||||||
|
* 可以添加 id deleted create_by 等通用字段
|
||||||
|
*
|
||||||
|
* @author yulichang
|
||||||
|
*/
|
||||||
|
public class MyBaseEntity {
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
package com.example.mp.mybatis.plus.base;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author yulichang
|
||||||
|
* @see BaseMapper
|
||||||
|
*/
|
||||||
|
public interface MyBaseMapper<T> extends BaseMapper<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表查询返回一条记录
|
||||||
|
*
|
||||||
|
* @param queryWrapper joinWrapper
|
||||||
|
* @param clazz 返回对象class
|
||||||
|
*/
|
||||||
|
<DTO> DTO selectJoinOne(@Param(Constants.WRAPPER) Wrapper<?> queryWrapper, Class<DTO> clazz);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表查询返回Map
|
||||||
|
*
|
||||||
|
* @param queryWrapper joinWrapper
|
||||||
|
*/
|
||||||
|
Map<String, Object> selectJoinMap(@Param(Constants.WRAPPER) Wrapper<?> queryWrapper);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表查询返回记录集合
|
||||||
|
*
|
||||||
|
* @param queryWrapper joinWrapper
|
||||||
|
* @param clazz 返回对象class
|
||||||
|
*/
|
||||||
|
<DTO> List<DTO> selectJoinList(@Param(Constants.WRAPPER) Wrapper<?> queryWrapper, Class<DTO> clazz);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表查询返回Map集合
|
||||||
|
*
|
||||||
|
* @param queryWrapper joinWrapper
|
||||||
|
*/
|
||||||
|
List<Map<String, Object>> selectJoinMaps(@Param(Constants.WRAPPER) Wrapper<?> queryWrapper);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表查询返回记录集合并分页
|
||||||
|
*
|
||||||
|
* @param queryWrapper joinWrapper
|
||||||
|
* @param <DTO> 分页返回对象
|
||||||
|
*/
|
||||||
|
<DTO, P extends IPage<?>> IPage<DTO> selectJoinPage(P page, @Param(Constants.WRAPPER) Wrapper<?> queryWrapper, Class<DTO> clazz);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表查询返回Map集合并分页
|
||||||
|
*
|
||||||
|
* @param queryWrapper joinWrapper
|
||||||
|
*/
|
||||||
|
<P extends IPage<?>> IPage<Map<String, Object>> selectJoinMapsPage(P page, @Param(Constants.WRAPPER) Wrapper<?> queryWrapper);
|
||||||
|
}
|
@ -0,0 +1,91 @@
|
|||||||
|
package com.example.mp.mybatis.plus.base;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.example.mp.mybatis.plus.func.MyWrapperFunc;
|
||||||
|
import com.example.mp.mybatis.plus.wrapper.MyLambdaQueryWrapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author yulichang
|
||||||
|
* @see IService
|
||||||
|
*/
|
||||||
|
public interface MyBaseService<T extends MyBaseEntity> extends IService<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default <DTO> DTO selectJoinOne(MyWrapperFunc<T> wrapperFunc, Class<DTO> clazz) {
|
||||||
|
return selectJoinOne(wrapperFunc.apply(new MyLambdaQueryWrapper<>()), clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接查询返回一条记录
|
||||||
|
*/
|
||||||
|
<DTO> DTO selectJoinOne(MyLambdaQueryWrapper<T> wrapper, Class<DTO> clazz);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default <DTO> List<DTO> selectJoinList(MyWrapperFunc<T> wrapperFunc, Class<DTO> clazz) {
|
||||||
|
return selectJoinList(wrapperFunc.apply(new MyLambdaQueryWrapper<>()), clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接查询返回集合
|
||||||
|
*/
|
||||||
|
<DTO> List<DTO> selectJoinList(MyLambdaQueryWrapper<T> wrapper, Class<DTO> clazz);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default <DTO, P extends IPage<?>> IPage<DTO> selectJoinListPage(P page, MyWrapperFunc<T> wrapperFunc, Class<DTO> clazz) {
|
||||||
|
return selectJoinListPage(page, wrapperFunc.apply(new MyLambdaQueryWrapper<>()), clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接查询返回集合并分页
|
||||||
|
*/
|
||||||
|
<DTO, P extends IPage<?>> IPage<DTO> selectJoinListPage(P page, MyLambdaQueryWrapper<T> wrapper, Class<DTO> clazz);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default Map<String, Object> selectJoinMap(MyWrapperFunc<T> wrapperFunc) {
|
||||||
|
return selectJoinMap(wrapperFunc.apply(new MyLambdaQueryWrapper<>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接查询返回Map
|
||||||
|
*/
|
||||||
|
Map<String, Object> selectJoinMap(MyLambdaQueryWrapper<T> wrapper);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default List<Map<String, Object>> selectJoinMaps(MyWrapperFunc<T> wrapperFunc) {
|
||||||
|
return selectJoinMaps(wrapperFunc.apply(new MyLambdaQueryWrapper<>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接查询返回Map集合
|
||||||
|
*/
|
||||||
|
List<Map<String, Object>> selectJoinMaps(MyLambdaQueryWrapper<T> wrapper);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default IPage<Map<String, Object>> selectJoinMapsPage(IPage<Map<String, Object>> page, MyWrapperFunc<T> wrapperFunc) {
|
||||||
|
return selectJoinMapsPage(page, wrapperFunc.apply(new MyLambdaQueryWrapper<>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接查询返回Map集合并分页
|
||||||
|
*/
|
||||||
|
IPage<Map<String, Object>> selectJoinMapsPage(IPage<Map<String, Object>> page, MyLambdaQueryWrapper<T> wrapper);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.example.mp.mybatis.plus.base;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.example.mp.mybatis.plus.wrapper.MyLambdaQueryWrapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author yulichang
|
||||||
|
* @see ServiceImpl
|
||||||
|
*/
|
||||||
|
public class MyBaseServiceImpl<M extends MyBaseMapper<T>, T extends MyBaseEntity> extends ServiceImpl<M, T> implements MyBaseService<T> {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <DTO> DTO selectJoinOne(MyLambdaQueryWrapper<T> wrapper, Class<DTO> clazz) {
|
||||||
|
return baseMapper.selectJoinOne(wrapper, clazz);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <DTO> List<DTO> selectJoinList(MyLambdaQueryWrapper<T> wrapper, Class<DTO> clazz) {
|
||||||
|
return baseMapper.selectJoinList(wrapper, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <DTO, P extends IPage<?>> IPage<DTO> selectJoinListPage(P page, MyLambdaQueryWrapper<T> wrapper, Class<DTO> clazz) {
|
||||||
|
return baseMapper.selectJoinPage(page, wrapper, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> selectJoinMap(MyLambdaQueryWrapper<T> wrapper) {
|
||||||
|
return baseMapper.selectJoinMap(wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Map<String, Object>> selectJoinMaps(MyLambdaQueryWrapper<T> wrapper) {
|
||||||
|
return baseMapper.selectJoinMaps(wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IPage<Map<String, Object>> selectJoinMapsPage(IPage<Map<String, Object>> page, MyLambdaQueryWrapper<T> wrapper) {
|
||||||
|
return baseMapper.selectJoinMapsPage(page, wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.example.mp.mybatis.plus.func;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseEntity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 支持别名的 SFunction
|
||||||
|
*
|
||||||
|
* @author yulichang
|
||||||
|
* @see SFunction
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface MySFunction<T extends MyBaseEntity, R> extends SFunction<T, R> {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.example.mp.mybatis.plus.func;
|
||||||
|
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseEntity;
|
||||||
|
import com.example.mp.mybatis.plus.wrapper.MyLambdaQueryWrapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author yulichang
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface MyWrapperFunc<T extends MyBaseEntity> {
|
||||||
|
|
||||||
|
MyLambdaQueryWrapper<T> apply(MyLambdaQueryWrapper<T> wrapper);
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.example.mp.mybatis.plus.injector;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
||||||
|
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
|
||||||
|
import com.example.mp.mybatis.plus.method.*;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SQL 注入器
|
||||||
|
*
|
||||||
|
* @see DefaultSqlInjector
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class MySqlInjector extends DefaultSqlInjector {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
|
||||||
|
List<AbstractMethod> list = super.getMethodList(mapperClass);
|
||||||
|
list.add(new SelectJoinOne());
|
||||||
|
list.add(new SelectJoinList());
|
||||||
|
list.add(new SelectJoinPage());
|
||||||
|
list.add(new SelectJoinMap());
|
||||||
|
list.add(new SelectJoinMaps());
|
||||||
|
list.add(new SelectJoinMapsPage());
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,93 @@
|
|||||||
|
package com.example.mp.mybatis.plus.interceptor;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.MybatisParameterHandler;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||||
|
import net.sf.cglib.beans.BeanMap;
|
||||||
|
import org.apache.ibatis.executor.resultset.DefaultResultSetHandler;
|
||||||
|
import org.apache.ibatis.executor.resultset.ResultSetHandler;
|
||||||
|
import org.apache.ibatis.plugin.*;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 结果封装
|
||||||
|
*
|
||||||
|
* @author yulichang
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Intercepts({@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})})
|
||||||
|
public class MyResultInterceptor implements Interceptor {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object intercept(Invocation invocation) throws Throwable {
|
||||||
|
DefaultResultSetHandler handler = (DefaultResultSetHandler) invocation.getTarget();
|
||||||
|
Object result = invocation.proceed();
|
||||||
|
if (Objects.isNull(result)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Class<?> val = getFieldVal(handler);
|
||||||
|
if (val == null) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
if (result instanceof ArrayList) {
|
||||||
|
List<Object> res = new ArrayList<>();
|
||||||
|
for (Object i : ((ArrayList) result)) {
|
||||||
|
if (i instanceof Map) {
|
||||||
|
res.add(mapToBean((Map<String, ?>) i, val));
|
||||||
|
} else {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
} else {
|
||||||
|
return mapToBean((Map<String, ?>) result, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T mapToBean(Map<String, ?> map, Class<T> clazz) {
|
||||||
|
T bean = ClassUtils.newInstance(clazz);
|
||||||
|
BeanMap beanMap = BeanMap.create(bean);
|
||||||
|
map.forEach((k, v) -> beanMap.put(StringUtils.underlineToCamel(k), v));
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 反射获取方法中的clazz
|
||||||
|
* 先用反射获取,应该是可以通过拓展框架直接获取的, todo
|
||||||
|
*
|
||||||
|
* @see MybatisParameterHandler
|
||||||
|
*/
|
||||||
|
public static Class<?> getFieldVal(DefaultResultSetHandler handler) {
|
||||||
|
Field[] fieldList = handler.getClass().getDeclaredFields();
|
||||||
|
for (Field f : fieldList) {
|
||||||
|
if (f.getName().equals("parameterHandler")) {
|
||||||
|
f.setAccessible(true);
|
||||||
|
try {
|
||||||
|
MybatisParameterHandler mybatisParameterHandler = (MybatisParameterHandler) f.get(handler);
|
||||||
|
Object object = mybatisParameterHandler.getParameterObject();
|
||||||
|
if (object instanceof Map) {
|
||||||
|
Map<?, ?> args = (Map<?, ?>) object;
|
||||||
|
return (Class<?>) args.get("clazz");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object plugin(Object target) {
|
||||||
|
return Plugin.wrap(target, this);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.example.mp.mybatis.plus.method;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||||
|
import com.example.mp.mybatis.plus.toolkit.Constant;
|
||||||
|
import com.example.mp.mybatis.plus.wrapper.MyLambdaQueryWrapper;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author yulichang
|
||||||
|
* @see AbstractMethod
|
||||||
|
*/
|
||||||
|
public abstract class MyAbstractMethod extends AbstractMethod {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String sqlSelectColumns(TableInfo table, boolean queryWrapper) {
|
||||||
|
String selectColumns = ASTERISK;
|
||||||
|
if (table.getResultMap() == null || (table.getResultMap() != null)) {
|
||||||
|
selectColumns = table.getAllSqlSelect();
|
||||||
|
String[] columns = selectColumns.split(StringPool.COMMA);
|
||||||
|
List<String> selectColumnList = new ArrayList<>();
|
||||||
|
for (String c : columns) {
|
||||||
|
selectColumnList.add(Constant.TABLE_ALIAS + MyLambdaQueryWrapper.TABLE_ALIAS_INDEX + StringPool.DOT + c);
|
||||||
|
}
|
||||||
|
selectColumns = String.join(StringPool.COMMA, selectColumnList);
|
||||||
|
}
|
||||||
|
if (!queryWrapper) {
|
||||||
|
return selectColumns;
|
||||||
|
}
|
||||||
|
return SqlScriptUtils.convertChoose(String.format("%s != null and %s != null", WRAPPER, Q_WRAPPER_SQL_SELECT),
|
||||||
|
SqlScriptUtils.unSafeParam(Q_WRAPPER_SQL_SELECT), selectColumns);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.example.mp.mybatis.plus.method;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
|
import com.example.mp.mybatis.plus.toolkit.Constant;
|
||||||
|
import com.example.mp.mybatis.plus.wrapper.MyLambdaQueryWrapper;
|
||||||
|
import org.apache.ibatis.mapping.MappedStatement;
|
||||||
|
import org.apache.ibatis.mapping.SqlSource;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* copy {@link com.baomidou.mybatisplus.core.injector.methods.SelectMaps}
|
||||||
|
*/
|
||||||
|
public class SelectJoinList extends MyAbstractMethod {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
|
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_LIST;
|
||||||
|
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
|
||||||
|
tableInfo.getTableName(), Constant.TABLE_ALIAS + MyLambdaQueryWrapper.TABLE_ALIAS_INDEX, sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||||
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||||
|
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.example.mp.mybatis.plus.method;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
|
import com.example.mp.mybatis.plus.toolkit.Constant;
|
||||||
|
import com.example.mp.mybatis.plus.wrapper.MyLambdaQueryWrapper;
|
||||||
|
import org.apache.ibatis.mapping.MappedStatement;
|
||||||
|
import org.apache.ibatis.mapping.SqlSource;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* copy {@link com.baomidou.mybatisplus.core.injector.methods.SelectMaps}
|
||||||
|
*/
|
||||||
|
public class SelectJoinMap extends MyAbstractMethod {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
|
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAP;
|
||||||
|
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
|
||||||
|
tableInfo.getTableName(), Constant.TABLE_ALIAS + MyLambdaQueryWrapper.TABLE_ALIAS_INDEX, sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||||
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||||
|
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.example.mp.mybatis.plus.method;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
|
import com.example.mp.mybatis.plus.toolkit.Constant;
|
||||||
|
import com.example.mp.mybatis.plus.wrapper.MyLambdaQueryWrapper;
|
||||||
|
import org.apache.ibatis.mapping.MappedStatement;
|
||||||
|
import org.apache.ibatis.mapping.SqlSource;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* copy {@link com.baomidou.mybatisplus.core.injector.methods.SelectMaps}
|
||||||
|
*/
|
||||||
|
public class SelectJoinMaps extends MyAbstractMethod {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
|
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAPS;
|
||||||
|
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
|
||||||
|
tableInfo.getTableName(), Constant.TABLE_ALIAS + MyLambdaQueryWrapper.TABLE_ALIAS_INDEX, sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||||
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||||
|
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.example.mp.mybatis.plus.method;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
|
import com.example.mp.mybatis.plus.toolkit.Constant;
|
||||||
|
import com.example.mp.mybatis.plus.wrapper.MyLambdaQueryWrapper;
|
||||||
|
import org.apache.ibatis.mapping.MappedStatement;
|
||||||
|
import org.apache.ibatis.mapping.SqlSource;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* copy {@link com.baomidou.mybatisplus.core.injector.methods.SelectMaps}
|
||||||
|
*/
|
||||||
|
public class SelectJoinMapsPage extends MyAbstractMethod {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
|
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAPS_PAGE;
|
||||||
|
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
|
||||||
|
tableInfo.getTableName(), Constant.TABLE_ALIAS + MyLambdaQueryWrapper.TABLE_ALIAS_INDEX, sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||||
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||||
|
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.example.mp.mybatis.plus.method;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
|
import com.example.mp.mybatis.plus.toolkit.Constant;
|
||||||
|
import com.example.mp.mybatis.plus.wrapper.MyLambdaQueryWrapper;
|
||||||
|
import org.apache.ibatis.mapping.MappedStatement;
|
||||||
|
import org.apache.ibatis.mapping.SqlSource;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* copy {@link com.baomidou.mybatisplus.core.injector.methods.SelectMaps}
|
||||||
|
*/
|
||||||
|
public class SelectJoinOne extends MyAbstractMethod {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
|
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_ONE;
|
||||||
|
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
|
||||||
|
tableInfo.getTableName(), Constant.TABLE_ALIAS + MyLambdaQueryWrapper.TABLE_ALIAS_INDEX, sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||||
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||||
|
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.example.mp.mybatis.plus.method;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
|
import com.example.mp.mybatis.plus.toolkit.Constant;
|
||||||
|
import com.example.mp.mybatis.plus.wrapper.MyLambdaQueryWrapper;
|
||||||
|
import org.apache.ibatis.mapping.MappedStatement;
|
||||||
|
import org.apache.ibatis.mapping.SqlSource;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* copy {@link com.baomidou.mybatisplus.core.injector.methods.SelectMaps}
|
||||||
|
*/
|
||||||
|
public class SelectJoinPage extends MyAbstractMethod {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
|
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_PAGE;
|
||||||
|
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
|
||||||
|
tableInfo.getTableName(), Constant.TABLE_ALIAS + MyLambdaQueryWrapper.TABLE_ALIAS_INDEX, sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||||
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||||
|
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
package com.example.mp.mybatis.plus.method;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author yulichang
|
||||||
|
* @see com.baomidou.mybatisplus.core.enums.SqlMethod
|
||||||
|
* @since 2021/1/15
|
||||||
|
*/
|
||||||
|
public enum SqlMethod {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表查询
|
||||||
|
*/
|
||||||
|
SELECT_JOIN_ONE("selectJoinOne", "返回一条记录",
|
||||||
|
"<script>\nSELECT %s FROM %s %s <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
|
||||||
|
|
||||||
|
SELECT_JOIN_LIST("selectJoinList", "返回List集合",
|
||||||
|
"<script>\nSELECT %s FROM %s %s <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
|
||||||
|
|
||||||
|
SELECT_JOIN_MAP("selectJoinMap", "返回一个Map",
|
||||||
|
"<script>\nSELECT %s FROM %s %s <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
|
||||||
|
|
||||||
|
SELECT_JOIN_MAPS("selectJoinMaps", "返回Map集合",
|
||||||
|
"<script>\nSELECT %s FROM %s %s <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
|
||||||
|
|
||||||
|
SELECT_JOIN_PAGE("selectJoinPage", "连表查询并分页",
|
||||||
|
"<script>\nSELECT %s FROM %s %s <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
|
||||||
|
|
||||||
|
SELECT_JOIN_MAPS_PAGE("selectJoinMapsPage", "返回Map集合并分页",
|
||||||
|
"<script>\nSELECT %s FROM %s %s <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>");
|
||||||
|
|
||||||
|
private final String method;
|
||||||
|
private final String desc;
|
||||||
|
private final String sql;
|
||||||
|
|
||||||
|
SqlMethod(String method, String desc, String sql) {
|
||||||
|
this.method = method;
|
||||||
|
this.desc = desc;
|
||||||
|
this.sql = sql;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMethod() {
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDesc() {
|
||||||
|
return desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSql() {
|
||||||
|
return sql;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.example.mp.mybatis.plus.toolkit;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author yulichang
|
||||||
|
*/
|
||||||
|
public interface Constant {
|
||||||
|
/**
|
||||||
|
* 表别名
|
||||||
|
*/
|
||||||
|
String TABLE_ALIAS = "t";
|
||||||
|
|
||||||
|
String AS = " AS ";
|
||||||
|
|
||||||
|
String ON = " ON ";
|
||||||
|
|
||||||
|
String EQUALS = " = ";
|
||||||
|
|
||||||
|
String JOIN = "JOIN";
|
||||||
|
|
||||||
|
String LEFT = "LEFT";
|
||||||
|
|
||||||
|
String RIGHT = "RIGHT";
|
||||||
|
|
||||||
|
String INNER = "INNER";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* " LEFT JOIN "
|
||||||
|
*/
|
||||||
|
String LEFT_JOIN = StringPool.SPACE + LEFT + StringPool.SPACE + JOIN + StringPool.SPACE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* " RIGHT JOIN "
|
||||||
|
*/
|
||||||
|
String RIGHT_JOIN = StringPool.SPACE + RIGHT + StringPool.SPACE + JOIN + StringPool.SPACE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* " INNER JOIN "
|
||||||
|
*/
|
||||||
|
String INNER_JOIN = StringPool.SPACE + INNER + StringPool.SPACE + JOIN + StringPool.SPACE;
|
||||||
|
}
|
@ -0,0 +1,166 @@
|
|||||||
|
package com.example.mp.mybatis.plus.toolkit;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.BiPredicate;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 程序连表工具类
|
||||||
|
*
|
||||||
|
* @author yulichang
|
||||||
|
* @since 2021/01/13
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class ListJoinUtils {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List关联
|
||||||
|
* example:
|
||||||
|
* join(leftList, rightList, Left::getRightId, Right::getId, Left::setRight);
|
||||||
|
* <p>
|
||||||
|
*
|
||||||
|
* @param leftData 主表
|
||||||
|
* @param rightData 数据表
|
||||||
|
* @param leftCondition 主表参与比较的属性
|
||||||
|
* @param rightCondition 数据表参与比较的属性
|
||||||
|
* @param action 主表数据关联操作
|
||||||
|
* @param <L> 主表泛型
|
||||||
|
* @param <R> 数据表泛型
|
||||||
|
* @param <LE> 主表参与比较的属性泛型
|
||||||
|
* @param <RE> 数据表参与比较的属性泛型
|
||||||
|
*/
|
||||||
|
public static <L, R, LE, RE> void join(List<L> leftData, List<R> rightData,
|
||||||
|
Function<L, LE> leftCondition,
|
||||||
|
Function<R, RE> rightCondition,
|
||||||
|
BiConsumer<L, R> action) {
|
||||||
|
if (CollectionUtils.isNotEmpty(leftData) && CollectionUtils.isNotEmpty(rightData)) {
|
||||||
|
for (L l : leftData) {
|
||||||
|
LE le = leftCondition.apply(l);
|
||||||
|
if (Objects.nonNull(le)) {
|
||||||
|
for (R r : rightData) {
|
||||||
|
RE re = rightCondition.apply(r);
|
||||||
|
if (Objects.nonNull(re) && le.equals(re)) {
|
||||||
|
action.accept(l, r);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List关联
|
||||||
|
* example:
|
||||||
|
* join(lefts, rights, (l, r) -> l.getRightId().intValue() == r.getId(), Left::setRight);
|
||||||
|
* <p>
|
||||||
|
*
|
||||||
|
* @param leftData 主表
|
||||||
|
* @param rightData 数据表
|
||||||
|
* @param condition 对比提条件
|
||||||
|
* @param action 关联操作
|
||||||
|
* @param <L> 主表泛型
|
||||||
|
* @param <R> 数据表泛型
|
||||||
|
*/
|
||||||
|
public static <L, R> void join(List<L> leftData, List<R> rightData,
|
||||||
|
BiPredicate<L, R> condition,
|
||||||
|
BiConsumer<L, R> action) {
|
||||||
|
if (CollectionUtils.isNotEmpty(leftData) && CollectionUtils.isNotEmpty(rightData)) {
|
||||||
|
for (L l : leftData) {
|
||||||
|
for (R r : rightData) {
|
||||||
|
if (Objects.nonNull(l) && Objects.nonNull(r)) {
|
||||||
|
if (condition.test(l, r)) {
|
||||||
|
action.accept(l, r);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List关联
|
||||||
|
* example:
|
||||||
|
* <p>
|
||||||
|
* join(leftList, rightList, (l, r) -> {
|
||||||
|
* ----if (l.getRightId().intValue() == r.getId()) {
|
||||||
|
* --------l.setRight(r);
|
||||||
|
* ----}
|
||||||
|
* });
|
||||||
|
*/
|
||||||
|
public static <L, R> void join(List<L> leftData, List<R> rightData,
|
||||||
|
BiConsumer<L, R> action) {
|
||||||
|
if (CollectionUtils.isNotEmpty(leftData) && CollectionUtils.isNotEmpty(rightData)) {
|
||||||
|
for (L l : leftData) {
|
||||||
|
for (R r : rightData) {
|
||||||
|
if (Objects.nonNull(l) && Objects.nonNull(r)) {
|
||||||
|
action.accept(l, r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取集合中 命中src 的第一条记录的值
|
||||||
|
* example
|
||||||
|
* <p>
|
||||||
|
* leftList.forEach(i -> i.setRightName(findFirst(rightList, i.getRightId(), Right::getId, Right::getName)));
|
||||||
|
*
|
||||||
|
* @param collection 数据集合
|
||||||
|
* @param src 目标值
|
||||||
|
* @param tag 获取目标值的方法
|
||||||
|
* @param result 命中结果
|
||||||
|
*/
|
||||||
|
public static <R, T, F> R findFirst(Collection<T> collection, Object src, Function<T, F> tag, Function<T, R> result) {
|
||||||
|
if (CollectionUtils.isNotEmpty(collection)) {
|
||||||
|
for (T t : collection) {
|
||||||
|
F f = tag.apply(t);
|
||||||
|
if (Objects.nonNull(f)) {
|
||||||
|
if (f.equals(src)) {
|
||||||
|
return result.apply(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T findFirst(Collection<T> collection, T src) {
|
||||||
|
if (CollectionUtils.isNotEmpty(collection)) {
|
||||||
|
for (T t : collection) {
|
||||||
|
if (t.equals(src)) {
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取集合中 命中src 的所有记录的值
|
||||||
|
*/
|
||||||
|
public static <R, T, F> List<R> findSubList(Collection<T> collection, Object src, Function<T, F> tag, Function<T, R> result) {
|
||||||
|
List<R> r = new ArrayList<>();
|
||||||
|
if (CollectionUtils.isNotEmpty(collection)) {
|
||||||
|
for (T t : collection) {
|
||||||
|
F f = tag.apply(t);
|
||||||
|
if (Objects.nonNull(f)) {
|
||||||
|
if (f.equals(src)) {
|
||||||
|
r.add(result.apply(t));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.example.mp.mybatis.plus.toolkit;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseEntity;
|
||||||
|
import com.example.mp.mybatis.plus.func.MySFunction;
|
||||||
|
import org.apache.ibatis.reflection.property.PropertyNamer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author yulichang
|
||||||
|
* @see LambdaUtils
|
||||||
|
* @see PropertyNamer
|
||||||
|
*/
|
||||||
|
public class MyLambdaUtils {
|
||||||
|
|
||||||
|
public static <T> String getName(SFunction<T, ?> fn) {
|
||||||
|
return PropertyNamer.methodToProperty(LambdaUtils.resolve(fn).getImplMethodName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T extends MyBaseEntity> String getColumn(MySFunction<T, ?> fn) {
|
||||||
|
return StringUtils.camelToUnderline(getName(fn));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T extends MyBaseEntity> Class<T> getEntityClass(MySFunction<T, ?> fn) {
|
||||||
|
return (Class<T>) LambdaUtils.resolve(fn).getImplClass();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,82 @@
|
|||||||
|
package com.example.mp.mybatis.plus.wrapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda;
|
||||||
|
import org.apache.ibatis.reflection.property.PropertyNamer;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static java.util.stream.Collectors.joining;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* copy {@link com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper}
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("serial")
|
||||||
|
public abstract class MyAbstractLambdaWrapper<T, Children extends MyAbstractLambdaWrapper<T, Children>>
|
||||||
|
extends MyAbstractWrapper<T, SFunction<T, ?>, Children> {
|
||||||
|
|
||||||
|
private Map<String, ColumnCache> columnMap = null;
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
protected String columnsToString(SFunction<T, ?>... columns) {
|
||||||
|
return columnsToString(true, columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
protected String columnsToString(boolean onlyColumn, SFunction<T, ?>... columns) {
|
||||||
|
return Arrays.stream(columns).map(i -> columnToString(i, onlyColumn)).collect(joining(StringPool.COMMA));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String columnToString(SFunction<T, ?> column) {
|
||||||
|
return columnToString(column, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String columnToString(SFunction<T, ?> column, boolean onlyColumn) {
|
||||||
|
return getColumn(LambdaUtils.resolve(column), onlyColumn);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String column2String(SFunction<?, ?> column, boolean onlyColumn) {
|
||||||
|
return getColumn(LambdaUtils.resolve(column), onlyColumn);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 SerializedLambda 对应的列信息,从 lambda 表达式中推测实体类
|
||||||
|
* <p>
|
||||||
|
* 如果获取不到列信息,那么本次条件组装将会失败
|
||||||
|
*
|
||||||
|
* @param lambda lambda 表达式
|
||||||
|
* @param onlyColumn 如果是,结果: "name", 如果否: "name" as "name"
|
||||||
|
* @return 列
|
||||||
|
* @throws com.baomidou.mybatisplus.core.exceptions.MybatisPlusException 获取不到列信息时抛出异常
|
||||||
|
* @see SerializedLambda#getImplClass()
|
||||||
|
* @see SerializedLambda#getImplMethodName()
|
||||||
|
*/
|
||||||
|
private String getColumn(SerializedLambda lambda, boolean onlyColumn) {
|
||||||
|
Class<?> aClass = lambda.getInstantiatedType();
|
||||||
|
tryInitCache(aClass);
|
||||||
|
String fieldName = PropertyNamer.methodToProperty(lambda.getImplMethodName());
|
||||||
|
ColumnCache columnCache = getColumnCache(fieldName, aClass);
|
||||||
|
return onlyColumn ? columnCache.getColumn() : columnCache.getColumnSelect();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void tryInitCache(Class<?> lambdaClass) {
|
||||||
|
columnMap = LambdaUtils.getColumnMap(lambdaClass);
|
||||||
|
Assert.notNull(columnMap, "can not find lambda cache for this entity [%s]", lambdaClass.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private ColumnCache getColumnCache(String fieldName, Class<?> lambdaClass) {
|
||||||
|
ColumnCache columnCache = columnMap.get(LambdaUtils.formatKey(fieldName));
|
||||||
|
Assert.notNull(columnCache, "can not find lambda cache for this property [%s] of entity [%s]",
|
||||||
|
fieldName, lambdaClass.getName());
|
||||||
|
return columnCache;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,201 @@
|
|||||||
|
package com.example.mp.mybatis.plus.wrapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.SharedString;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.*;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
import static java.util.stream.Collectors.joining;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* copy {@link com.baomidou.mybatisplus.core.conditions.AbstractWrapper}
|
||||||
|
*/
|
||||||
|
@SuppressWarnings({"serial", "unchecked"})
|
||||||
|
public abstract class MyAbstractWrapper<T, R, Children extends MyAbstractWrapper<T, R, Children>> extends Wrapper<T> {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 占位符
|
||||||
|
*/
|
||||||
|
protected final Children typedThis = (Children) this;
|
||||||
|
/**
|
||||||
|
* 必要度量
|
||||||
|
*/
|
||||||
|
protected AtomicInteger paramNameSeq;
|
||||||
|
protected Map<String, Object> paramNameValuePairs;
|
||||||
|
protected SharedString lastSql;
|
||||||
|
/**
|
||||||
|
* SQL注释
|
||||||
|
*/
|
||||||
|
protected SharedString sqlComment;
|
||||||
|
/**
|
||||||
|
* SQL起始语句
|
||||||
|
*/
|
||||||
|
protected SharedString sqlFirst;
|
||||||
|
/**
|
||||||
|
* ß
|
||||||
|
* 数据库表映射实体类
|
||||||
|
*/
|
||||||
|
private T entity;
|
||||||
|
protected MergeSegments expression;
|
||||||
|
/**
|
||||||
|
* 实体类型(主要用于确定泛型以及取TableInfo缓存)
|
||||||
|
*/
|
||||||
|
private Class<T> entityClass;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T getEntity() {
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Children setEntity(T entity) {
|
||||||
|
this.entity = entity;
|
||||||
|
return typedThis;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class<T> getEntityClass() {
|
||||||
|
if (entityClass == null && entity != null) {
|
||||||
|
entityClass = (Class<T>) entity.getClass();
|
||||||
|
}
|
||||||
|
return entityClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Children setEntityClass(Class<T> entityClass) {
|
||||||
|
if (entityClass != null) {
|
||||||
|
this.entityClass = entityClass;
|
||||||
|
}
|
||||||
|
return typedThis;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 子类返回一个自己的新对象
|
||||||
|
*/
|
||||||
|
protected abstract Children instance();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 格式化SQL
|
||||||
|
*
|
||||||
|
* @param sqlStr SQL语句部分
|
||||||
|
* @param params 参数集
|
||||||
|
* @return sql
|
||||||
|
*/
|
||||||
|
protected final String formatSql(String sqlStr, Object... params) {
|
||||||
|
return formatSqlIfNeed(true, sqlStr, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 根据需要格式化SQL<br>
|
||||||
|
* <br>
|
||||||
|
* Format SQL for methods: EntityQ<T>.where/and/or...("name={0}", value);
|
||||||
|
* ALL the {<b>i</b>} will be replaced with #{MPGENVAL<b>i</b>}<br>
|
||||||
|
* <br>
|
||||||
|
* ew.where("sample_name=<b>{0}</b>", "haha").and("sample_age ><b>{0}</b>
|
||||||
|
* and sample_age<<b>{1}</b>", 18, 30) <b>TO</b>
|
||||||
|
* sample_name=<b>#{MPGENVAL1}</b> and sample_age>#<b>{MPGENVAL2}</b> and
|
||||||
|
* sample_age<<b>#{MPGENVAL3}</b><br>
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param need 是否需要格式化
|
||||||
|
* @param sqlStr SQL语句部分
|
||||||
|
* @param params 参数集
|
||||||
|
* @return sql
|
||||||
|
*/
|
||||||
|
protected final String formatSqlIfNeed(boolean need, String sqlStr, Object... params) {
|
||||||
|
if (!need || StringUtils.isBlank(sqlStr)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (ArrayUtils.isNotEmpty(params)) {
|
||||||
|
for (int i = 0; i < params.length; ++i) {
|
||||||
|
String genParamName = Constants.WRAPPER_PARAM + paramNameSeq.incrementAndGet();
|
||||||
|
sqlStr = sqlStr.replace(String.format("{%s}", i),
|
||||||
|
String.format(Constants.WRAPPER_PARAM_FORMAT, Constants.WRAPPER, genParamName));
|
||||||
|
paramNameValuePairs.put(genParamName, params[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sqlStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 必要的初始化
|
||||||
|
*/
|
||||||
|
protected void initNeed() {
|
||||||
|
paramNameSeq = new AtomicInteger(0);
|
||||||
|
paramNameValuePairs = new HashMap<>(16);
|
||||||
|
expression = new MergeSegments();
|
||||||
|
lastSql = SharedString.emptyString();
|
||||||
|
sqlComment = SharedString.emptyString();
|
||||||
|
sqlFirst = SharedString.emptyString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
entity = null;
|
||||||
|
paramNameSeq.set(0);
|
||||||
|
paramNameValuePairs.clear();
|
||||||
|
expression.clear();
|
||||||
|
lastSql.toEmpty();
|
||||||
|
sqlComment.toEmpty();
|
||||||
|
sqlFirst.toEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSqlSegment() {
|
||||||
|
return expression.getSqlSegment() + lastSql.getStringValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSqlComment() {
|
||||||
|
if (StringUtils.isNotBlank(sqlComment.getStringValue())) {
|
||||||
|
return "/*" + StringEscape.escapeRawString(sqlComment.getStringValue()) + "*/";
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSqlFirst() {
|
||||||
|
if (StringUtils.isNotBlank(sqlFirst.getStringValue())) {
|
||||||
|
return StringEscape.escapeRawString(sqlFirst.getStringValue());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MergeSegments getExpression() {
|
||||||
|
return expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Object> getParamNameValuePairs() {
|
||||||
|
return paramNameValuePairs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 columnName
|
||||||
|
*/
|
||||||
|
protected String columnToString(R column) {
|
||||||
|
return (String) column;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 多字段转换为逗号 "," 分割字符串
|
||||||
|
*
|
||||||
|
* @param columns 多字段
|
||||||
|
*/
|
||||||
|
protected String columnsToString(R... columns) {
|
||||||
|
return Arrays.stream(columns).map(this::columnToString).collect(joining(StringPool.COMMA));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
public Children clone() {
|
||||||
|
return SerializationUtils.clone(typedThis);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,303 @@
|
|||||||
|
package com.example.mp.mybatis.plus.wrapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.SharedString;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.Query;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseEntity;
|
||||||
|
import com.example.mp.mybatis.plus.func.MySFunction;
|
||||||
|
import com.example.mp.mybatis.plus.toolkit.Constant;
|
||||||
|
import com.example.mp.mybatis.plus.toolkit.MyLambdaUtils;
|
||||||
|
import com.example.mp.mybatis.plus.wrapper.interfaces.MyJoin;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 链接查询子Query
|
||||||
|
*
|
||||||
|
* @author yulichang
|
||||||
|
* @see com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
|
||||||
|
* @since 2021/01/19
|
||||||
|
*/
|
||||||
|
public class MyJoinLambdaQueryWrapper<T extends MyBaseEntity> extends MyAbstractLambdaWrapper<T, MyJoinLambdaQueryWrapper<T>>
|
||||||
|
implements Query<MyJoinLambdaQueryWrapper<T>, T, SFunction<T, ?>>, MyJoin<MyJoinLambdaQueryWrapper<T>, T> {
|
||||||
|
|
||||||
|
protected SharedString sqlSelect = new SharedString();
|
||||||
|
/**
|
||||||
|
* select字段
|
||||||
|
*/
|
||||||
|
protected List<SelectColumn> selectColumnList = new ArrayList<>();
|
||||||
|
/**
|
||||||
|
* 表名
|
||||||
|
*/
|
||||||
|
protected SharedString from = new SharedString();
|
||||||
|
/**
|
||||||
|
* 子表列表
|
||||||
|
*/
|
||||||
|
protected List<SubTable> classList = new ArrayList<>();
|
||||||
|
/**
|
||||||
|
* 表序号
|
||||||
|
*/
|
||||||
|
protected int rUid = 1;
|
||||||
|
|
||||||
|
|
||||||
|
public MyJoinLambdaQueryWrapper() {
|
||||||
|
super.setEntity(null);
|
||||||
|
super.initNeed();
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyJoinLambdaQueryWrapper(int rUid) {
|
||||||
|
this.rUid = rUid;
|
||||||
|
super.setEntity(null);
|
||||||
|
super.initNeed();
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyJoinLambdaQueryWrapper(T entity, Class<T> entityClass, SharedString sqlSelect, AtomicInteger paramNameSeq,
|
||||||
|
Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
|
||||||
|
SharedString lastSql, SharedString sqlComment) {
|
||||||
|
super.setEntity(entity);
|
||||||
|
this.setEntityClass(entityClass);
|
||||||
|
this.paramNameSeq = paramNameSeq;
|
||||||
|
this.paramNameValuePairs = paramNameValuePairs;
|
||||||
|
this.expression = mergeSegments;
|
||||||
|
this.sqlSelect = sqlSelect;
|
||||||
|
this.lastSql = lastSql;
|
||||||
|
this.sqlComment = sqlComment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected MyJoinLambdaQueryWrapper<T> instance() {
|
||||||
|
return new MyJoinLambdaQueryWrapper<>(getEntity(), getEntityClass(), null, paramNameSeq, paramNameValuePairs,
|
||||||
|
new MergeSegments(), SharedString.emptyString(), SharedString.emptyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
super.clear();
|
||||||
|
this.sqlSelect.toNull();
|
||||||
|
this.selectColumnList.clear();
|
||||||
|
this.from.toNull();
|
||||||
|
this.classList.clear();
|
||||||
|
this.rUid = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MyJoinLambdaQueryWrapper<T> select(SFunction<T, ?>... columns) {
|
||||||
|
if (ArrayUtils.isNotEmpty(columns)) {
|
||||||
|
for (SFunction<T, ?> str : columns) {
|
||||||
|
selectColumnList.add(new SelectColumn(rUid, columnToString(str), null, null));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return typedThis;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询所有字段 (后置处理)
|
||||||
|
*/
|
||||||
|
public MyJoinLambdaQueryWrapper<T> selectAll(Class<T> clazz) {
|
||||||
|
setEntityClass(clazz);
|
||||||
|
TableInfo info = TableInfoHelper.getTableInfo(getEntityClass());
|
||||||
|
info.getFieldList().forEach(s ->
|
||||||
|
selectColumnList.add(new SelectColumn(this.rUid, s.getColumn(), s.getColumn()/*s.getProperty()*/, null)));
|
||||||
|
if (StringUtils.isNotBlank(info.getKeyColumn())) {
|
||||||
|
selectColumnList.add(new SelectColumn(this.rUid, info.getKeyColumn(),info.getKeyColumn()/* info.getKeyProperty()*/, null));
|
||||||
|
}
|
||||||
|
return typedThis;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字段去重(同时使用select和as的情况下去除select中重复查询的列,优先使用as列作为查询字段)
|
||||||
|
* 注意:
|
||||||
|
* 只对selectDistinct前面的 select selectAll as(asCount ...)有效
|
||||||
|
* 只对当前的表去重,对连接查询的其他表不生效,如需要在对应的表下使用此方法
|
||||||
|
*/
|
||||||
|
public MyJoinLambdaQueryWrapper<T> selectDistinct() {
|
||||||
|
List<SelectColumn> collect = selectColumnList.stream().filter(i -> i.getUid() == rUid).collect(Collectors.toList());
|
||||||
|
List<SelectColumn> distinctList = new ArrayList<>();
|
||||||
|
|
||||||
|
List<SelectColumn> collect1 = collect.stream().distinct().collect(Collectors.toList());
|
||||||
|
if (collect.size() != collect1.size()) {
|
||||||
|
collect.forEach(i -> {
|
||||||
|
if (!collect1.contains(i)) {
|
||||||
|
distinctList.add(i);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
for (SelectColumn x : collect1) {
|
||||||
|
if (x.getAs() != null) {
|
||||||
|
for (SelectColumn y : collect1) {
|
||||||
|
if (x.getUid() == y.getUid() && x.getColumn().equals(y.getColumn()) && y.getAs() == null) {
|
||||||
|
distinctList.add(y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
selectColumnList.removeIf(distinctList::contains);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MyJoinLambdaQueryWrapper<T> select(Predicate<TableFieldInfo> predicate) {
|
||||||
|
return select(getEntityClass(), predicate);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MyJoinLambdaQueryWrapper<T> select(Class<T> entityClass, Predicate<TableFieldInfo> predicate) {
|
||||||
|
this.setEntityClass(entityClass);
|
||||||
|
TableInfo info = TableInfoHelper.getTableInfo(getEntityClass());
|
||||||
|
info.getFieldList().stream().filter(predicate).forEach(s ->
|
||||||
|
selectColumnList.add(new SelectColumn(this.rUid, s.getColumn(), null, null)));
|
||||||
|
return typedThis;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public <DTO> MyJoinLambdaQueryWrapper<T> as(SFunction<T, ?> entityColumn, SFunction<DTO, ?> DTOColumn) {
|
||||||
|
selectColumnList.add(new SelectColumn(rUid, columnToString(entityColumn), MyLambdaUtils.getName(DTOColumn), null));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <DTO> MyJoinLambdaQueryWrapper<T> asCount(MySFunction<T, ?> entityColumn, SFunction<DTO, ?> DTOColumn) {
|
||||||
|
selectColumnList.add(new SelectColumn(rUid, columnToString(entityColumn), MyLambdaUtils.getName(DTOColumn), SelectFunc.COUNT));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <DTO> MyJoinLambdaQueryWrapper<T> asSum(MySFunction<T, ?> entityColumn, SFunction<DTO, ?> DTOColumn) {
|
||||||
|
selectColumnList.add(new SelectColumn(rUid, columnToString(entityColumn), MyLambdaUtils.getName(DTOColumn), SelectFunc.SUM));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <DTO> MyJoinLambdaQueryWrapper<T> asAvg(MySFunction<T, ?> entityColumn, SFunction<DTO, ?> DTOColumn) {
|
||||||
|
selectColumnList.add(new SelectColumn(rUid, columnToString(entityColumn), MyLambdaUtils.getName(DTOColumn), SelectFunc.AVG));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <DTO> MyJoinLambdaQueryWrapper<T> asMax(MySFunction<T, ?> entityColumn, SFunction<DTO, ?> DTOColumn) {
|
||||||
|
selectColumnList.add(new SelectColumn(rUid, columnToString(entityColumn), MyLambdaUtils.getName(DTOColumn), SelectFunc.MAX));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <DTO> MyJoinLambdaQueryWrapper<T> asMin(MySFunction<T, ?> entityColumn, SFunction<DTO, ?> DTOColumn) {
|
||||||
|
selectColumnList.add(new SelectColumn(rUid, columnToString(entityColumn), MyLambdaUtils.getName(DTOColumn), SelectFunc.MIN));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <DTO> MyJoinLambdaQueryWrapper<T> asDateFormat(MySFunction<T, ?> entityColumn, SFunction<DTO, ?> DTOColumn) {
|
||||||
|
selectColumnList.add(new SelectColumn(rUid, columnToString(entityColumn), MyLambdaUtils.getName(DTOColumn), SelectFunc.DATE_FORMAT));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 左连接查询
|
||||||
|
*
|
||||||
|
* @param leftCondition 主表参与比较的字段 (on)
|
||||||
|
* @param rightCondition 子表参与比较的字段 (on)
|
||||||
|
* @param rightWrapper 子表的wrapper
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public <R extends MyBaseEntity, TE, RE> MyJoinLambdaQueryWrapper<T> leftJoin(boolean condition, String alias, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return join(condition, alias, Constant.LEFT_JOIN, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 右连接查询(参考左连接)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public <R extends MyBaseEntity, TE, RE> MyJoinLambdaQueryWrapper<T> rightJoin(boolean condition, String alias, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return join(condition, alias, Constant.RIGHT_JOIN, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 内连接查询(参考左连接)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public <R extends MyBaseEntity, TE, RE> MyJoinLambdaQueryWrapper<T> innerJoin(boolean condition, String alias, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return join(condition, alias, Constant.INNER_JOIN, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
private <R extends MyBaseEntity, TE, RE> MyJoinLambdaQueryWrapper<T> join(boolean condition, String alias, String keyWord, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
if (condition) {
|
||||||
|
setEntityClass(MyLambdaUtils.getEntityClass(leftCondition));
|
||||||
|
int childrenId = rUid + 1;
|
||||||
|
classList.add(new SubTable(alias, keyWord, rUid, MyLambdaUtils.getColumn(leftCondition), childrenId, MyLambdaUtils.getColumn(rightCondition), TableInfoHelper.getTableInfo(MyLambdaUtils.getEntityClass(rightCondition)).getTableName()));
|
||||||
|
MyJoinLambdaQueryWrapper<R> apply = rightWrapper.apply(new MyJoinLambdaQueryWrapper<>(childrenId));
|
||||||
|
classList.addAll(apply.classList);
|
||||||
|
this.selectColumnList.addAll(apply.selectColumnList);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表条件
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class SubTable {
|
||||||
|
|
||||||
|
private String alias;
|
||||||
|
|
||||||
|
private String keyWord;
|
||||||
|
|
||||||
|
private int leftUid;
|
||||||
|
|
||||||
|
private String leftColumn;
|
||||||
|
|
||||||
|
private int rightUid;
|
||||||
|
|
||||||
|
private String rightColumn;
|
||||||
|
|
||||||
|
private String rightTableName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* select查询字段
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class SelectColumn {
|
||||||
|
|
||||||
|
private int uid;
|
||||||
|
|
||||||
|
private String column;
|
||||||
|
|
||||||
|
private String as;
|
||||||
|
|
||||||
|
private SelectFunc function;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (!(o instanceof SelectColumn)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
SelectColumn v = (SelectColumn) o;
|
||||||
|
return v.getUid() == this.uid
|
||||||
|
&& eq(v.getColumn(), this.column)
|
||||||
|
&& eq(v.getAs(), this.as)
|
||||||
|
&& eq(v.getFunction(), this.function);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean eq(Object o1, Object o2) {
|
||||||
|
if (o1 == null && o2 == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (o1 == null || o2 == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return o1.equals(o2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,539 @@
|
|||||||
|
package com.example.mp.mybatis.plus.wrapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.SharedString;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
|
||||||
|
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
|
||||||
|
import com.baomidou.mybatisplus.core.enums.SqlLike;
|
||||||
|
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseEntity;
|
||||||
|
import com.example.mp.mybatis.plus.func.MySFunction;
|
||||||
|
import com.example.mp.mybatis.plus.toolkit.Constant;
|
||||||
|
import com.example.mp.mybatis.plus.toolkit.MyLambdaUtils;
|
||||||
|
import com.example.mp.mybatis.plus.wrapper.interfaces.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
import static com.baomidou.mybatisplus.core.enums.SqlKeyword.*;
|
||||||
|
import static com.baomidou.mybatisplus.core.enums.WrapperKeyword.APPLY;
|
||||||
|
import static java.util.stream.Collectors.joining;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接查询Query
|
||||||
|
*
|
||||||
|
* @author yulichang
|
||||||
|
* @see com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
|
||||||
|
* @see com.baomidou.mybatisplus.core.conditions.interfaces.Compare
|
||||||
|
* @see com.baomidou.mybatisplus.core.conditions.interfaces.Join
|
||||||
|
* @see com.baomidou.mybatisplus.core.conditions.interfaces.Nested
|
||||||
|
* @see com.baomidou.mybatisplus.core.conditions.interfaces.Func
|
||||||
|
* @since 2021/01/19
|
||||||
|
*/
|
||||||
|
public class MyLambdaQueryWrapper<T extends MyBaseEntity> extends MyJoinLambdaQueryWrapper<T>
|
||||||
|
implements MyCompare<MyLambdaQueryWrapper<T>>, MyNested<MyLambdaQueryWrapper<T>, MyLambdaQueryWrapper<T>>,
|
||||||
|
MyFunc<MyLambdaQueryWrapper<T>> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表别名初始序号
|
||||||
|
*/
|
||||||
|
public static final int TABLE_ALIAS_INDEX = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 主表默认别名
|
||||||
|
*/
|
||||||
|
public static final String DEFAULT_ALIAS = "t";
|
||||||
|
|
||||||
|
public MyLambdaQueryWrapper() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyLambdaQueryWrapper(T entity) {
|
||||||
|
super.rUid = TABLE_ALIAS_INDEX;
|
||||||
|
super.setEntity(entity);
|
||||||
|
super.initNeed();
|
||||||
|
}
|
||||||
|
|
||||||
|
MyLambdaQueryWrapper(T entity, Class<T> entityClass, SharedString sqlSelect, AtomicInteger paramNameSeq,
|
||||||
|
Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
|
||||||
|
SharedString lastSql, SharedString sqlComment, SharedString sqlFirst,
|
||||||
|
int rUid, List<SelectColumn> selectColumns, List<SubTable> classList) {
|
||||||
|
super.setEntity(entity);
|
||||||
|
super.setEntityClass(entityClass);
|
||||||
|
this.paramNameSeq = paramNameSeq;
|
||||||
|
this.paramNameValuePairs = paramNameValuePairs;
|
||||||
|
this.expression = mergeSegments;
|
||||||
|
this.sqlSelect = sqlSelect;
|
||||||
|
this.lastSql = lastSql;
|
||||||
|
this.sqlComment = sqlComment;
|
||||||
|
this.sqlFirst = sqlFirst;
|
||||||
|
this.rUid = rUid;
|
||||||
|
this.selectColumnList = selectColumns;
|
||||||
|
this.classList = classList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected MyLambdaQueryWrapper<T> instance() {
|
||||||
|
return new MyLambdaQueryWrapper<>(getEntity(), getEntityClass(), null, paramNameSeq, paramNameValuePairs,
|
||||||
|
new MergeSegments(), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(),
|
||||||
|
rUid, selectColumnList, classList);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R extends MyBaseEntity, TE, RE> MyLambdaQueryWrapper<T> leftJoin(boolean condition, String alias, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return join(condition, alias, Constant.LEFT_JOIN, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R extends MyBaseEntity, TE, RE> MyLambdaQueryWrapper<T> leftJoin(MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return this.leftJoin(true, null, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R extends MyBaseEntity, TE, RE> MyLambdaQueryWrapper<T> leftJoin(String alias, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return this.leftJoin(true, alias, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R extends MyBaseEntity, TE, RE> MyLambdaQueryWrapper<T> leftJoin(boolean condition, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return this.leftJoin(condition, null, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <R extends MyBaseEntity, TE, RE> MyLambdaQueryWrapper<T> rightJoin(boolean condition, String alias, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return join(condition, alias, Constant.RIGHT_JOIN, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R extends MyBaseEntity, TE, RE> MyLambdaQueryWrapper<T> rightJoin(MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return this.rightJoin(true, null, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R extends MyBaseEntity, TE, RE> MyLambdaQueryWrapper<T> rightJoin(String alias, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return this.rightJoin(true, alias, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R extends MyBaseEntity, TE, RE> MyLambdaQueryWrapper<T> rightJoin(boolean condition, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return this.rightJoin(condition, null, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public <R extends MyBaseEntity, TE, RE> MyLambdaQueryWrapper<T> innerJoin(boolean condition, String alias, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return join(condition, alias, Constant.INNER_JOIN, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R extends MyBaseEntity, TE, RE> MyLambdaQueryWrapper<T> innerJoin(MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return this.innerJoin(true, null, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R extends MyBaseEntity, TE, RE> MyLambdaQueryWrapper<T> innerJoin(String alias, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return this.innerJoin(true, alias, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R extends MyBaseEntity, TE, RE> MyLambdaQueryWrapper<T> innerJoin(boolean condition, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return this.innerJoin(condition, null, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
private <R extends MyBaseEntity, TE, RE> MyLambdaQueryWrapper<T> join(boolean condition, String alias, String keyWord, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
if (condition) {
|
||||||
|
setEntityClass(MyLambdaUtils.getEntityClass(leftCondition));
|
||||||
|
classList.add(new SubTable(alias, keyWord, rUid, MyLambdaUtils.getColumn(leftCondition), classList.size() + 1, MyLambdaUtils.getColumn(rightCondition), TableInfoHelper.getTableInfo(MyLambdaUtils.getEntityClass(rightCondition)).getTableName()));
|
||||||
|
MyJoinLambdaQueryWrapper<R> apply = rightWrapper.apply(new MyJoinLambdaQueryWrapper<>(classList.size()));
|
||||||
|
classList.addAll(apply.classList);
|
||||||
|
this.selectColumnList.addAll(apply.selectColumnList);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
@Override
|
||||||
|
public final MyLambdaQueryWrapper<T> select(SFunction<T, ?>... columns) {
|
||||||
|
super.select(columns);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MyLambdaQueryWrapper<T> selectAll(Class<T> clazz) {
|
||||||
|
super.selectAll(clazz);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MyLambdaQueryWrapper<T> selectDistinct() {
|
||||||
|
super.selectDistinct();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MyLambdaQueryWrapper<T> select(Predicate<TableFieldInfo> predicate) {
|
||||||
|
super.select(predicate);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MyLambdaQueryWrapper<T> select(Class<T> entityClass, Predicate<TableFieldInfo> predicate) {
|
||||||
|
super.select(entityClass, predicate);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <DTO> MyLambdaQueryWrapper<T> as(SFunction<T, ?> entityColumn, SFunction<DTO, ?> DTOColumn) {
|
||||||
|
super.as(entityColumn, DTOColumn);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <DTO> MyLambdaQueryWrapper<T> asCount(MySFunction<T, ?> entityColumn, SFunction<DTO, ?> DTOColumn) {
|
||||||
|
super.asCount(entityColumn, DTOColumn);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <DTO> MyLambdaQueryWrapper<T> asSum(MySFunction<T, ?> entityColumn, SFunction<DTO, ?> DTOColumn) {
|
||||||
|
super.asSum(entityColumn, DTOColumn);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <DTO> MyLambdaQueryWrapper<T> asAvg(MySFunction<T, ?> entityColumn, SFunction<DTO, ?> DTOColumn) {
|
||||||
|
super.asAvg(entityColumn, DTOColumn);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <DTO> MyLambdaQueryWrapper<T> asMax(MySFunction<T, ?> entityColumn, SFunction<DTO, ?> DTOColumn) {
|
||||||
|
super.asMax(entityColumn, DTOColumn);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <DTO> MyLambdaQueryWrapper<T> asMin(MySFunction<T, ?> entityColumn, SFunction<DTO, ?> DTOColumn) {
|
||||||
|
super.asMin(entityColumn, DTOColumn);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <DTO> MyLambdaQueryWrapper<T> asDateFormat(MySFunction<T, ?> entityColumn, SFunction<DTO, ?> DTOColumn) {
|
||||||
|
super.asDateFormat(entityColumn, DTOColumn);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSqlSelect() {
|
||||||
|
if (StringUtils.isBlank(sqlSelect.getStringValue()) && CollectionUtils.isNotEmpty(selectColumnList)) {
|
||||||
|
List<String> collect = new ArrayList<>();
|
||||||
|
selectColumnList.forEach(c -> {
|
||||||
|
String s = Constant.TABLE_ALIAS + c.getUid() + StringPool.DOT + c.getColumn();
|
||||||
|
if (c.getFunction() != null) {
|
||||||
|
s = String.format(c.getFunction().getSql(), s);
|
||||||
|
}
|
||||||
|
if (Objects.isNull(c.getAs())) {
|
||||||
|
collect.add(s);
|
||||||
|
} else {
|
||||||
|
collect.add(s + Constant.AS + c.getAs());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.sqlSelect.setStringValue(String.join(StringPool.COMMA, collect));
|
||||||
|
}
|
||||||
|
return sqlSelect.getStringValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
super.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFrom() {
|
||||||
|
if (StringUtils.isNotBlank(from.getStringValue())) {
|
||||||
|
return from.getStringValue();
|
||||||
|
}
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
this.classList.forEach(right ->
|
||||||
|
sb.append(right.getKeyWord()).append(right.getRightTableName())
|
||||||
|
.append(StringPool.SPACE)
|
||||||
|
.append(Constant.TABLE_ALIAS)
|
||||||
|
.append(right.getRightUid())
|
||||||
|
.append(Constant.ON)
|
||||||
|
.append(Constant.TABLE_ALIAS)
|
||||||
|
.append(right.getLeftUid())
|
||||||
|
.append(StringPool.DOT)
|
||||||
|
.append(right.getLeftColumn())
|
||||||
|
.append(Constant.EQUALS)
|
||||||
|
.append(Constant.TABLE_ALIAS)
|
||||||
|
.append(right.getRightUid())
|
||||||
|
.append(StringPool.DOT)
|
||||||
|
.append(right.getRightColumn())
|
||||||
|
.append(StringPool.SPACE)
|
||||||
|
);
|
||||||
|
this.from.setStringValue(sb.toString());
|
||||||
|
return this.from.getStringValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取表前缀
|
||||||
|
*/
|
||||||
|
private String getClassTablePrefix(Class<?> tag) {
|
||||||
|
if (getEntityClass() == tag) {
|
||||||
|
return Constant.TABLE_ALIAS + rUid;
|
||||||
|
} else {
|
||||||
|
String tableName = TableInfoHelper.getTableInfo(tag).getTableName();
|
||||||
|
for (SubTable sub : classList) {
|
||||||
|
if (sub.getRightTableName().equals(tableName)) {
|
||||||
|
return Constant.TABLE_ALIAS + sub.getRightUid();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new MybatisPlusException("table not find");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取column
|
||||||
|
*/
|
||||||
|
private <E extends MyBaseEntity, F> String getColumn(MySFunction<E, F> column, String alias) {
|
||||||
|
if (alias != null) {
|
||||||
|
if (alias.equals(DEFAULT_ALIAS)) {
|
||||||
|
return Constant.TABLE_ALIAS + rUid + StringPool.DOT + column2String(column, true);
|
||||||
|
}
|
||||||
|
for (SubTable sub : classList) {
|
||||||
|
if (alias.equals(sub.getAlias())) {
|
||||||
|
return Constant.TABLE_ALIAS + sub.getRightUid() + StringPool.DOT + column2String(column, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return getClassTablePrefix(MyLambdaUtils.getEntityClass(column)) + StringPool.DOT + column2String(column, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private <E extends MyBaseEntity, F> String getColumn(String alias, MySFunction<E, F>... column) {
|
||||||
|
return Arrays.stream(column).map(i -> getColumn(i, alias)).collect(joining(StringPool.COMMA));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> eq(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), EQ,
|
||||||
|
() -> formatSql("{0}", val));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> MyLambdaQueryWrapper<T> eq(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), EQ, () -> getColumn(val, as));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> ne(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), NE, () -> formatSql("{0}", val));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> MyLambdaQueryWrapper<T> ne(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), NE, () -> getColumn(val, as));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> gt(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), GT, () -> formatSql("{0}", val));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> MyLambdaQueryWrapper<T> gt(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), GT, () -> getColumn(val, as));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> ge(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), GE, () -> formatSql("{0}", val));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> MyLambdaQueryWrapper<T> ge(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), GE, () -> getColumn(val, as));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> lt(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), LT, () -> formatSql("{0}", val));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> MyLambdaQueryWrapper<T> lt(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), LT, () -> getColumn(val, as));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> le(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), LE, () -> formatSql("{0}", val));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> MyLambdaQueryWrapper<T> le(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), LE, () -> getColumn(val, as));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> between(boolean condition, String alias, MySFunction<E, F> column, Object val1, Object val2) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), BETWEEN, () -> formatSql("{0}", val1), AND,
|
||||||
|
() -> formatSql("{0}", val2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> notBetween(boolean condition, String alias, MySFunction<E, F> column, Object val1, Object val2) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), NOT_BETWEEN, () -> formatSql("{0}", val1), AND,
|
||||||
|
() -> formatSql("{0}", val2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> like(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return likeValue(condition, LIKE, getColumn(column, alias), val, SqlLike.DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> notLike(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return likeValue(condition, NOT_LIKE, getColumn(column, alias), val, SqlLike.DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> likeLeft(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return likeValue(condition, LIKE, getColumn(column, alias), val, SqlLike.LEFT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> likeRight(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return likeValue(condition, LIKE, getColumn(column, alias), val, SqlLike.RIGHT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MyLambdaQueryWrapper<T> and(boolean condition, Consumer<MyLambdaQueryWrapper<T>> consumer) {
|
||||||
|
return and(condition).addNestedCondition(condition, consumer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MyLambdaQueryWrapper<T> or(boolean condition, Consumer<MyLambdaQueryWrapper<T>> consumer) {
|
||||||
|
return or(condition).addNestedCondition(condition, consumer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MyLambdaQueryWrapper<T> nested(boolean condition, Consumer<MyLambdaQueryWrapper<T>> consumer) {
|
||||||
|
return addNestedCondition(condition, consumer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MyLambdaQueryWrapper<T> not(boolean condition, Consumer<MyLambdaQueryWrapper<T>> consumer) {
|
||||||
|
return not(condition).addNestedCondition(condition, consumer);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private MyLambdaQueryWrapper<T> and(boolean condition) {
|
||||||
|
return doIt(condition, AND);
|
||||||
|
}
|
||||||
|
|
||||||
|
private MyLambdaQueryWrapper<T> or(boolean condition) {
|
||||||
|
return doIt(condition, OR);
|
||||||
|
}
|
||||||
|
|
||||||
|
private MyLambdaQueryWrapper<T> not(boolean condition) {
|
||||||
|
return doIt(condition, NOT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private MyLambdaQueryWrapper<T> doIt(boolean condition, ISqlSegment... sqlSegments) {
|
||||||
|
if (condition) {
|
||||||
|
expression.add(sqlSegments);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> likeValue(boolean condition, SqlKeyword keyword, String column, Object val, SqlLike sqlLike) {
|
||||||
|
return doIt(condition, () -> column, keyword, () -> formatSql("{0}", SqlUtils.concatLike(val, sqlLike)));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> addCondition(boolean condition, String column, SqlKeyword sqlKeyword, Object val) {
|
||||||
|
return doIt(condition, () -> column, sqlKeyword, () -> formatSql("{0}", val));
|
||||||
|
}
|
||||||
|
|
||||||
|
private MyLambdaQueryWrapper<T> addNestedCondition(boolean condition, Consumer<MyLambdaQueryWrapper<T>> consumer) {
|
||||||
|
if (condition) {
|
||||||
|
final MyLambdaQueryWrapper<T> instance = instance();
|
||||||
|
consumer.accept(instance);
|
||||||
|
return doIt(true, APPLY, instance);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ISqlSegment inExpression(Collection<?> value) {
|
||||||
|
return () -> value.stream().map(i -> formatSql("{0}", i))
|
||||||
|
.collect(joining(StringPool.COMMA, StringPool.LEFT_BRACKET, StringPool.RIGHT_BRACKET));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> isNull(boolean condition, String alias, MySFunction<E, F> column) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), IS_NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> isNotNull(boolean condition, String alias, MySFunction<E, F> column) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), IS_NOT_NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> in(boolean condition, String alias, MySFunction<E, F> column, Collection<?> coll) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), IN, inExpression(coll));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> notIn(boolean condition, String alias, MySFunction<E, F> column, Collection<?> coll) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), NOT_IN, inExpression(coll));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> inSql(boolean condition, String alias, MySFunction<E, F> column, String inValue) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), IN, () -> String.format("(%s)", inValue));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> notInSql(boolean condition, String alias, MySFunction<E, F> column, String inValue) {
|
||||||
|
return doIt(condition, () -> getColumn(column, alias), NOT_IN, () -> String.format("(%s)", inValue));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> groupBy(boolean condition, String alias, MySFunction<E, F>... columns) {
|
||||||
|
if (ArrayUtils.isEmpty(columns)) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
return doIt(condition, GROUP_BY,
|
||||||
|
() -> columns.length == 1 ? getColumn(columns[0], alias) : getColumn(alias, columns));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> orderBy(boolean condition, String alias, boolean isAsc, MySFunction<E, F>... columns) {
|
||||||
|
if (ArrayUtils.isEmpty(columns)) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
SqlKeyword mode = isAsc ? ASC : DESC;
|
||||||
|
for (MySFunction<E, F> column : columns) {
|
||||||
|
doIt(condition, ORDER_BY, () -> getColumn(column, alias), mode);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> having(boolean condition, String alias, String sqlHaving, Object... params) {
|
||||||
|
return doIt(condition, HAVING, () -> formatSqlIfNeed(condition, sqlHaving, params));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends MyBaseEntity, F> MyLambdaQueryWrapper<T> func(boolean condition, String alias, Consumer<MyLambdaQueryWrapper<T>> consumer) {
|
||||||
|
if (condition) {
|
||||||
|
consumer.accept(this);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.example.mp.mybatis.plus.wrapper;
|
||||||
|
|
||||||
|
public enum SelectFunc {
|
||||||
|
|
||||||
|
SUM("SUM( %s )", "求和"),
|
||||||
|
COUNT("COUNT( %s )", "数量"),
|
||||||
|
AVG("AVG( %s )", "平均值"),
|
||||||
|
MAX("MAX( %s )", "最大值"),
|
||||||
|
MIN("MIN( %s )", "最小值"),
|
||||||
|
DATE_FORMAT("DATE_FORMAT( %s ,'%Y-%m-%d %H:%i:%s')", "日期格式化");
|
||||||
|
|
||||||
|
private final String sql;
|
||||||
|
private final String desc;
|
||||||
|
|
||||||
|
SelectFunc(String sql, String desc) {
|
||||||
|
this.sql = sql;
|
||||||
|
this.desc = desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSql() {
|
||||||
|
return sql;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDesc() {
|
||||||
|
return desc;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,423 @@
|
|||||||
|
package com.example.mp.mybatis.plus.wrapper.interfaces;
|
||||||
|
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseEntity;
|
||||||
|
import com.example.mp.mybatis.plus.func.MySFunction;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* copy {@link com.baomidou.mybatisplus.core.conditions.interfaces.Compare}
|
||||||
|
*/
|
||||||
|
public interface MyCompare<Children> extends Serializable {
|
||||||
|
|
||||||
|
/* 遗弃allEq */
|
||||||
|
|
||||||
|
/* ***************************************** eq start ********************************************* */
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children eq(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children eq(boolean condition, MySFunction<E, F> column, Object val) {
|
||||||
|
return eq(condition, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children eq(String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return eq(true, alias, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children eq(MySFunction<E, F> column, Object val) {
|
||||||
|
return eq(true, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children eq(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val);
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children eq(MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return eq(true, null, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children eq(Boolean condition, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return eq(condition, null, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children eq(String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return eq(true, alias, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children eq(MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return eq(true, null, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children eq(Boolean condition, String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return eq(condition, alias, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children eq(Boolean condition, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return eq(condition, null, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children eq(String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return eq(true, alias, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ***************************************** eq end ********************************************* */
|
||||||
|
|
||||||
|
|
||||||
|
/* ***************************************** ne start ********************************************* */
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children ne(MySFunction<E, F> column, Object val) {
|
||||||
|
return ne(true, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children ne(boolean condition, MySFunction<E, F> column, Object val) {
|
||||||
|
return ne(condition, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children ne(String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return ne(true, alias, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children ne(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children ne(MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return ne(true, null, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children ne(boolean condition, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return ne(condition, null, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children ne(String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return ne(true, alias, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children ne(MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return ne(true, null, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children ne(boolean condition, String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return ne(condition, alias, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children ne(String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return ne(true, alias, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children ne(boolean condition, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return ne(condition, null, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children ne(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val);
|
||||||
|
|
||||||
|
/* ***************************************** ne end ********************************************* */
|
||||||
|
|
||||||
|
|
||||||
|
/* ***************************************** gt start ********************************************* */
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children gt(MySFunction<E, F> column, Object val) {
|
||||||
|
return gt(true, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children gt(boolean condition, MySFunction<E, F> column, Object val) {
|
||||||
|
return gt(condition, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children gt(String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return gt(true, alias, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children gt(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children gt(MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return gt(true, null, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children gt(boolean condition, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return gt(condition, null, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children gt(String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return gt(true, alias, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children gt(MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return gt(true, null, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children gt(boolean condition, String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return gt(condition, alias, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children gt(String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return gt(true, alias, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children gt(boolean condition, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return gt(condition, null, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children gt(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val);
|
||||||
|
|
||||||
|
|
||||||
|
/* ***************************************** gt end ********************************************* */
|
||||||
|
|
||||||
|
|
||||||
|
/* ***************************************** ge start ********************************************* */
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children ge(MySFunction<E, F> column, Object val) {
|
||||||
|
return ge(true, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children ge(boolean condition, MySFunction<E, F> column, Object val) {
|
||||||
|
return ge(condition, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children ge(String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return ge(true, alias, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children ge(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children ge(MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return ge(true, null, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children ge(boolean condition, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return ge(condition, null, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children ge(String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return ge(true, alias, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children ge(MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return ge(true, null, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children ge(boolean condition, String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return ge(condition, alias, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children ge(String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return ge(true, alias, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children ge(boolean condition, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return ge(condition, null, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children ge(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val);
|
||||||
|
|
||||||
|
/* ***************************************** ge end ********************************************* */
|
||||||
|
|
||||||
|
|
||||||
|
/* ***************************************** lt start ********************************************* */
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children lt(MySFunction<E, F> column, Object val) {
|
||||||
|
return lt(true, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children lt(boolean condition, MySFunction<E, F> column, Object val) {
|
||||||
|
return lt(condition, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children lt(String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return lt(true, alias, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children lt(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children lt(MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return lt(true, null, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children lt(boolean condition, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return lt(condition, null, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children lt(String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return lt(true, alias, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children lt(MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return lt(true, null, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children lt(boolean condition, String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return lt(condition, alias, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children lt(String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return lt(true, alias, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children lt(boolean condition, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return lt(condition, null, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children lt(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val);
|
||||||
|
|
||||||
|
/* ***************************************** lt end ********************************************* */
|
||||||
|
|
||||||
|
|
||||||
|
/* ***************************************** lt start ********************************************* */
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children le(MySFunction<E, F> column, Object val) {
|
||||||
|
return le(true, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children le(boolean condition, MySFunction<E, F> column, Object val) {
|
||||||
|
return le(condition, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children le(String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return le(true, alias, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children le(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children le(MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return le(true, null, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children le(boolean condition, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return le(condition, null, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children le(String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return le(true, alias, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children le(MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return le(true, null, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children le(boolean condition, String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||||
|
return le(condition, alias, column, null, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children le(String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return le(true, alias, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children le(boolean condition, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||||
|
return le(condition, null, column, as, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F, X extends MyBaseEntity, Y> Children le(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val);
|
||||||
|
|
||||||
|
/* ***************************************** le end ********************************************* */
|
||||||
|
|
||||||
|
/* ***************************************** between start ********************************************* */
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children between(MySFunction<E, F> column, Object val1, Object val2) {
|
||||||
|
return between(true, null, column, val1, val2);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children between(boolean condition, MySFunction<E, F> column, Object val1, Object val2) {
|
||||||
|
return between(condition, null, column, val1, val2);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children between(String alias, MySFunction<E, F> column, Object val1, Object val2) {
|
||||||
|
return between(true, alias, column, val1, val2);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children between(boolean condition, String alias, MySFunction<E, F> column, Object val1, Object val2);
|
||||||
|
|
||||||
|
/* ***************************************** between end ********************************************* */
|
||||||
|
|
||||||
|
|
||||||
|
/* ***************************************** notBetween start ********************************************* */
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children notBetween(MySFunction<E, F> column, Object val1, Object val2) {
|
||||||
|
return notBetween(true, null, column, val1, val2);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children notBetween(boolean condition, MySFunction<E, F> column, Object val1, Object val2) {
|
||||||
|
return notBetween(condition, null, column, val1, val2);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children notBetween(String alias, MySFunction<E, F> column, Object val1, Object val2) {
|
||||||
|
return notBetween(true, alias, column, val1, val2);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children notBetween(boolean condition, String alias, MySFunction<E, F> column, Object val1, Object val2);
|
||||||
|
|
||||||
|
/* ***************************************** notBetween end ********************************************* */
|
||||||
|
|
||||||
|
|
||||||
|
/* ***************************************** like start ********************************************* */
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children like(MySFunction<E, F> column, Object val) {
|
||||||
|
return like(true, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children like(boolean condition, MySFunction<E, F> column, Object val) {
|
||||||
|
return like(condition, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children like(String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return like(true, alias, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children like(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||||
|
|
||||||
|
/* ***************************************** like end ********************************************* */
|
||||||
|
|
||||||
|
|
||||||
|
/* ***************************************** notLike start ********************************************* */
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children notLike(MySFunction<E, F> column, Object val) {
|
||||||
|
return notLike(true, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children notLike(boolean condition, MySFunction<E, F> column, Object val) {
|
||||||
|
return notLike(condition, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children notLike(String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return notLike(true, alias, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children notLike(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||||
|
|
||||||
|
/* ***************************************** notLike end ********************************************* */
|
||||||
|
|
||||||
|
|
||||||
|
/* ***************************************** likeLeft start ********************************************* */
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children likeLeft(MySFunction<E, F> column, Object val) {
|
||||||
|
return likeLeft(true, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children likeLeft(boolean condition, MySFunction<E, F> column, Object val) {
|
||||||
|
return likeLeft(condition, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children likeLeft(String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return likeLeft(true, alias, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children likeLeft(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||||
|
|
||||||
|
/* ***************************************** likeLeft end ********************************************* */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* ***************************************** likeRight start ********************************************* */
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children likeRight(MySFunction<E, F> column, Object val) {
|
||||||
|
return likeRight(true, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children likeRight(boolean condition, MySFunction<E, F> column, Object val) {
|
||||||
|
return likeRight(condition, null, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children likeRight(String alias, MySFunction<E, F> column, Object val) {
|
||||||
|
return likeRight(true, alias, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children likeRight(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||||
|
|
||||||
|
/* ***************************************** likeRight end ********************************************* */
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,214 @@
|
|||||||
|
package com.example.mp.mybatis.plus.wrapper.interfaces;
|
||||||
|
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseEntity;
|
||||||
|
import com.example.mp.mybatis.plus.func.MySFunction;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import static java.util.stream.Collectors.toList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* copy {@link com.baomidou.mybatisplus.core.conditions.interfaces.Func}
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public interface MyFunc<Children> extends Serializable {
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children isNull(MySFunction<E, F> column) {
|
||||||
|
return isNull(true, null, column);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children isNull(boolean condition, MySFunction<E, F> column) {
|
||||||
|
return isNull(condition, null, column);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children isNull(String alias, MySFunction<E, F> column) {
|
||||||
|
return isNull(true, alias, column);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children isNull(boolean condition, String alias, MySFunction<E, F> column);
|
||||||
|
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children isNotNull(MySFunction<E, F> column) {
|
||||||
|
return isNotNull(true, column);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children isNotNull(boolean condition, MySFunction<E, F> column) {
|
||||||
|
return isNotNull(condition, null, column);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children isNotNull(String alias, MySFunction<E, F> column) {
|
||||||
|
return isNotNull(true, alias, column);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children isNotNull(boolean condition, String alias, MySFunction<E, F> column);
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children in(MySFunction<E, F> column, Collection<?> coll) {
|
||||||
|
return in(true, null, column, coll);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children in(boolean condition, MySFunction<E, F> column, Collection<?> coll) {
|
||||||
|
return in(condition, null, column, coll);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children in(String alias, MySFunction<E, F> column, Collection<?> coll) {
|
||||||
|
return in(true, alias, column, coll);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children in(boolean condition, String alias, MySFunction<E, F> column, Collection<?> coll);
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children in(MySFunction<E, F> column, Object... values) {
|
||||||
|
return in(true, null, column, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children in(boolean condition, MySFunction<E, F> column, Object... values) {
|
||||||
|
return in(condition, null, column, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children in(String alias, MySFunction<E, F> column, Object... values) {
|
||||||
|
return in(true, alias, column, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children in(boolean condition, String alias, MySFunction<E, F> column, Object... values) {
|
||||||
|
return in(condition, alias, column, Arrays.stream(Optional.ofNullable(values).orElseGet(() -> new Object[]{}))
|
||||||
|
.collect(toList()));
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children notIn(MySFunction<E, F> column, Collection<?> coll) {
|
||||||
|
return notIn(true, null, column, coll);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children notIn(boolean condition, MySFunction<E, F> column, Collection<?> coll) {
|
||||||
|
return notIn(condition, null, column, coll);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children notIn(String alias, MySFunction<E, F> column, Collection<?> coll) {
|
||||||
|
return notIn(true, alias, column, coll);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children notIn(boolean condition, String alias, MySFunction<E, F> column, Collection<?> coll);
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children notIn(MySFunction<E, F> column, Object... value) {
|
||||||
|
return notIn(true, null, column, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children notIn(boolean condition, MySFunction<E, F> column, Object... value) {
|
||||||
|
return notIn(condition, null, column, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children notIn(String alias, MySFunction<E, F> column, Object... value) {
|
||||||
|
return notIn(true, alias, column, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children notIn(boolean condition, String alias, MySFunction<E, F> column, Object... values) {
|
||||||
|
return notIn(condition, alias, column, Arrays.stream(Optional.ofNullable(values).orElseGet(() -> new Object[]{}))
|
||||||
|
.collect(toList()));
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children inSql(MySFunction<E, F> column, String inValue) {
|
||||||
|
return inSql(true, null, column, inValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children inSql(boolean condition, MySFunction<E, F> column, String inValue) {
|
||||||
|
return inSql(condition, null, column, inValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children inSql(String alias, MySFunction<E, F> column, String inValue) {
|
||||||
|
return inSql(true, alias, column, inValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children inSql(boolean condition, String alias, MySFunction<E, F> column, String inValue);
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children notInSql(MySFunction<E, F> column, String inValue) {
|
||||||
|
return notInSql(true, null, column, inValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children notInSql(boolean condition, MySFunction<E, F> column, String inValue) {
|
||||||
|
return notInSql(condition, null, column, inValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children notInSql(String alias, MySFunction<E, F> column, String inValue) {
|
||||||
|
return notInSql(true, alias, column, inValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children notInSql(boolean condition, String alias, MySFunction<E, F> column, String inValue);
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children groupBy(MySFunction<E, F>... columns) {
|
||||||
|
return groupBy(true, null, columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children groupBy(boolean condition, MySFunction<E, F>... columns) {
|
||||||
|
return groupBy(condition, null, columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children groupBy(String alias, MySFunction<E, F>... columns) {
|
||||||
|
return groupBy(true, alias, columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children groupBy(boolean condition, String alias, MySFunction<E, F>... columns);
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children orderByAsc(MySFunction<E, F>... columns) {
|
||||||
|
return orderByAsc(true, null, columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children orderByAsc(boolean condition, MySFunction<E, F>... columns) {
|
||||||
|
return orderByAsc(condition, null, columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children orderByAsc(String alias, MySFunction<E, F>... columns) {
|
||||||
|
return orderByAsc(true, alias, columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children orderByAsc(boolean condition, String alias, MySFunction<E, F>... columns) {
|
||||||
|
return orderBy(condition, alias, true, columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children orderByDesc(MySFunction<E, F>... columns) {
|
||||||
|
return orderByDesc(true, null, columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children orderByDesc(boolean condition, MySFunction<E, F>... columns) {
|
||||||
|
return orderByDesc(condition, null, columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children orderByDesc(String alias, MySFunction<E, F>... columns) {
|
||||||
|
return orderByDesc(true, alias, columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children orderByDesc(boolean condition, String alias, MySFunction<E, F>... columns) {
|
||||||
|
return orderBy(condition, alias, false, columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children orderBy(boolean condition, String alias, boolean isAsc, MySFunction<E, F>... columns);
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children having(String sqlHaving, Object... params) {
|
||||||
|
return having(true, null, sqlHaving, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children having(boolean condition, String sqlHaving, Object... params) {
|
||||||
|
return having(condition, null, sqlHaving, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children having(String alias, String sqlHaving, Object... params) {
|
||||||
|
return having(true, alias, sqlHaving, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children having(boolean condition, String alias, String sqlHaving, Object... params);
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children func(Consumer<Children> consumer) {
|
||||||
|
return func(true, null, consumer);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children func(boolean condition, Consumer<Children> consumer) {
|
||||||
|
return func(condition, null, consumer);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <E extends MyBaseEntity, F> Children func(String alias, Consumer<Children> consumer) {
|
||||||
|
return func(true, alias, consumer);
|
||||||
|
}
|
||||||
|
|
||||||
|
<E extends MyBaseEntity, F> Children func(boolean condition, String alias, Consumer<Children> consumer);
|
||||||
|
}
|
@ -0,0 +1,90 @@
|
|||||||
|
package com.example.mp.mybatis.plus.wrapper.interfaces;
|
||||||
|
|
||||||
|
import com.example.mp.mybatis.plus.base.MyBaseEntity;
|
||||||
|
import com.example.mp.mybatis.plus.func.MySFunction;
|
||||||
|
import com.example.mp.mybatis.plus.wrapper.MyJoinLambdaQueryWrapper;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author yulichang
|
||||||
|
*/
|
||||||
|
public interface MyJoin<Children, T extends MyBaseEntity> {
|
||||||
|
|
||||||
|
|
||||||
|
<R extends MyBaseEntity, TE, RE> Children leftJoin(boolean condition,
|
||||||
|
String alias,
|
||||||
|
MySFunction<T, TE> leftCondition,
|
||||||
|
MySFunction<R, RE> rightCondition,
|
||||||
|
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper);
|
||||||
|
|
||||||
|
|
||||||
|
default <R extends MyBaseEntity, TE, RE> Children leftJoin(MySFunction<T, TE> leftCondition,
|
||||||
|
MySFunction<R, RE> rightCondition,
|
||||||
|
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return leftJoin(true, null, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <R extends MyBaseEntity, TE, RE> Children leftJoin(String alias, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return leftJoin(true, alias, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <R extends MyBaseEntity, TE, RE> Children leftJoin(boolean condition,
|
||||||
|
MySFunction<T, TE> leftCondition,
|
||||||
|
MySFunction<R, RE> rightCondition,
|
||||||
|
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return leftJoin(condition, null, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
<R extends MyBaseEntity, TE, RE> Children rightJoin(boolean condition,
|
||||||
|
String alias,
|
||||||
|
MySFunction<T, TE> leftCondition,
|
||||||
|
MySFunction<R, RE> rightCondition,
|
||||||
|
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper);
|
||||||
|
|
||||||
|
|
||||||
|
default <R extends MyBaseEntity, TE, RE> Children rightJoin(MySFunction<T, TE> leftCondition,
|
||||||
|
MySFunction<R, RE> rightCondition,
|
||||||
|
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return rightJoin(true, null, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <R extends MyBaseEntity, TE, RE> Children rightJoin(String alias, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return rightJoin(true, alias, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <R extends MyBaseEntity, TE, RE> Children rightJoin(boolean condition,
|
||||||
|
MySFunction<T, TE> leftCondition,
|
||||||
|
MySFunction<R, RE> rightCondition,
|
||||||
|
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return rightJoin(condition, null, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
<R extends MyBaseEntity, TE, RE> Children innerJoin(boolean condition,
|
||||||
|
String alias,
|
||||||
|
MySFunction<T, TE> leftCondition,
|
||||||
|
MySFunction<R, RE> rightCondition,
|
||||||
|
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper);
|
||||||
|
|
||||||
|
|
||||||
|
default <R extends MyBaseEntity, TE, RE> Children innerJoin(MySFunction<T, TE> leftCondition,
|
||||||
|
MySFunction<R, RE> rightCondition,
|
||||||
|
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return innerJoin(true, null, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <R extends MyBaseEntity, TE, RE> Children innerJoin(String alias, MySFunction<T, TE> leftCondition, MySFunction<R, RE> rightCondition, Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return innerJoin(true, alias, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <R extends MyBaseEntity, TE, RE> Children innerJoin(boolean condition,
|
||||||
|
MySFunction<T, TE> leftCondition,
|
||||||
|
MySFunction<R, RE> rightCondition,
|
||||||
|
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||||
|
return innerJoin(condition, null, leftCondition, rightCondition, rightWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,130 @@
|
|||||||
|
package com.example.mp.mybatis.plus.wrapper.interfaces;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* copy {@link com.baomidou.mybatisplus.core.conditions.interfaces.Join}
|
||||||
|
*/
|
||||||
|
public interface MyMPJoin<Children> extends Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default Children or() {
|
||||||
|
return or(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拼接 OR
|
||||||
|
*
|
||||||
|
* @param condition 执行条件
|
||||||
|
* @return children
|
||||||
|
*/
|
||||||
|
Children or(boolean condition);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default Children apply(String applySql, Object... value) {
|
||||||
|
return apply(true, applySql, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拼接 sql
|
||||||
|
* <p>!! 会有 sql 注入风险 !!</p>
|
||||||
|
* <p>例1: apply("id = 1")</p>
|
||||||
|
* <p>例2: apply("date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")</p>
|
||||||
|
* <p>例3: apply("date_format(dateColumn,'%Y-%m-%d') = {0}", LocalDate.now())</p>
|
||||||
|
*
|
||||||
|
* @param condition 执行条件
|
||||||
|
* @return children
|
||||||
|
*/
|
||||||
|
Children apply(boolean condition, String applySql, Object... value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default Children last(String lastSql) {
|
||||||
|
return last(true, lastSql);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 无视优化规则直接拼接到 sql 的最后(有sql注入的风险,请谨慎使用)
|
||||||
|
* <p>例: last("limit 1")</p>
|
||||||
|
* <p>注意只能调用一次,多次调用以最后一次为准</p>
|
||||||
|
*
|
||||||
|
* @param condition 执行条件
|
||||||
|
* @param lastSql sql语句
|
||||||
|
* @return children
|
||||||
|
*/
|
||||||
|
Children last(boolean condition, String lastSql);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default Children comment(String comment) {
|
||||||
|
return comment(true, comment);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sql 注释(会拼接在 sql 的最后面)
|
||||||
|
*
|
||||||
|
* @param condition 执行条件
|
||||||
|
* @param comment sql注释
|
||||||
|
* @return children
|
||||||
|
*/
|
||||||
|
Children comment(boolean condition, String comment);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default Children first(String firstSql) {
|
||||||
|
return first(true, firstSql);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sql 起始句(会拼接在SQL语句的起始处)
|
||||||
|
*
|
||||||
|
* @param condition 执行条件
|
||||||
|
* @param firstSql 起始语句
|
||||||
|
* @return children
|
||||||
|
* @since 3.3.1
|
||||||
|
*/
|
||||||
|
Children first(boolean condition, String firstSql);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default Children exists(String existsSql) {
|
||||||
|
return exists(true, existsSql);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拼接 EXISTS ( sql语句 )
|
||||||
|
* <p>!! sql 注入方法 !!</p>
|
||||||
|
* <p>例: exists("select id from table where age = 1")</p>
|
||||||
|
*
|
||||||
|
* @param condition 执行条件
|
||||||
|
* @param existsSql sql语句
|
||||||
|
* @return children
|
||||||
|
*/
|
||||||
|
Children exists(boolean condition, String existsSql);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default Children notExists(String existsSql) {
|
||||||
|
return notExists(true, existsSql);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拼接 NOT EXISTS ( sql语句 )
|
||||||
|
* <p>!! sql 注入方法 !!</p>
|
||||||
|
* <p>例: notExists("select id from table where age = 1")</p>
|
||||||
|
*
|
||||||
|
* @param condition 执行条件
|
||||||
|
* @param existsSql sql语句
|
||||||
|
* @return children
|
||||||
|
*/
|
||||||
|
Children notExists(boolean condition, String existsSql);
|
||||||
|
}
|
@ -0,0 +1,87 @@
|
|||||||
|
package com.example.mp.mybatis.plus.wrapper.interfaces;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* copy {@link com.baomidou.mybatisplus.core.conditions.interfaces.Nested}
|
||||||
|
*/
|
||||||
|
public interface MyNested<Param, Children> extends Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default Children and(Consumer<Param> consumer) {
|
||||||
|
return and(true, consumer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AND 嵌套
|
||||||
|
* <p>
|
||||||
|
* 例: and(i -> i.eq("name", "李白").ne("status", "活着"))
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param condition 执行条件
|
||||||
|
* @param consumer 消费函数
|
||||||
|
* @return children
|
||||||
|
*/
|
||||||
|
Children and(boolean condition, Consumer<Param> consumer);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default Children or(Consumer<Param> consumer) {
|
||||||
|
return or(true, consumer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OR 嵌套
|
||||||
|
* <p>
|
||||||
|
* 例: or(i -> i.eq("name", "李白").ne("status", "活着"))
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param condition 执行条件
|
||||||
|
* @param consumer 消费函数
|
||||||
|
* @return children
|
||||||
|
*/
|
||||||
|
Children or(boolean condition, Consumer<Param> consumer);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default Children nested(Consumer<Param> consumer) {
|
||||||
|
return nested(true, consumer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 正常嵌套 不带 AND 或者 OR
|
||||||
|
* <p>
|
||||||
|
* 例: nested(i -> i.eq("name", "李白").ne("status", "活着"))
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param condition 执行条件
|
||||||
|
* @param consumer 消费函数
|
||||||
|
* @return children
|
||||||
|
*/
|
||||||
|
Children nested(boolean condition, Consumer<Param> consumer);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default Children not(Consumer<Param> consumer) {
|
||||||
|
return not(true, consumer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* not嵌套
|
||||||
|
* <p>
|
||||||
|
* 例: not(i -> i.eq("name", "李白").ne("status", "活着"))
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param condition 执行条件
|
||||||
|
* @param consumer 消费函数
|
||||||
|
* @return children
|
||||||
|
*/
|
||||||
|
Children not(boolean condition, Consumer<Param> consumer);
|
||||||
|
}
|
14
src/main/resources/application.yml
Normal file
14
src/main/resources/application.yml
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
spring:
|
||||||
|
datasource:
|
||||||
|
type: com.alibaba.druid.pool.DruidDataSource
|
||||||
|
druid:
|
||||||
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
url: jdbc:mysql://localhost:3306/mp-test?useSSL=false&serverTimezone=Asia/Shanghai
|
||||||
|
username: root
|
||||||
|
password: 123456
|
||||||
|
|
||||||
|
mybatis-plus:
|
||||||
|
global-config:
|
||||||
|
banner: false
|
||||||
|
configuration:
|
||||||
|
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
135
src/test/java/com/example/mp/MpJoinTest.java
Normal file
135
src/test/java/com/example/mp/MpJoinTest.java
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
package com.example.mp;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.example.mp.business.dto.UserDTO;
|
||||||
|
import com.example.mp.business.entity.UserAddressEntity;
|
||||||
|
import com.example.mp.business.entity.UserEntity;
|
||||||
|
import com.example.mp.business.mapper.UserMapper;
|
||||||
|
import com.example.mp.business.service.UserService;
|
||||||
|
import com.example.mp.mybatis.plus.wrapper.MyLambdaQueryWrapper;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表
|
||||||
|
*/
|
||||||
|
@SpringBootTest
|
||||||
|
class MpJoinTest {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快速开始
|
||||||
|
* 简单的连表查询
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void quickStart() {
|
||||||
|
//简单的连表查询 --> user left join user_address
|
||||||
|
//mapper
|
||||||
|
List<UserDTO> dtoList1 = userMapper.selectJoinList(new MyLambdaQueryWrapper<UserEntity>()
|
||||||
|
.selectAll(UserEntity.class)//查询全部字段,mp中默认全部字段,但是这默认为空,class按道理可以不传,从泛型中获取,目前还没有想到解决方案
|
||||||
|
.leftJoin(UserEntity::getId, UserAddressEntity::getUserId,// left join -> sql中join后面on的两个比骄傲字段
|
||||||
|
right -> right.selectAll(UserAddressEntity.class)),
|
||||||
|
UserDTO.class);
|
||||||
|
log(dtoList1.get(0).getTel());
|
||||||
|
|
||||||
|
//service实现
|
||||||
|
List<UserDTO> dtoList2 = userService.selectJoinList(new MyLambdaQueryWrapper<UserEntity>()
|
||||||
|
.selectAll(UserEntity.class)
|
||||||
|
//同样支持条件判断 false不会进行连表 right join(还有 inner join) -> sql中join后面on的两个比较字段
|
||||||
|
.rightJoin(true, UserEntity::getId, UserAddressEntity::getUserId,
|
||||||
|
right -> right.selectAll(UserAddressEntity.class)),
|
||||||
|
UserDTO.class);
|
||||||
|
log(dtoList2.get(0).getHeadImg());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 别名用法(as)
|
||||||
|
* 简单的连表查询
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void test1() {
|
||||||
|
//连表查询 --> user left join user_address
|
||||||
|
//mapper
|
||||||
|
List<UserDTO> dtoList1 = userMapper.selectJoinList(new MyLambdaQueryWrapper<UserEntity>()
|
||||||
|
.selectAll(UserEntity.class)
|
||||||
|
.as(UserEntity::getId, UserDTO::getId)//sql as 当数据库字段与属性不匹配是使用, user.id as UserDTO.id
|
||||||
|
.leftJoin(UserEntity::getId, UserAddressEntity::getUserId, right -> right
|
||||||
|
.asSum(UserAddressEntity::getAddress, UserDTO::getAddress)//as 的拓展 sum() 还有 count avg max min 等...
|
||||||
|
.selectAll(UserAddressEntity.class)),
|
||||||
|
UserDTO.class);
|
||||||
|
log(dtoList1.get(0).getTel());
|
||||||
|
//service 略...
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 待条件的连表查询
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void test2() {
|
||||||
|
//连表查询 --> user left join user_address
|
||||||
|
//mapper
|
||||||
|
List<UserDTO> dtoList1 = userMapper.selectJoinList(new MyLambdaQueryWrapper<UserEntity>()
|
||||||
|
.selectAll(UserEntity.class)
|
||||||
|
.leftJoin(UserEntity::getId, UserAddressEntity::getUserId, right -> right
|
||||||
|
.selectAll(UserAddressEntity.class))
|
||||||
|
.eq(true, UserEntity::getId, 1)//支持条件判断
|
||||||
|
.like(UserAddressEntity::getTel, "1")//支持所有join的表字段 UserEntity 或 UserAddressEntity都可以
|
||||||
|
.eq(UserEntity::getId, UserAddressEntity::getUserId)//甚至可以字段与字段比较 UserEntity.id = UserAddressEntity.id
|
||||||
|
/* 其他 .ne() .gt() .ge() .... 请参考 mybatis-plus */
|
||||||
|
, UserDTO.class);
|
||||||
|
log(dtoList1.get(0).getTel());
|
||||||
|
//service 略...
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表分页查询 (请配置mybatis-plus 分页插件)
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void test3() {
|
||||||
|
//连表查询 --> user left join user_address
|
||||||
|
//mapper
|
||||||
|
IPage<UserDTO> page = userMapper.selectJoinPage(new Page<>(1, 10), new MyLambdaQueryWrapper<UserEntity>()
|
||||||
|
.selectAll(UserEntity.class)
|
||||||
|
.leftJoin(UserEntity::getId, UserAddressEntity::getUserId, right -> right
|
||||||
|
.selectAll(UserAddressEntity.class))
|
||||||
|
.eq(true, UserEntity::getId, 1)//支持条件判断
|
||||||
|
.like(UserAddressEntity::getTel, "1")//支持所有join的表字段 UserEntity 或 UserAddressEntity都可以
|
||||||
|
.eq(UserEntity::getId, UserAddressEntity::getUserId)//甚至可以字段与字段比较 UserEntity.id = UserAddressEntity.id
|
||||||
|
/* 其他 .ne() .gt() .ge() .... 请参考 mybatis-plus */
|
||||||
|
, UserDTO.class);
|
||||||
|
log(page.getRecords().get(0).getTel());
|
||||||
|
//service 略...
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义别名支持
|
||||||
|
* 如果表重复(自连接,或join同一张表多次 需要用到别名)
|
||||||
|
* <p>
|
||||||
|
* 不只是两张表,这个框架没有限制join数量,理论上可以无限join但不建议 [狗头]
|
||||||
|
* <p>
|
||||||
|
* 自己去探索发现吧!
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void test4() {
|
||||||
|
//todo
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void log(String format, String... args) {
|
||||||
|
System.err.println(String.format(format, args));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void log(String msg) {
|
||||||
|
System.err.println(msg);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user