diff --git a/README.md b/README.md index 401006a..5c1c52a 100644 --- a/README.md +++ b/README.md @@ -14,13 +14,13 @@ QQ群:1022221898 ```xml com.github.yulichang - mybatis-plus-join - 1.3.5 + mybatis-plus-join-boot-starter + 1.3.8 ``` - Gradle ``` - implementation 'com.github.yulichang:mybatis-plus-join:1.3.5' + implementation 'com.github.yulichang:mybatis-plus-join-boot-starter:1.3.8' ``` 或者clone代码到本地执行 mvn install, 再引入以上依赖
diff --git a/mybatis-plus-join-annotation/pom.xml b/mybatis-plus-join-annotation/pom.xml new file mode 100644 index 0000000..cc58eeb --- /dev/null +++ b/mybatis-plus-join-annotation/pom.xml @@ -0,0 +1,54 @@ + + + + 4.0.0 + + com.github.yulichang + mybatis-plus-join-root + 1.3.8 + + 1.3.8 + mybatis-plus-join-annotation + mybatis-plus-join-annotation + + An enhanced toolkit of Mybatis-Plus to simplify development. + https://github.com/yulichang/mybatis-plus-join + + + The Apache License, Version 2.0 + https://www.apache.org/licenses/LICENSE-2.0.txt + + + + + mybatis-plus-join + yulichang + yu_lichang@qq.com + + + + scm:git:https://github.com/yulichang/mybatis-plus-join.git + scm:git:https://github.com/yulichang/mybatis-plus-join.git + https://github.com/yulichang/mybatis-plus-join + + + + 1.8 + 1.8 + 1.8 + 1.8 + github + UTF-8 + + + + + com.baomidou + mybatis-plus-core + 3.5.2 + provided + + + \ No newline at end of file diff --git a/src/main/java/com/github/yulichang/annotation/EntityMapping.java b/mybatis-plus-join-annotation/src/main/java/com/github/yulichang/annotation/EntityMapping.java similarity index 99% rename from src/main/java/com/github/yulichang/annotation/EntityMapping.java rename to mybatis-plus-join-annotation/src/main/java/com/github/yulichang/annotation/EntityMapping.java index 5d6ffdd..847cee2 100644 --- a/src/main/java/com/github/yulichang/annotation/EntityMapping.java +++ b/mybatis-plus-join-annotation/src/main/java/com/github/yulichang/annotation/EntityMapping.java @@ -10,6 +10,7 @@ import java.lang.annotation.*; * @since 1.2.0 */ @Documented +@SuppressWarnings("unused") @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) public @interface EntityMapping { diff --git a/src/main/java/com/github/yulichang/annotation/FieldMapping.java b/mybatis-plus-join-annotation/src/main/java/com/github/yulichang/annotation/FieldMapping.java similarity index 98% rename from src/main/java/com/github/yulichang/annotation/FieldMapping.java rename to mybatis-plus-join-annotation/src/main/java/com/github/yulichang/annotation/FieldMapping.java index a61c7db..5f310a0 100644 --- a/src/main/java/com/github/yulichang/annotation/FieldMapping.java +++ b/mybatis-plus-join-annotation/src/main/java/com/github/yulichang/annotation/FieldMapping.java @@ -10,6 +10,7 @@ import java.lang.annotation.*; * @since 1.2.0 */ @Documented +@SuppressWarnings("unused") @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) public @interface FieldMapping { diff --git a/src/main/java/com/github/yulichang/annotation/MPJMappingApply.java b/mybatis-plus-join-annotation/src/main/java/com/github/yulichang/annotation/MPJMappingApply.java similarity index 92% rename from src/main/java/com/github/yulichang/annotation/MPJMappingApply.java rename to mybatis-plus-join-annotation/src/main/java/com/github/yulichang/annotation/MPJMappingApply.java index 8784957..ded2e61 100644 --- a/src/main/java/com/github/yulichang/annotation/MPJMappingApply.java +++ b/mybatis-plus-join-annotation/src/main/java/com/github/yulichang/annotation/MPJMappingApply.java @@ -7,6 +7,7 @@ package com.github.yulichang.annotation; * @author yulichang * @since 1.2.0 */ +@SuppressWarnings("unused") public @interface MPJMappingApply { /** diff --git a/src/main/java/com/github/yulichang/annotation/MPJMappingCondition.java b/mybatis-plus-join-annotation/src/main/java/com/github/yulichang/annotation/MPJMappingCondition.java similarity index 95% rename from src/main/java/com/github/yulichang/annotation/MPJMappingCondition.java rename to mybatis-plus-join-annotation/src/main/java/com/github/yulichang/annotation/MPJMappingCondition.java index da024a5..8c09726 100644 --- a/src/main/java/com/github/yulichang/annotation/MPJMappingCondition.java +++ b/mybatis-plus-join-annotation/src/main/java/com/github/yulichang/annotation/MPJMappingCondition.java @@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.core.enums.SqlKeyword; * @author yulichang * @since 1.2.0 */ +@SuppressWarnings("unused") public @interface MPJMappingCondition { /** diff --git a/mybatis-plus-join-boot-starter/pom.xml b/mybatis-plus-join-boot-starter/pom.xml new file mode 100644 index 0000000..1c37bc0 --- /dev/null +++ b/mybatis-plus-join-boot-starter/pom.xml @@ -0,0 +1,71 @@ + + + + 4.0.0 + + com.github.yulichang + mybatis-plus-join-root + 1.3.8 + + 1.3.8 + mybatis-plus-join-boot-starter + mybatis-plus-join-boot-starter + + An enhanced toolkit of Mybatis-Plus to simplify development. + https://github.com/yulichang/mybatis-plus-join + + + The Apache License, Version 2.0 + https://www.apache.org/licenses/LICENSE-2.0.txt + + + + + mybatis-plus-join + yulichang + yu_lichang@qq.com + + + + scm:git:https://github.com/yulichang/mybatis-plus-join.git + scm:git:https://github.com/yulichang/mybatis-plus-join.git + https://github.com/yulichang/mybatis-plus-join + + + + 1.8 + 1.8 + 1.8 + 1.8 + github + UTF-8 + + + + + com.github.yulichang + mybatis-plus-join-core + 1.3.8 + + + org.springframework.boot + spring-boot-configuration-processor + true + 2.7.5 + + + com.baomidou + mybatis-plus-boot-starter + 3.5.2 + provided + + + org.projectlombok + lombok + 1.18.24 + provided + + + \ No newline at end of file diff --git a/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinAutoConfiguration.java b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinAutoConfiguration.java new file mode 100644 index 0000000..bbb011b --- /dev/null +++ b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinAutoConfiguration.java @@ -0,0 +1,128 @@ +package com.github.yulichang.autoconfigure; + +import com.baomidou.mybatisplus.autoconfigure.MybatisPlusLanguageDriverAutoConfiguration; +import com.baomidou.mybatisplus.core.injector.AbstractSqlInjector; +import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; +import com.baomidou.mybatisplus.core.injector.ISqlInjector; +import com.github.yulichang.config.ConfigProperties; +import com.github.yulichang.config.InterceptorConfig; +import com.github.yulichang.injector.MPJSqlInjector; +import com.github.yulichang.interceptor.MPJInterceptor; +import com.github.yulichang.toolkit.SpringContentUtils; +import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionFactoryBean; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.BeansException; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ApplicationListener; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; + +import javax.sql.DataSource; +import java.util.List; + +/** + * springboot 自动配置类 + * + * @author yulichang + * @since 1.3.7 + */ +@SuppressWarnings("unused") +@Configuration(proxyBeanMethods = false) +@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class}) +@ConditionalOnSingleCandidate(DataSource.class) +@EnableConfigurationProperties(MybatisPlusJoinProperties.class) +@AutoConfigureAfter({DataSourceAutoConfiguration.class, MybatisPlusLanguageDriverAutoConfiguration.class}) +public class MybatisPlusJoinAutoConfiguration { + + private static final Logger logger = LoggerFactory.getLogger(MybatisPlusJoinAutoConfiguration.class); + + + @SuppressWarnings("FieldCanBeLocal") + private final MybatisPlusJoinProperties properties; + + + public MybatisPlusJoinAutoConfiguration(MybatisPlusJoinProperties properties) { + this.properties = properties; + ConfigProperties.subTableLogic = properties.getSubTableLogic(); + } + + /** + * mybatis plus join 拦截器 + */ + @Bean + public MPJInterceptor mpjInterceptor() { + return new MPJInterceptor(); + } + + /** + * mybatis plus 拦截器配置 + */ + @Bean + @ConditionalOnBean(SqlSessionFactory.class) + public InterceptorConfig interceptorConfig(List sqlSessionFactoryList) { + return new InterceptorConfig(sqlSessionFactoryList, properties.getBanner()); + } + + /** + * mybatis plus join 自定义方法 + */ + @Bean + @Order(Ordered.HIGHEST_PRECEDENCE) + @ConditionalOnMissingBean({DefaultSqlInjector.class, AbstractSqlInjector.class, ISqlInjector.class}) + public MPJSqlInjector mpjSqlInjector() { + return new MPJSqlInjector(); + } + + /** + * springboot content 工具类 + */ + @Bean + @Order(Ordered.HIGHEST_PRECEDENCE) + @SuppressWarnings("InstantiationOfUtilityClass") + public SpringContentUtils springContentUtils(SpringContext springContext) { + return new SpringContentUtils(springContext); + } + + @Configuration + @Order(Ordered.HIGHEST_PRECEDENCE) + @ConditionalOnBean(SqlSessionFactory.class) + public static class MappingConfig implements ApplicationListener { + @Override + @SuppressWarnings("NullableProblems") + public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) { + new com.github.yulichang.config.MappingConfig(); + } + } + + @Configuration + @ConditionalOnBean(SqlSessionFactory.class) + public static class SpringContext implements SpringContentUtils.SpringContext, ApplicationContextAware { + + private ApplicationContext applicationContext; + + @Override + public T getBean(Class clazz) { + return this.applicationContext.getBean(clazz); + } + + @Override + @SuppressWarnings("NullableProblems") + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + } + +} diff --git a/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinProperties.java b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinProperties.java new file mode 100644 index 0000000..91dfdf5 --- /dev/null +++ b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinProperties.java @@ -0,0 +1,27 @@ +package com.github.yulichang.autoconfigure; + +import lombok.Data; +import lombok.experimental.Accessors; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * 配置类 + * + * @author yulichang + * @since 1.3.7 + */ +@Data +@Accessors(chain = true) +@ConfigurationProperties(prefix = "mybatis-plus-join") +public class MybatisPlusJoinProperties { + + /** + * 打印banner + */ + private Boolean banner = true; + + /** + * 连表查询副表是否启用逻辑删除(前提是MP配置了逻辑删除) + */ + private Boolean subTableLogic = true; +} diff --git a/mybatis-plus-join-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/mybatis-plus-join-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json new file mode 100644 index 0000000..de6b922 --- /dev/null +++ b/mybatis-plus-join-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -0,0 +1,23 @@ +{ + "groups": [ + { + "sourceType": "com.github.yulichang.autoconfigure.MybatisPlusJoinProperties", + "name": "mybatis-plus-join", + "type": "com.github.yulichang.autoconfigure.MybatisPlusJoinProperties" + } + ], + "properties": [ + { + "name": "mybatis-plus-join.banner", + "defaultValue": true, + "type": "java.lang.Boolean", + "description": "打印 banner." + }, + { + "name": "mybatis-plus-join.sub-table-logic", + "defaultValue": true, + "type": "java.lang.Boolean", + "description": "连表查询副表是否启用逻辑删除(前提是MP配置了逻辑删除)." + } + ] +} \ No newline at end of file diff --git a/mybatis-plus-join-boot-starter/src/main/resources/META-INF/spring.factories b/mybatis-plus-join-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..f08fa4e --- /dev/null +++ b/mybatis-plus-join-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.github.yulichang.autoconfigure.MybatisPlusJoinAutoConfiguration diff --git a/mybatis-plus-join-core/pom.xml b/mybatis-plus-join-core/pom.xml new file mode 100644 index 0000000..ab0d912 --- /dev/null +++ b/mybatis-plus-join-core/pom.xml @@ -0,0 +1,77 @@ + + + + 4.0.0 + + com.github.yulichang + mybatis-plus-join-root + 1.3.8 + + 1.3.8 + mybatis-plus-join-core + mybatis-plus-join-core + + An enhanced toolkit of Mybatis-Plus to simplify development. + https://github.com/yulichang/mybatis-plus-join + + + The Apache License, Version 2.0 + https://www.apache.org/licenses/LICENSE-2.0.txt + + + + + mybatis-plus-join + yulichang + yu_lichang@qq.com + + + + scm:git:https://github.com/yulichang/mybatis-plus-join.git + scm:git:https://github.com/yulichang/mybatis-plus-join.git + https://github.com/yulichang/mybatis-plus-join + + + + 1.8 + 1.8 + 1.8 + 1.8 + github + UTF-8 + + + + + com.github.yulichang + mybatis-plus-join-annotation + 1.3.8 + + + com.baomidou + mybatis-plus-extension + 3.5.2 + provided + + + org.projectlombok + lombok + 1.18.24 + provided + + + org.slf4j + slf4j-api + provided + 2.0.4 + + + org.springframework + spring-aop + 5.3.23 + provided + + + diff --git a/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java b/mybatis-plus-join-core/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java similarity index 100% rename from src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java rename to mybatis-plus-join-core/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java diff --git a/src/main/java/com/github/yulichang/base/MPJBaseMapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/MPJBaseMapper.java similarity index 100% rename from src/main/java/com/github/yulichang/base/MPJBaseMapper.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/base/MPJBaseMapper.java diff --git a/src/main/java/com/github/yulichang/base/MPJBaseService.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/MPJBaseService.java similarity index 100% rename from src/main/java/com/github/yulichang/base/MPJBaseService.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/base/MPJBaseService.java diff --git a/src/main/java/com/github/yulichang/base/MPJBaseServiceImpl.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/MPJBaseServiceImpl.java similarity index 100% rename from src/main/java/com/github/yulichang/base/MPJBaseServiceImpl.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/base/MPJBaseServiceImpl.java diff --git a/src/main/java/com/github/yulichang/base/mapper/MPJDeepMapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/mapper/MPJDeepMapper.java similarity index 100% rename from src/main/java/com/github/yulichang/base/mapper/MPJDeepMapper.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/base/mapper/MPJDeepMapper.java diff --git a/src/main/java/com/github/yulichang/base/mapper/MPJJoinMapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/mapper/MPJJoinMapper.java similarity index 100% rename from src/main/java/com/github/yulichang/base/mapper/MPJJoinMapper.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/base/mapper/MPJJoinMapper.java diff --git a/src/main/java/com/github/yulichang/base/mapper/wrapper/MappingQuery.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/mapper/wrapper/MappingQuery.java similarity index 100% rename from src/main/java/com/github/yulichang/base/mapper/wrapper/MappingQuery.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/base/mapper/wrapper/MappingQuery.java diff --git a/src/main/java/com/github/yulichang/base/service/MPJDeepService.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/service/MPJDeepService.java similarity index 100% rename from src/main/java/com/github/yulichang/base/service/MPJDeepService.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/base/service/MPJDeepService.java diff --git a/src/main/java/com/github/yulichang/base/service/MPJJoinService.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/service/MPJJoinService.java similarity index 100% rename from src/main/java/com/github/yulichang/base/service/MPJJoinService.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/base/service/MPJJoinService.java diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/ConfigProperties.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/ConfigProperties.java new file mode 100644 index 0000000..e96c9aa --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/ConfigProperties.java @@ -0,0 +1,9 @@ +package com.github.yulichang.config; + +/** + * @author yulichang + * @since 1.3.7 + */ +public class ConfigProperties { + public static boolean subTableLogic = true; +} diff --git a/src/main/java/com/github/yulichang/config/InterceptorConfig.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/InterceptorConfig.java similarity index 86% rename from src/main/java/com/github/yulichang/config/InterceptorConfig.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/config/InterceptorConfig.java index 7d0292b..60f8f59 100644 --- a/src/main/java/com/github/yulichang/config/InterceptorConfig.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/InterceptorConfig.java @@ -8,8 +8,6 @@ import org.apache.ibatis.logging.LogFactory; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.InterceptorChain; import org.apache.ibatis.session.SqlSessionFactory; -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; -import org.springframework.context.annotation.Configuration; import java.lang.reflect.Field; import java.util.List; @@ -19,16 +17,19 @@ import java.util.List; * * @author yulichang */ -@Configuration -@ConditionalOnBean(SqlSessionFactory.class) -@SuppressWarnings("unused") public class InterceptorConfig { private static final Log logger = LogFactory.getLog(InterceptorConfig.class); - public InterceptorConfig(List sqlSessionFactoryList) { + public InterceptorConfig(List sqlSessionFactoryList, Boolean banner) { replaceInterceptorChain(sqlSessionFactoryList); + if (banner) { + //打印banner + System.out.println(" _ _ |_ _ _|_. ___ _ | _ . _ . _ \n" + + "| | |\\/|_)(_| | |_\\ |_)||_|_\\ | (_) | | | \n" + + " / | / 1.3.8"); + } } @SuppressWarnings("unchecked") diff --git a/src/main/java/com/github/yulichang/config/MappingConfig.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MappingConfig.java similarity index 50% rename from src/main/java/com/github/yulichang/config/MappingConfig.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MappingConfig.java index ee07913..3d8d63e 100644 --- a/src/main/java/com/github/yulichang/config/MappingConfig.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MappingConfig.java @@ -1,12 +1,8 @@ package com.github.yulichang.config; import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper; -import com.github.yulichang.mapper.MPJTableMapperHelper; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; -import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.context.ApplicationListener; -import org.springframework.core.Ordered; -import org.springframework.core.annotation.Order; +import com.github.yulichang.mapper.MPJTableMapperHelper; /** * 关系映射配置 @@ -14,13 +10,11 @@ import org.springframework.core.annotation.Order; * @author yulichang * @since 1.2.0 */ -@Order(Ordered.HIGHEST_PRECEDENCE) -public class MappingConfig implements ApplicationListener { +public class MappingConfig { - @Override - @SuppressWarnings("NullableProblems") - public void onApplicationEvent(ApplicationReadyEvent event) { + public MappingConfig() { TableInfoHelper.getTableInfos().forEach(i -> MPJTableInfoHelper.initTableInfo(i.getEntityType(), MPJTableMapperHelper.get(i.getEntityType()))); } + } diff --git a/src/main/java/com/github/yulichang/exception/MPJException.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/exception/MPJException.java similarity index 100% rename from src/main/java/com/github/yulichang/exception/MPJException.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/exception/MPJException.java diff --git a/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java similarity index 77% rename from src/main/java/com/github/yulichang/injector/MPJSqlInjector.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java index 7224d22..9056174 100644 --- a/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java @@ -2,27 +2,16 @@ package com.github.yulichang.injector; import com.baomidou.mybatisplus.core.MybatisPlusVersion; import com.baomidou.mybatisplus.core.injector.AbstractMethod; -import com.baomidou.mybatisplus.core.injector.AbstractSqlInjector; import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; -import com.baomidou.mybatisplus.core.injector.ISqlInjector; import com.baomidou.mybatisplus.core.injector.methods.*; import com.baomidou.mybatisplus.core.mapper.Mapper; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.toolkit.ClassUtils; import com.github.yulichang.mapper.MPJTableMapperHelper; import com.github.yulichang.method.*; -import com.github.yulichang.method.mp.SelectCount; -import com.github.yulichang.method.mp.SelectList; -import com.github.yulichang.method.mp.SelectMaps; -import com.github.yulichang.method.mp.SelectMapsPage; -import com.github.yulichang.method.mp.SelectObjs; import com.github.yulichang.method.mp.SelectOne; -import com.github.yulichang.method.mp.SelectPage; import org.apache.ibatis.builder.MapperBuilderAssistant; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.core.GenericTypeResolver; -import org.springframework.core.Ordered; -import org.springframework.core.annotation.Order; import java.util.ArrayList; import java.util.Arrays; @@ -37,13 +26,8 @@ import static java.util.stream.Collectors.toList; * @author yulichang * @see DefaultSqlInjector */ -@Order(Ordered.HIGHEST_PRECEDENCE) -@ConditionalOnMissingBean({DefaultSqlInjector.class, AbstractSqlInjector.class, ISqlInjector.class}) public class MPJSqlInjector extends DefaultSqlInjector { - private static final List METHOD_LIST = Arrays.asList("SelectOne", "SelectCount", - "SelectMaps", "SelectMapsPage", "SelectObjs", "SelectList", "SelectPage"); - /** * 升级到 mybatis plus 3.4.3.2 后对之前的版本兼容 @@ -72,8 +56,10 @@ public class MPJSqlInjector extends DefaultSqlInjector { */ @Override public List getMethodList(Class mapperClass, TableInfo tableInfo) { + List methodList = Arrays.asList("SelectOne", "SelectCount", + "SelectMaps", "SelectMapsPage", "SelectObjs", "SelectList", "SelectPage"); List list = super.getMethodList(mapperClass, tableInfo); - list.removeIf(i -> METHOD_LIST.contains(i.getClass().getSimpleName())); + list.removeIf(i -> methodList.contains(i.getClass().getSimpleName())); list.addAll(getSelectMethod()); list.addAll(getJoinMethod()); return list; @@ -115,12 +101,12 @@ public class MPJSqlInjector extends DefaultSqlInjector { private List getSelectMethod() { List list = new ArrayList<>(); list.add(new SelectOne()); - list.add(new SelectCount()); - list.add(new SelectMaps()); - list.add(new SelectMapsPage()); - list.add(new SelectObjs()); - list.add(new SelectList()); - list.add(new SelectPage()); + list.add(new com.github.yulichang.method.mp.SelectCount()); + list.add(new com.github.yulichang.method.mp.SelectMaps()); + list.add(new com.github.yulichang.method.mp.SelectMapsPage()); + list.add(new com.github.yulichang.method.mp.SelectObjs()); + list.add(new com.github.yulichang.method.mp.SelectList()); + list.add(new com.github.yulichang.method.mp.SelectPage()); return list; } diff --git a/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java similarity index 97% rename from src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java index fa2f7ab..63db8b4 100644 --- a/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java @@ -95,7 +95,7 @@ public class MPJInterceptor implements Interceptor { public MappedStatement getMappedStatement(MappedStatement ms, Class resultType, Object ew) { String id = ms.getId() + StringPool.UNDERSCORE + resultType.getName(); - if (ew instanceof MPJLambdaWrapper) { + if (ew instanceof MPJLambdaWrapper && ((MPJLambdaWrapper) ew).isResultMap()) { //不走缓存 return buildMappedStatement(ms, resultType, ew, id); } @@ -200,7 +200,8 @@ public class MPJInterceptor implements Interceptor { } else { // 主键列 resultMappings.add(new ResultMapping.Builder(ms.getConfiguration(), i.getTagProperty(), - StringUtils.getTargetColumn(i.getColumnName()), i.getKeyType()).build()); + StringUtils.getTargetColumn(i.getColumnName()), i.getKeyType()) + .flags(Collections.singletonList(ResultFlag.ID)).build()); } } Set columnSet = resultMappings.stream().map(ResultMapping::getColumn).collect(Collectors.toSet()); @@ -310,13 +311,12 @@ public class MPJInterceptor implements Interceptor { * @return 唯一列名 */ private String getColumn(Set pool, String columnName) { - String tagName = StringUtils.getTargetColumn(columnName); - if (!pool.contains(tagName)) { - pool.add(tagName); - return tagName; + if (!pool.contains(columnName)) { + pool.add(columnName); + return columnName; } - tagName = "mpj_" + tagName; - return getColumn(pool, tagName); + columnName = "join_" + StringUtils.getTargetColumn(columnName); + return getColumn(pool, columnName); } /** diff --git a/src/main/java/com/github/yulichang/interfaces/MPJBaseJoin.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/interfaces/MPJBaseJoin.java similarity index 100% rename from src/main/java/com/github/yulichang/interfaces/MPJBaseJoin.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/interfaces/MPJBaseJoin.java diff --git a/src/main/java/com/github/yulichang/mapper/MPJMappingWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJMappingWrapper.java similarity index 100% rename from src/main/java/com/github/yulichang/mapper/MPJMappingWrapper.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJMappingWrapper.java diff --git a/src/main/java/com/github/yulichang/mapper/MPJTableFieldInfo.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJTableFieldInfo.java similarity index 99% rename from src/main/java/com/github/yulichang/mapper/MPJTableFieldInfo.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJTableFieldInfo.java index dfef2dd..5d0b036 100644 --- a/src/main/java/com/github/yulichang/mapper/MPJTableFieldInfo.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJTableFieldInfo.java @@ -279,7 +279,7 @@ public class MPJTableFieldInfo { if (joinTableInfo == null) { throw new MPJException("未注册 mapper " + this.joinClass.getName()); } - this.joinMapper = (BaseMapper) SpringContentUtils.getApplicationContext().getBean(joinTableInfo.getMapperClass()); + this.joinMapper = (BaseMapper) SpringContentUtils.getBean(joinTableInfo.getMapperClass()); } return this.joinMapper; } diff --git a/src/main/java/com/github/yulichang/mapper/MPJTableInfo.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJTableInfo.java similarity index 100% rename from src/main/java/com/github/yulichang/mapper/MPJTableInfo.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJTableInfo.java diff --git a/src/main/java/com/github/yulichang/mapper/MPJTableMapperHelper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJTableMapperHelper.java similarity index 100% rename from src/main/java/com/github/yulichang/mapper/MPJTableMapperHelper.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJTableMapperHelper.java diff --git a/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java similarity index 100% rename from src/main/java/com/github/yulichang/method/MPJAbstractMethod.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java diff --git a/src/main/java/com/github/yulichang/method/MPJBaseMethod.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJBaseMethod.java similarity index 93% rename from src/main/java/com/github/yulichang/method/MPJBaseMethod.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJBaseMethod.java index dae42c6..60bcad0 100644 --- a/src/main/java/com/github/yulichang/method/MPJBaseMethod.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJBaseMethod.java @@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; +import com.github.yulichang.config.ConfigProperties; import com.github.yulichang.toolkit.Constant; import java.util.Objects; @@ -26,6 +27,9 @@ public interface MPJBaseMethod extends Constants { sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", WRAPPER_ENTITY), true); sqlScript += NEWLINE; sqlScript += (NEWLINE + getLogicDeleteSql(table, true, true) + NEWLINE); + if (ConfigProperties.subTableLogic) { + sqlScript += (String.format("${%s.logicSql}", WRAPPER)); + } String normalSqlScript = SqlScriptUtils.convertIf(String.format("AND ${%s}", WRAPPER_SQLSEGMENT), String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, WRAPPER_NONEMPTYOFNORMAL), true); normalSqlScript += NEWLINE; normalSqlScript += SqlScriptUtils.convertIf(String.format(" ${%s}", WRAPPER_SQLSEGMENT), String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, WRAPPER_EMPTYOFNORMAL), true); @@ -37,7 +41,10 @@ public interface MPJBaseMethod extends Constants { String sqlScript = getAllSqlWhere(table, false, true, WRAPPER_ENTITY_DOT); sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", WRAPPER_ENTITY), true); sqlScript += NEWLINE; - sqlScript += SqlScriptUtils.convertIf(String.format("${%s}", WRAPPER_SQLSEGMENT), String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, WRAPPER_NONEMPTYOFWHERE), true); + if (ConfigProperties.subTableLogic) { + sqlScript += (String.format("${%s.logicSql}", WRAPPER) + NEWLINE); + } + sqlScript += SqlScriptUtils.convertIf(String.format("AND ${%s}", WRAPPER_SQLSEGMENT), String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, WRAPPER_NONEMPTYOFWHERE), true); sqlScript = SqlScriptUtils.convertWhere(sqlScript) + NEWLINE; sqlScript += SqlScriptUtils.convertIf(String.format(" ${%s}", WRAPPER_SQLSEGMENT), String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, WRAPPER_EMPTYOFWHERE), true); sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", WRAPPER), true); diff --git a/src/main/java/com/github/yulichang/method/MPJResultType.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJResultType.java similarity index 100% rename from src/main/java/com/github/yulichang/method/MPJResultType.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJResultType.java diff --git a/src/main/java/com/github/yulichang/method/SelectJoinCount.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinCount.java similarity index 100% rename from src/main/java/com/github/yulichang/method/SelectJoinCount.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinCount.java diff --git a/src/main/java/com/github/yulichang/method/SelectJoinList.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinList.java similarity index 100% rename from src/main/java/com/github/yulichang/method/SelectJoinList.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinList.java diff --git a/src/main/java/com/github/yulichang/method/SelectJoinMap.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMap.java similarity index 100% rename from src/main/java/com/github/yulichang/method/SelectJoinMap.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMap.java diff --git a/src/main/java/com/github/yulichang/method/SelectJoinMaps.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMaps.java similarity index 100% rename from src/main/java/com/github/yulichang/method/SelectJoinMaps.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMaps.java diff --git a/src/main/java/com/github/yulichang/method/SelectJoinMapsPage.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMapsPage.java similarity index 100% rename from src/main/java/com/github/yulichang/method/SelectJoinMapsPage.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMapsPage.java diff --git a/src/main/java/com/github/yulichang/method/SelectJoinOne.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinOne.java similarity index 100% rename from src/main/java/com/github/yulichang/method/SelectJoinOne.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinOne.java diff --git a/src/main/java/com/github/yulichang/method/SelectJoinPage.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinPage.java similarity index 100% rename from src/main/java/com/github/yulichang/method/SelectJoinPage.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinPage.java diff --git a/src/main/java/com/github/yulichang/method/SqlMethod.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SqlMethod.java similarity index 100% rename from src/main/java/com/github/yulichang/method/SqlMethod.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SqlMethod.java diff --git a/src/main/java/com/github/yulichang/method/mp/SelectCount.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectCount.java similarity index 100% rename from src/main/java/com/github/yulichang/method/mp/SelectCount.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectCount.java diff --git a/src/main/java/com/github/yulichang/method/mp/SelectList.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectList.java similarity index 100% rename from src/main/java/com/github/yulichang/method/mp/SelectList.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectList.java diff --git a/src/main/java/com/github/yulichang/method/mp/SelectMaps.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectMaps.java similarity index 100% rename from src/main/java/com/github/yulichang/method/mp/SelectMaps.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectMaps.java diff --git a/src/main/java/com/github/yulichang/method/mp/SelectMapsPage.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectMapsPage.java similarity index 100% rename from src/main/java/com/github/yulichang/method/mp/SelectMapsPage.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectMapsPage.java diff --git a/src/main/java/com/github/yulichang/method/mp/SelectObjs.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectObjs.java similarity index 100% rename from src/main/java/com/github/yulichang/method/mp/SelectObjs.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectObjs.java diff --git a/src/main/java/com/github/yulichang/method/mp/SelectOne.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectOne.java similarity index 100% rename from src/main/java/com/github/yulichang/method/mp/SelectOne.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectOne.java diff --git a/src/main/java/com/github/yulichang/method/mp/SelectPage.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectPage.java similarity index 100% rename from src/main/java/com/github/yulichang/method/mp/SelectPage.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectPage.java diff --git a/src/main/java/com/github/yulichang/method/mp/TableAlias.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/TableAlias.java similarity index 100% rename from src/main/java/com/github/yulichang/method/mp/TableAlias.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/TableAlias.java diff --git a/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java similarity index 96% rename from src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java index 6915ab1..f1d2ad7 100644 --- a/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java @@ -7,14 +7,10 @@ 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.CollectionUtils; -import com.baomidou.mybatisplus.core.toolkit.StringPool; -import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import com.baomidou.mybatisplus.core.toolkit.*; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.query.interfaces.MPJJoin; import com.github.yulichang.toolkit.Constant; -import org.springframework.util.Assert; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; diff --git a/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java similarity index 98% rename from src/main/java/com/github/yulichang/query/MPJQueryWrapper.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java index e2aa3d2..48df15a 100644 --- a/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java @@ -204,6 +204,13 @@ public class MPJQueryWrapper extends AbstractWrapper extends AbstractWrapper + * ClassUtils + *

+ * + * @author Caratacus + * @author HCL + * @since 2017/07/08 + */ +@SuppressWarnings("All") +public final class ClassUtils { + + private static ClassLoader systemClassLoader; + + static { + try { + systemClassLoader = ClassLoader.getSystemClassLoader(); + } catch (SecurityException ignored) { + // AccessControlException on Google App Engine + } + } + + /** + * 代理 class 的名称 + */ + private static final List PROXY_CLASS_NAMES = Arrays.asList("net.sf.cglib.proxy.Factory" + // cglib + , "org.springframework.cglib.proxy.Factory" + , "javassist.util.proxy.ProxyObject" + // javassist + , "org.apache.ibatis.javassist.util.proxy.ProxyObject"); + + private ClassUtils() { + } + + /** + * 判断传入的类型是否是布尔类型 + * + * @param type 类型 + * @return 如果是原生布尔或者包装类型布尔,均返回 true + */ + public static boolean isBoolean(Class type) { + return type == boolean.class || Boolean.class == type; + } + + /** + * 判断是否为代理对象 + * + * @param clazz 传入 class 对象 + * @return 如果对象class是代理 class,返回 true + */ + public static boolean isProxy(Class clazz) { + if (clazz != null) { + for (Class cls : clazz.getInterfaces()) { + if (PROXY_CLASS_NAMES.contains(cls.getName())) { + return true; + } + } + } + return false; + } + + /** + *

+ * 获取当前对象的 class + *

+ * + * @param clazz 传入 + * @return 如果是代理的class,返回父 class,否则返回自身 + */ + public static Class getUserClass(Class clazz) { + Assert.notNull(clazz, "Class must not be null"); + return isProxy(clazz) ? clazz.getSuperclass() : clazz; + } + + /** + *

+ * 获取当前对象的class + *

+ * + * @param object 对象 + * @return 返回对象的 user class + */ + public static Class getUserClass(Object object) { + Assert.notNull(object, "Instance must not be null"); + return getUserClass(object.getClass()); + } + + /** + *

+ * 根据指定的 class , 实例化一个对象,根据构造参数来实例化 + *

+ *

+ * 在 java9 及其之后的版本 Class.newInstance() 方法已被废弃 + *

+ * + * @param clazz 需要实例化的对象 + * @param 类型,由输入类型决定 + * @return 返回新的实例 + */ + public static T newInstance(Class clazz) { + try { + Constructor constructor = clazz.getDeclaredConstructor(); + constructor.setAccessible(true); + return constructor.newInstance(); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | + NoSuchMethodException e) { + throw ExceptionUtils.mpe("实例化对象时出现错误,请尝试给 %s 添加无参的构造方法", e, clazz.getName()); + } + } + + /** + * 实例化对象. + * + * @param clazzName 类名 + * @param 类型 + * @return 实例 + * @since 3.3.2 + */ + @SuppressWarnings("unchecked") + public static T newInstance(String clazzName) { + return (T) newInstance(toClassConfident(clazzName)); + } + + + /** + *

+ * 请仅在确定类存在的情况下调用该方法 + *

+ * + * @param name 类名称 + * @return 返回转换后的 Class + */ + public static Class toClassConfident(String name) { + return toClassConfident(name, null); + } + + /** + * @param name + * @param classLoader + * @return + * @since 3.4.3 + */ + public static Class toClassConfident(String name, ClassLoader classLoader) { + try { + return loadClass(name, getClassLoaders(classLoader)); + } catch (ClassNotFoundException e) { + throw ExceptionUtils.mpe("找不到指定的class!请仅在明确确定会有 class 的时候,调用该方法", e); + } + } + + private static Class loadClass(String className, ClassLoader[] classLoaders) throws ClassNotFoundException { + for (ClassLoader classLoader : classLoaders) { + if (classLoader != null) { + try { + return Class.forName(className, true, classLoader); + } catch (ClassNotFoundException e) { + // ignore + } + } + } + throw new ClassNotFoundException("Cannot find class: " + className); + } + + + /** + * Determine the name of the package of the given class, + * e.g. "java.lang" for the {@code java.lang.String} class. + * + * @param clazz the class + * @return the package name, or the empty String if the class + * is defined in the default package + */ + public static String getPackageName(Class clazz) { + Assert.notNull(clazz, "Class must not be null"); + return getPackageName(clazz.getName()); + } + + /** + * Determine the name of the package of the given fully-qualified class name, + * e.g. "java.lang" for the {@code java.lang.String} class name. + * + * @param fqClassName the fully-qualified class name + * @return the package name, or the empty String if the class + * is defined in the default package + */ + public static String getPackageName(String fqClassName) { + Assert.notNull(fqClassName, "Class name must not be null"); + int lastDotIndex = fqClassName.lastIndexOf(StringPool.DOT); + return (lastDotIndex != -1 ? fqClassName.substring(0, lastDotIndex) : StringPool.EMPTY); + } + + /** + * Return the default ClassLoader to use: typically the thread context + * ClassLoader, if available; the ClassLoader that loaded the ClassUtils + * class will be used as fallback. + *

Call this method if you intend to use the thread context ClassLoader + * in a scenario where you clearly prefer a non-null ClassLoader reference: + * for example, for class path resource loading (but not necessarily for + * {@code Class.forName}, which accepts a {@code null} ClassLoader + * reference as well). + * + * @return the default ClassLoader (only {@code null} if even the system + * ClassLoader isn't accessible) + * @see Thread#getContextClassLoader() + * @see ClassLoader#getSystemClassLoader() + * @since 3.3.2 + */ + @Deprecated + public static ClassLoader getDefaultClassLoader() { + ClassLoader cl = null; + try { + cl = Thread.currentThread().getContextClassLoader(); + } catch (Throwable ex) { + // Cannot access thread context ClassLoader - falling back... + } + if (cl == null) { + // No thread context class loader -> use class loader of this class. + cl = ClassUtils.class.getClassLoader(); + if (cl == null) { + // getClassLoader() returning null indicates the bootstrap ClassLoader + try { + cl = ClassLoader.getSystemClassLoader(); + } catch (Throwable ex) { + // Cannot access system ClassLoader - oh well, maybe the caller can live with null... + } + } + } + return cl; + } + + private static ClassLoader[] getClassLoaders(ClassLoader classLoader) { + return new ClassLoader[]{ + classLoader, + Resources.getDefaultClassLoader(), + Thread.currentThread().getContextClassLoader(), + ClassUtils.class.getClassLoader(), + systemClassLoader}; + } +} diff --git a/src/main/java/com/github/yulichang/toolkit/Constant.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/Constant.java similarity index 100% rename from src/main/java/com/github/yulichang/toolkit/Constant.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/Constant.java diff --git a/src/main/java/com/github/yulichang/toolkit/InterceptorList.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/InterceptorList.java similarity index 98% rename from src/main/java/com/github/yulichang/toolkit/InterceptorList.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/InterceptorList.java index e8620cc..c113632 100644 --- a/src/main/java/com/github/yulichang/toolkit/InterceptorList.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/InterceptorList.java @@ -13,7 +13,7 @@ import java.util.function.Predicate; * 保证 MPJInterceptor 再最后一个(第一个执行) * * @author yulichang - * @since 1.2.5 + * @since 1.3.0 */ public class InterceptorList extends ArrayList { diff --git a/src/main/java/com/github/yulichang/toolkit/LambdaUtils.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LambdaUtils.java similarity index 63% rename from src/main/java/com/github/yulichang/toolkit/LambdaUtils.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LambdaUtils.java index e782640..9e782cd 100644 --- a/src/main/java/com/github/yulichang/toolkit/LambdaUtils.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LambdaUtils.java @@ -5,13 +5,13 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; -import com.github.yulichang.toolkit.support.ColumnCache; -import com.github.yulichang.toolkit.support.SerializedLambda; +import com.github.yulichang.toolkit.support.*; import org.apache.ibatis.reflection.property.PropertyNamer; -import java.lang.ref.WeakReference; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; import java.util.Map; -import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import static java.util.Locale.ENGLISH; @@ -19,50 +19,54 @@ import static java.util.Locale.ENGLISH; /** * copy {@link com.baomidou.mybatisplus.core.toolkit.LambdaUtils} */ +@SuppressWarnings("unused") public final class LambdaUtils { /* ******* 自定义方法 *********** */ public static String getName(SFunction fn) { - return PropertyNamer.methodToProperty(resolve(fn).getImplMethodName()); + LambdaMeta extract = extract(fn); + String name = PropertyNamer.methodToProperty(extract.getImplMethodName()); + if (Character.isUpperCase(name.charAt(0))) { + Map map = MPJReflectionKit.getFieldMap(extract.getInstantiatedClass()); + if (map.containsKey(name)) { + return name; + } else { + return map.keySet().stream().filter(i -> i.equalsIgnoreCase(name)).findFirst().orElse(null); + } + } + return name; } @SuppressWarnings("unchecked") public static Class getEntityClass(SFunction fn) { - return (Class) resolve(fn).getInstantiatedType(); + return (Class) extract(fn).getInstantiatedClass(); } /* ******* 自定义方法 结束 以下代码均为拷贝 *********** */ - /** - * 字段映射 - */ private static final Map> COLUMN_CACHE_MAP = new ConcurrentHashMap<>(); /** - * SerializedLambda 反序列化缓存 - */ - private static final Map> FUNC_CACHE = new ConcurrentHashMap<>(); - - /** - * 解析 lambda 表达式, 该方法只是调用了 {@link SerializedLambda#resolve(SFunction)} 中的方法,在此基础上加了缓存。 * 该缓存可能会在任意不定的时间被清除 * * @param func 需要解析的 lambda 对象 * @param 类型,被调用的 Function 对象的目标类型 * @return 返回解析后的结果 - * @see SerializedLambda#resolve(SFunction) */ - public static SerializedLambda resolve(SFunction func) { - Class clazz = func.getClass(); - String name = clazz.getName(); - return Optional.ofNullable(FUNC_CACHE.get(name)) - .map(WeakReference::get) - .orElseGet(() -> { - SerializedLambda lambda = SerializedLambda.resolve(func); - FUNC_CACHE.put(name, new WeakReference<>(lambda)); - return lambda; - }); + public static LambdaMeta extract(SFunction func) { + // 1. IDEA 调试模式下 lambda 表达式是一个代理 + if (func instanceof Proxy) { + return new IdeaProxyLambdaMeta((Proxy) func); + } + // 2. 反射读取 + try { + Method method = func.getClass().getDeclaredMethod("writeReplace"); + return new ReflectLambdaMeta((java.lang.invoke.SerializedLambda) ReflectionKit.setAccessible(method).invoke(func)); + } catch (Throwable e) { + // 3. 反射失败使用序列化的方式读取 + return new ShadowLambdaMeta(SerializedLambda.extract(func)); + } } /** @@ -79,6 +83,14 @@ public final class LambdaUtils { return key.toUpperCase(ENGLISH); } + /** + * 将传入的表信息加入缓存 + * + * @param tableInfo 表信息 + */ + public static void installCache(TableInfo tableInfo) { + COLUMN_CACHE_MAP.put(tableInfo.getEntityType().getName(), createColumnCacheMap(tableInfo)); + } /** * 缓存实体字段 MAP 信息 @@ -115,5 +127,4 @@ public final class LambdaUtils { return info == null ? null : createColumnCacheMap(info); }); } - } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java new file mode 100644 index 0000000..bfd1dce --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java @@ -0,0 +1,45 @@ +package com.github.yulichang.toolkit; + +import com.baomidou.mybatisplus.core.metadata.TableInfo; +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; +import com.baomidou.mybatisplus.core.toolkit.Assert; +import com.baomidou.mybatisplus.core.toolkit.Constants; +import com.baomidou.mybatisplus.core.toolkit.StringPool; + +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; + +/** + * 逻辑删除信息类 + * + * @author yulichang + * @since 1.3.7 + */ +public class LogicInfoUtils implements Constants { + + private static final Map, String> LOGIC_CACHE = new ConcurrentHashMap<>(); + + + @SuppressWarnings("ConstantConditions") + public static String getLogicInfo(int tableIndex, Class clazz) { + String logicStr = LOGIC_CACHE.get(clazz); + if (Objects.nonNull(logicStr)) { + return logicStr; + } + TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz); + Assert.notNull(tableInfo, "%s 不是数据库实体或没有注册到mybatis plus中", clazz.getName()); + if (tableInfo.isWithLogicDelete() && Objects.nonNull(tableInfo.getLogicDeleteFieldInfo())) { + final String value = tableInfo.getLogicDeleteFieldInfo().getLogicNotDeleteValue(); + if (NULL.equalsIgnoreCase(value)) { + logicStr = " AND " + Constant.TABLE_ALIAS + tableIndex + DOT + tableInfo.getLogicDeleteFieldInfo().getColumn() + " IS NULL"; + } else { + logicStr = " AND " + Constant.TABLE_ALIAS + tableIndex + DOT + tableInfo.getLogicDeleteFieldInfo().getColumn() + EQUALS + String.format(tableInfo.getLogicDeleteFieldInfo().isCharSequence() ? "'%s'" : "%s", value); + } + } else { + logicStr = StringPool.EMPTY; + } + LOGIC_CACHE.put(clazz, logicStr); + return logicStr; + } +} diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/MPJReflectionKit.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/MPJReflectionKit.java new file mode 100644 index 0000000..1f923e4 --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/MPJReflectionKit.java @@ -0,0 +1,62 @@ +package com.github.yulichang.toolkit; + +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.lang.reflect.WildcardType; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * 反射工具类 + * + * @author yulichang + * @since 1.3.7 + */ +@SuppressWarnings("unused") +public final class MPJReflectionKit { + + private static final Map, Map> CLASS_FIELD_CACHE = new ConcurrentHashMap<>(); + + private static final Map EMPTY_MAP = new HashMap<>(); + + /** + * Collection字段的泛型 + */ + public static Class getGenericType(Field field) { + Type type = field.getGenericType(); + //没有写泛型 + if (!(type instanceof ParameterizedType)) { + return Object.class; + } + ParameterizedType pt = (ParameterizedType) type; + Type[] actualTypeArguments = pt.getActualTypeArguments(); + Type argument = actualTypeArguments[0]; + //通配符泛型 ? , ? extends XXX , ? super XXX + if (argument instanceof WildcardType) { + //获取上界 + Type[] types = ((WildcardType) argument).getUpperBounds(); + return (Class) types[0]; + } + return (Class) argument; + } + + /** + * 获取该类的所有属性列表 + * + * @param clazz 反射类 + */ + public static Map getFieldMap(Class clazz) { + if (clazz == null) { + return EMPTY_MAP; + } + Map fieldMap = CLASS_FIELD_CACHE.get(clazz); + if (fieldMap != null) { + return fieldMap; + } + Map map = ReflectionKit.getFieldMap(clazz); + CLASS_FIELD_CACHE.put(clazz, map); + return map; + } +} diff --git a/src/main/java/com/github/yulichang/toolkit/MPJWrappers.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/MPJWrappers.java similarity index 100% rename from src/main/java/com/github/yulichang/toolkit/MPJWrappers.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/MPJWrappers.java diff --git a/src/main/java/com/github/yulichang/toolkit/ReflectionKit.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/ReflectionKit.java similarity index 74% rename from src/main/java/com/github/yulichang/toolkit/ReflectionKit.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/ReflectionKit.java index e7b663d..d9d92a8 100644 --- a/src/main/java/com/github/yulichang/toolkit/ReflectionKit.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/ReflectionKit.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2021, baomidou (jobob@qq.com). + * Copyright (c) 2011-2022, baomidou (jobob@qq.com). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,17 +15,16 @@ */ package com.github.yulichang.toolkit; -import com.baomidou.mybatisplus.core.toolkit.Assert; -import com.baomidou.mybatisplus.core.toolkit.ClassUtils; -import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; -import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils; -import org.apache.ibatis.logging.Log; -import org.apache.ibatis.logging.LogFactory; -import org.springframework.core.GenericTypeResolver; +import com.baomidou.mybatisplus.core.toolkit.*; +import com.baomidou.mybatisplus.core.toolkit.reflect.GenericTypeUtils; -import java.lang.reflect.*; +import java.lang.reflect.AccessibleObject; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.security.AccessController; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -39,14 +38,14 @@ import static java.util.stream.Collectors.toMap; * @author hcl * @since 2016-09-22 */ -@SuppressWarnings("unused") +@SuppressWarnings("All") public final class ReflectionKit { - private static final Log logger = LogFactory.getLog(ReflectionKit.class); /** * class field cache */ private static final Map, List> CLASS_FIELD_CACHE = new ConcurrentHashMap<>(); + @Deprecated private static final Map, Class> PRIMITIVE_WRAPPER_TYPE_MAP = new IdentityHashMap<>(8); private static final Map, Class> PRIMITIVE_TYPE_TO_WRAPPER_MAP = new IdentityHashMap<>(8); @@ -96,32 +95,11 @@ public final class ReflectionKit { * @return Class */ public static Class getSuperClassGenericType(final Class clazz, final Class genericIfc, final int index) { - Class[] typeArguments = GenericTypeResolver.resolveTypeArguments(ClassUtils.getUserClass(clazz), genericIfc); + //update by noear @2021-09-03 + Class[] typeArguments = GenericTypeUtils.resolveTypeArguments(ClassUtils.getUserClass(clazz), genericIfc); return null == typeArguments ? null : typeArguments[index]; } - - /** - * Collection字段的泛型 - */ - public static Class getGenericType(Field field) { - Type type = field.getGenericType(); - //没有写泛型 - if (!(type instanceof ParameterizedType)) { - return Object.class; - } - ParameterizedType pt = (ParameterizedType) type; - Type[] actualTypeArguments = pt.getActualTypeArguments(); - Type argument = actualTypeArguments[0]; - //通配符泛型 ? , ? extends XXX , ? super XXX - if (argument instanceof WildcardType) { - //获取上界 - Type[] types = ((WildcardType) argument).getUpperBounds(); - return (Class) types[0]; - } - return (Class) argument; - } - /** *

* 获取该类的所有属性列表 @@ -131,7 +109,7 @@ public final class ReflectionKit { */ public static Map getFieldMap(Class clazz) { List fieldList = getFieldList(clazz); - return CollectionUtils.isNotEmpty(fieldList) ? fieldList.stream().collect(Collectors.toMap(Field::getName, field -> field)) : Collections.emptyMap(); + return CollectionUtils.isNotEmpty(fieldList) ? fieldList.stream().collect(Collectors.toMap(Field::getName, Function.identity())) : Collections.emptyMap(); } /** @@ -162,11 +140,11 @@ public final class ReflectionKit { * 中间表实体重写父类属性 ` private transient Date createTime; ` */ return fieldMap.values().stream() - /* 过滤静态属性 */ - .filter(f -> !Modifier.isStatic(f.getModifiers())) - /* 过滤 transient关键字修饰的属性 */ - .filter(f -> !Modifier.isTransient(f.getModifiers())) - .collect(Collectors.toList()); + /* 过滤静态属性 */ + .filter(f -> !Modifier.isStatic(f.getModifiers())) + /* 过滤 transient关键字修饰的属性 */ + .filter(f -> !Modifier.isTransient(f.getModifiers())) + .collect(Collectors.toList()); }); } @@ -181,12 +159,12 @@ public final class ReflectionKit { public static Map excludeOverrideSuperField(Field[] fields, List superFieldList) { // 子类属性 Map fieldMap = Stream.of(fields).collect(toMap(Field::getName, identity(), - (u, v) -> { - throw new IllegalStateException(String.format("Duplicate key %s", u)); - }, - LinkedHashMap::new)); + (u, v) -> { + throw new IllegalStateException(String.format("Duplicate key %s", u)); + }, + LinkedHashMap::new)); superFieldList.stream().filter(field -> !fieldMap.containsKey(field.getName())) - .forEach(f -> fieldMap.put(f.getName(), f)); + .forEach(f -> fieldMap.put(f.getName(), f)); return fieldMap; } @@ -196,6 +174,7 @@ public final class ReflectionKit { * @param clazz class * @return 是否基本类型或基本包装类型 */ + @Deprecated public static boolean isPrimitiveOrWrapper(Class clazz) { Assert.notNull(clazz, "Class must not be null"); return (clazz.isPrimitive() || PRIMITIVE_WRAPPER_TYPE_MAP.containsKey(clazz)); @@ -204,4 +183,16 @@ public final class ReflectionKit { public static Class resolvePrimitiveIfNecessary(Class clazz) { return (clazz.isPrimitive() && clazz != void.class ? PRIMITIVE_TYPE_TO_WRAPPER_MAP.get(clazz) : clazz); } + + /** + * 设置可访问对象的可访问权限为 true + * + * @param object 可访问的对象 + * @param 类型 + * @return 返回设置后的对象 + */ + public static T setAccessible(T object) { + return AccessController.doPrivileged(new SetAccessibleAction<>(object)); + } + } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/SpringContentUtils.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/SpringContentUtils.java new file mode 100644 index 0000000..7067b9b --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/SpringContentUtils.java @@ -0,0 +1,26 @@ +package com.github.yulichang.toolkit; + + +/** + * spring容器工具类 + * + * @author yulichang + * @since 1.2.0 + */ +public class SpringContentUtils { + + private static SpringContext springContext; + + public SpringContentUtils(SpringContext springContext) { + SpringContentUtils.springContext = springContext; + } + + public static T getBean(Class clazz) { + return SpringContentUtils.springContext.getBean(clazz); + } + + public interface SpringContext { + + T getBean(Class clazz); + } +} diff --git a/src/main/java/com/github/yulichang/toolkit/sql/SqlScriptUtils.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/sql/SqlScriptUtils.java similarity index 100% rename from src/main/java/com/github/yulichang/toolkit/sql/SqlScriptUtils.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/sql/SqlScriptUtils.java diff --git a/src/main/java/com/github/yulichang/toolkit/support/ColumnCache.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/ColumnCache.java similarity index 98% rename from src/main/java/com/github/yulichang/toolkit/support/ColumnCache.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/ColumnCache.java index 2d4df23..9a4c95c 100644 --- a/src/main/java/com/github/yulichang/toolkit/support/ColumnCache.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/ColumnCache.java @@ -8,7 +8,7 @@ import lombok.Getter; * * @author yulichang * @see com.baomidou.mybatisplus.core.toolkit.support.ColumnCache - * @since 1.2.5 + * @since 1.3.0 */ public class ColumnCache extends com.baomidou.mybatisplus.core.toolkit.support.ColumnCache { diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/IdeaProxyLambdaMeta.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/IdeaProxyLambdaMeta.java new file mode 100644 index 0000000..a4c3482 --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/IdeaProxyLambdaMeta.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2011-2022, baomidou (jobob@qq.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.yulichang.toolkit.support; + +import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; +import com.baomidou.mybatisplus.core.toolkit.ReflectionKit; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Proxy; + +/** + * 在 IDEA 的 Evaluate 中执行的 Lambda 表达式元数据需要使用该类处理元数据 + *

+ * Create by hcl at 2021/5/17 + */ +public class IdeaProxyLambdaMeta implements LambdaMeta { + private static final Field FIELD_MEMBER_NAME; + private static final Field FIELD_MEMBER_NAME_CLAZZ; + private static final Field FIELD_MEMBER_NAME_NAME; + + static { + try { + Class classDirectMethodHandle = Class.forName("java.lang.invoke.DirectMethodHandle"); + FIELD_MEMBER_NAME = ReflectionKit.setAccessible(classDirectMethodHandle.getDeclaredField("member")); + Class classMemberName = Class.forName("java.lang.invoke.MemberName"); + FIELD_MEMBER_NAME_CLAZZ = ReflectionKit.setAccessible(classMemberName.getDeclaredField("clazz")); + FIELD_MEMBER_NAME_NAME = ReflectionKit.setAccessible(classMemberName.getDeclaredField("name")); + } catch (ClassNotFoundException | NoSuchFieldException e) { + throw new MybatisPlusException(e); + } + } + + private final Class clazz; + private final String name; + + public IdeaProxyLambdaMeta(Proxy func) { + InvocationHandler handler = Proxy.getInvocationHandler(func); + try { + Object dmh = ReflectionKit.setAccessible(handler.getClass().getDeclaredField("val$target")).get(handler); + Object member = FIELD_MEMBER_NAME.get(dmh); + clazz = (Class) FIELD_MEMBER_NAME_CLAZZ.get(member); + name = (String) FIELD_MEMBER_NAME_NAME.get(member); + } catch (IllegalAccessException | NoSuchFieldException e) { + throw new MybatisPlusException(e); + } + } + + @Override + public String getImplMethodName() { + return name; + } + + @Override + public Class getInstantiatedClass() { + return clazz; + } + + @Override + public String toString() { + return clazz.getSimpleName() + "::" + name; + } + +} diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/LambdaMeta.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/LambdaMeta.java new file mode 100644 index 0000000..80295f7 --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/LambdaMeta.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2011-2022, baomidou (jobob@qq.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.yulichang.toolkit.support; + +/** + * Lambda 信息 + *

+ * Created by hcl at 2021/5/14 + */ +public interface LambdaMeta { + + /** + * 获取 lambda 表达式实现方法的名称 + * + * @return lambda 表达式对应的实现方法名称 + */ + String getImplMethodName(); + + /** + * 实例化该方法的类 + * + * @return 返回对应的类名称 + */ + Class getInstantiatedClass(); + +} diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/ReflectLambdaMeta.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/ReflectLambdaMeta.java new file mode 100644 index 0000000..11796f7 --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/ReflectLambdaMeta.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2011-2022, baomidou (jobob@qq.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.yulichang.toolkit.support; + + +import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; +import com.baomidou.mybatisplus.core.toolkit.StringPool; +import com.github.yulichang.toolkit.ClassUtils; +import com.github.yulichang.toolkit.ReflectionKit; +import lombok.extern.slf4j.Slf4j; + +import java.lang.invoke.SerializedLambda; +import java.lang.reflect.Field; + +/** + * Created by hcl at 2021/5/14 + */ +@Slf4j +public class ReflectLambdaMeta implements LambdaMeta { + private static final Field FIELD_CAPTURING_CLASS; + + static { + Field fieldCapturingClass; + try { + Class aClass = SerializedLambda.class; + fieldCapturingClass = ReflectionKit.setAccessible(aClass.getDeclaredField("capturingClass")); + } catch (Throwable e) { + // 解决高版本 jdk 的问题 gitee: https://gitee.com/baomidou/mybatis-plus/issues/I4A7I5 + log.warn(e.getMessage()); + fieldCapturingClass = null; + } + FIELD_CAPTURING_CLASS = fieldCapturingClass; + } + + private final SerializedLambda lambda; + + public ReflectLambdaMeta(SerializedLambda lambda) { + this.lambda = lambda; + } + + @Override + public String getImplMethodName() { + return lambda.getImplMethodName(); + } + + @Override + public Class getInstantiatedClass() { + String instantiatedMethodType = lambda.getInstantiatedMethodType(); + String instantiatedType = instantiatedMethodType.substring(2, instantiatedMethodType.indexOf(StringPool.SEMICOLON)).replace(StringPool.SLASH, StringPool.DOT); + return ClassUtils.toClassConfident(instantiatedType, getCapturingClassClassLoader()); + } + + private ClassLoader getCapturingClassClassLoader() { + // 如果反射失败,使用默认的 classloader + if (FIELD_CAPTURING_CLASS == null) { + return null; + } + try { + return ((Class) FIELD_CAPTURING_CLASS.get(lambda)).getClassLoader(); + } catch (IllegalAccessException e) { + throw new MybatisPlusException(e); + } + } + +} diff --git a/src/main/java/com/github/yulichang/toolkit/support/SelectColumn.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/SelectColumn.java similarity index 99% rename from src/main/java/com/github/yulichang/toolkit/support/SelectColumn.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/SelectColumn.java index 1d4d96c..964cfc1 100644 --- a/src/main/java/com/github/yulichang/toolkit/support/SelectColumn.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/SelectColumn.java @@ -9,7 +9,7 @@ import lombok.Getter; * MPJLambdaWrapper 查询字段 * * @author yulichang - * @since 1.2.5 + * @since 1.3.0 */ @Getter public class SelectColumn { diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/SerializedLambda.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/SerializedLambda.java new file mode 100644 index 0000000..4b7d4cb --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/SerializedLambda.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2011-2022, baomidou (jobob@qq.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.yulichang.toolkit.support; + +import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; + +import java.io.*; + +/** + * 当前类是 {@link java.lang.invoke.SerializedLambda } 的一个镜像 + *

+ * Create by hcl at 2020/7/17 + */ +@SuppressWarnings("ALL") +public class SerializedLambda implements Serializable { + private static final long serialVersionUID = 8025925345765570181L; + + private Class capturingClass; + private String functionalInterfaceClass; + private String functionalInterfaceMethodName; + private String functionalInterfaceMethodSignature; + private String implClass; + private String implMethodName; + private String implMethodSignature; + private int implMethodKind; + private String instantiatedMethodType; + private Object[] capturedArgs; + + public static SerializedLambda extract(Serializable serializable) { + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos)) { + oos.writeObject(serializable); + oos.flush(); + try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())) { + @Override + protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { + Class clazz = super.resolveClass(desc); + return clazz == java.lang.invoke.SerializedLambda.class ? SerializedLambda.class : clazz; + } + + }) { + return (SerializedLambda) ois.readObject(); + } + } catch (IOException | ClassNotFoundException e) { + throw new MybatisPlusException(e); + } + } + + public String getInstantiatedMethodType() { + return instantiatedMethodType; + } + + public Class getCapturingClass() { + return capturingClass; + } + + public String getImplMethodName() { + return implMethodName; + } +} diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/ShadowLambdaMeta.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/ShadowLambdaMeta.java new file mode 100644 index 0000000..d1a3ad4 --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/ShadowLambdaMeta.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2011-2022, baomidou (jobob@qq.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.yulichang.toolkit.support; + + +import com.baomidou.mybatisplus.core.toolkit.StringPool; +import com.github.yulichang.toolkit.ClassUtils; + +/** + * 基于 {@link com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda} 创建的元信息 + *

+ * Create by hcl at 2021/7/7 + */ +public class ShadowLambdaMeta implements LambdaMeta { + private final SerializedLambda lambda; + + public ShadowLambdaMeta(SerializedLambda lambda) { + this.lambda = lambda; + } + + @Override + public String getImplMethodName() { + return lambda.getImplMethodName(); + } + + @Override + public Class getInstantiatedClass() { + String instantiatedMethodType = lambda.getInstantiatedMethodType(); + String instantiatedType = instantiatedMethodType.substring(2, instantiatedMethodType.indexOf(StringPool.SEMICOLON)).replace(StringPool.SLASH, StringPool.DOT); + return ClassUtils.toClassConfident(instantiatedType, lambda.getCapturingClass().getClassLoader()); + } + +} diff --git a/src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java similarity index 100% rename from src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java diff --git a/src/main/java/com/github/yulichang/wrapper/MPJAbstractWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractWrapper.java similarity index 100% rename from src/main/java/com/github/yulichang/wrapper/MPJAbstractWrapper.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractWrapper.java diff --git a/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java similarity index 78% rename from src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java index 61d4050..a73b3f7 100644 --- a/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java @@ -5,16 +5,12 @@ 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.Assert; -import com.baomidou.mybatisplus.core.toolkit.StringPool; -import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import com.baomidou.mybatisplus.core.toolkit.*; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; -import com.github.yulichang.exception.MPJException; -import com.github.yulichang.toolkit.Constant; +import com.github.yulichang.config.ConfigProperties; import com.github.yulichang.toolkit.LambdaUtils; -import com.github.yulichang.toolkit.MPJWrappers; import com.github.yulichang.toolkit.ReflectionKit; +import com.github.yulichang.toolkit.*; import com.github.yulichang.toolkit.support.ColumnCache; import com.github.yulichang.toolkit.support.SelectColumn; import com.github.yulichang.wrapper.enums.BaseFuncEnum; @@ -37,8 +33,6 @@ import java.util.stream.Collectors; /** * 参考 {@link com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper} * Lambda 语法使用 Wrapper - *

- * 推荐使用 MPJWrappers.lambdaJoin();构造 * * @author yulichang * @see MPJWrappers @@ -154,18 +148,18 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper - * 举例 UserDO UserAddressDO 为一对多关系 UserDTO 为结果类 + * 举例 UserDO AddressDO 为一对多关系 UserDTO 为结果类 *

      *     MPJLambdaQueryWrapper wrapper = new MPJLambdaQueryWrapper();
      *     wrapper.selectAll(UserDO.class)
-     *            .selectCollection(UserAddressDO.class, UserDTO::getAddressListDTO)
-     *            .leftJoin(UserAddressDO.class, ...... )
+     *            .selectCollection(AddressDO.class, UserDTO::getAddressListDTO)
+     *            .leftJoin(AddressDO.class, ...... )
      *            .eq(...)
      *            ...
      * 
-     * 会自动将 UserAddressDO类中相同属性的字段 以mybatis的方式映射到UserDTO.addressListDTO属性中
+     * 会自动将 AddressDO类中相同属性的字段 以mybatis的方式映射到UserDTO.addressListDTO属性中
      *
-     * @since 1.2.5
+     * @since 1.3.0
      *
      * @param child    连表数据库实体类
      * @param dtoField 包装类对应的属性
@@ -177,19 +171,16 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper> MPJLambdaWrapper selectCollection(Class child, SFunction dtoField) {
         String dtoFieldName = LambdaUtils.getName(dtoField);
         Class dtoClass = LambdaUtils.getEntityClass(dtoField);
-        Map fieldMap = ReflectionKit.getFieldMap(dtoClass);
+        Map fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
         Field field = fieldMap.get(dtoFieldName);
         this.resultMap = true;
-        Class genericType = ReflectionKit.getGenericType(field);
+        Class genericType = MPJReflectionKit.getGenericType(field);
         MybatisLabel.Builder builder;
         if (genericType == null || genericType.isAssignableFrom(child)) {
             //找不到集合泛型 List List List , 直接查询数据库实体
             builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType());
         } else {
             Class ofType = (Class) genericType;
-            if (ReflectionKit.isPrimitiveOrWrapper(ofType)) {
-                throw new MPJException("collection 不支持基本数据类型");
-            }
             builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), ofType, true);
         }
         this.resultMapMybatisLabel.add(builder.build());
@@ -199,22 +190,22 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper
-     * 举例 UserDO UserAddressDO 为一对多关系  UserDTO 为结果类
+     * 举例 UserDO AddressDO 为一对多关系  UserDTO 为结果类
      * 
-     *     MPJLambdaQueryWrapper wrapper = new MPJLambdaQueryWrapper();
-     *     wrapper.selectAll(UserDO.class)
-     *            .selectCollection(UserAddressDO.class, UserDTO::getAddressListDTO, map -> map
-     *                 .id(UserAddressDO::getId, AddressDTO::getId)                     //如果属性名一致 可以传一个
-     *                 .result(UserAddressDO::getUserId)                                //如果属性名一致 可以传一个
-     *                 .result(UserAddressDO::getAddress, AddressDTO::getAddress)))     //如果属性名一致 可以传一个
-     *            .leftJoin(UserAddressDO.class, ...... )
-     *            .eq(...)
-     *            ...
+     *   MPJLambdaQueryWrapper wrapper = new MPJLambdaQueryWrapper();
+     *   wrapper.selectAll(UserDO.class)
+     *      .selectCollection(AddressDO.class, UserDTO::getAddressListDTO, map -> map
+     *           .id(AddressDO::getId, AddressDTO::getId)                 //如果属性名一致 可以传一个
+     *           .result(AddressDO::getUserId)                            //如果属性名一致 可以传一个
+     *           .result(AddressDO::getAddress, AddressDTO::getAddress))) //如果属性名一致 可以传一个
+     *      .leftJoin(AddressDO.class, ...... )
+     *      .eq(...)
+     *      ...
      * 
      *
-     * 会自动将 UserAddressDO类中指定的字段 以mybatis的方式映射到UserDTO.addressListDTO属性中
+     * 会自动将 AddressDO类中指定的字段 以mybatis的方式映射到UserDTO.addressListDTO属性中
      *
-     * @since 1.2.5
+     * @since 1.3.0
      *
      * @param child      连表数据库实体类
      * @param dtoField   包装类对应的属性
@@ -228,10 +219,10 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper child, SFunction dtoField, MFunc> collection) {
         String dtoFieldName = LambdaUtils.getName(dtoField);
         Class dtoClass = LambdaUtils.getEntityClass(dtoField);
-        Field field = ReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
+        Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
         this.resultMap = true;
         //获取集合泛型
-        Class genericType = ReflectionKit.getGenericType(field);
+        Class genericType = MPJReflectionKit.getGenericType(field);
         Class ofType = (Class) genericType;
         MybatisLabel.Builder builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), ofType, false);
         this.resultMapMybatisLabel.add(collection.apply(builder).build());
@@ -241,17 +232,14 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper MPJLambdaWrapper selectAssociation(Class child, SFunction dtoField) {
         String dtoFieldName = LambdaUtils.getName(dtoField);
         Class dtoClass = LambdaUtils.getEntityClass(dtoField);
-        Map fieldMap = ReflectionKit.getFieldMap(dtoClass);
+        Map fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
         Field field = fieldMap.get(dtoFieldName);
         Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
-        if (ReflectionKit.isPrimitiveOrWrapper(field.getType())) {
-            throw new MPJException("association 不支持基本数据类型");
-        }
         this.resultMap = true;
         MybatisLabel.Builder builder;
         builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), (Class) field.getType(), true);
@@ -262,18 +250,15 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper MPJLambdaWrapper selectAssociation(Class child, SFunction dtoField,
                                                            MFunc> collection) {
         String dtoFieldName = LambdaUtils.getName(dtoField);
         Class dtoClass = LambdaUtils.getEntityClass(dtoField);
-        Field field = ReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
+        Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
         this.resultMap = true;
         Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
-        if (ReflectionKit.isPrimitiveOrWrapper(field.getType())) {
-            throw new MPJException("association 不支持基本数据类型");
-        }
         MybatisLabel.Builder builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), (Class) child, false);
         this.resultMapMybatisLabel.add(collection.apply(builder).build());
         return typedThis;
@@ -314,20 +299,16 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper MPJLambdaWrapper selectFunc(boolean condition, BaseFuncEnum funcEnum, SFunction column, String alias) {
-        if (condition) {
-            ColumnCache cache = getCache(column);
-            selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(column), cache.getColumn(),
-                    cache.getTableFieldInfo(), alias, alias, cache.getKeyType(), false, funcEnum));
-        }
+    public  MPJLambdaWrapper selectFunc(BaseFuncEnum funcEnum, SFunction column, String alias) {
+        ColumnCache cache = getCache(column);
+        selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(column), cache.getColumn(),
+                cache.getTableFieldInfo(), alias, alias, cache.getKeyType(), false, funcEnum));
         return typedThis;
     }
 
     @Override
-    public MPJLambdaWrapper selectFunc(boolean condition, BaseFuncEnum funcEnum, Object column, String alias) {
-        if (condition) {
-            selectColumns.add(SelectColumn.of(null, column.toString(), null, alias, alias, null, false, funcEnum));
-        }
+    public MPJLambdaWrapper selectFunc(BaseFuncEnum funcEnum, Object column, String alias) {
+        selectColumns.add(SelectColumn.of(null, column.toString(), null, alias, alias, null, false, funcEnum));
         return typedThis;
     }
 
@@ -348,7 +329,7 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper {
                 String str = Constant.TABLE_ALIAS + getDefault(subTable.get(i.getClazz())) + StringPool.DOT + i.getColumnName();
                 return (i.getFuncEnum() == null ? str : String.format(i.getFuncEnum().getSql(), str)) +
@@ -412,14 +393,26 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper MPJLambdaWrapper join(String keyWord, boolean condition, Class clazz, OnFunction function) {
-        if (condition) {
-            MPJLambdaWrapper apply = function.apply(instance(keyWord, clazz));
-            onWrappers.add(apply);
-            subTable.put(clazz, tableIndex);
-            tableIndex++;
+    /**
+     * 副表部分逻辑删除支持
+     */
+    public String getLogicSql() {
+        if (ConfigProperties.subTableLogic) {
+            if (CollectionUtils.isEmpty(subTable)) {
+                return StringPool.EMPTY;
+            }
+            return subTable.entrySet().stream().map(entry -> LogicInfoUtils.getLogicInfo(entry.getValue(),
+                    entry.getKey())).collect(Collectors.joining(StringPool.SPACE));
         }
+        return StringPool.EMPTY;
+    }
+
+    @Override
+    public  MPJLambdaWrapper join(String keyWord, Class clazz, OnFunction function) {
+        MPJLambdaWrapper apply = function.apply(instance(keyWord, clazz));
+        subTable.put(clazz, tableIndex);
+        onWrappers.add(apply);
+        tableIndex++;
         return typedThis;
     }
-}
\ No newline at end of file
+}
diff --git a/src/main/java/com/github/yulichang/wrapper/enums/BaseFuncEnum.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/enums/BaseFuncEnum.java
similarity index 100%
rename from src/main/java/com/github/yulichang/wrapper/enums/BaseFuncEnum.java
rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/enums/BaseFuncEnum.java
diff --git a/src/main/java/com/github/yulichang/wrapper/enums/DefaultFuncEnum.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/enums/DefaultFuncEnum.java
similarity index 100%
rename from src/main/java/com/github/yulichang/wrapper/enums/DefaultFuncEnum.java
rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/enums/DefaultFuncEnum.java
diff --git a/src/main/java/com/github/yulichang/wrapper/interfaces/Compare.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Compare.java
similarity index 100%
rename from src/main/java/com/github/yulichang/wrapper/interfaces/Compare.java
rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Compare.java
diff --git a/src/main/java/com/github/yulichang/wrapper/interfaces/Func.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Func.java
similarity index 100%
rename from src/main/java/com/github/yulichang/wrapper/interfaces/Func.java
rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Func.java
diff --git a/src/main/java/com/github/yulichang/wrapper/interfaces/Join.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Join.java
similarity index 100%
rename from src/main/java/com/github/yulichang/wrapper/interfaces/Join.java
rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Join.java
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/LambdaJoin.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/LambdaJoin.java
new file mode 100644
index 0000000..a6c56c8
--- /dev/null
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/LambdaJoin.java
@@ -0,0 +1,88 @@
+package com.github.yulichang.wrapper.interfaces;
+
+import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
+import com.github.yulichang.interfaces.MPJBaseJoin;
+import com.github.yulichang.toolkit.Constant;
+import com.github.yulichang.wrapper.interfaces.on.OnFunction;
+
+/**
+ * @author yulichang
+ */
+@SuppressWarnings("unused")
+public interface LambdaJoin extends MPJBaseJoin {
+
+    /**
+     * left join
+     *
+     * @param clazz 关联的实体类
+     * @param left  条件
+     * @param right 条件
+     */
+    default  Children leftJoin(Class clazz, SFunction left, SFunction right) {
+        return leftJoin(clazz, on -> on.eq(left, right));
+    }
+
+    /**
+     * left join
+     * 

+ * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) + * + * @param clazz 关联实体类 + * @param function 条件 + */ + default Children leftJoin(Class clazz, OnFunction function) { + return join(Constant.LEFT_JOIN, clazz, function); + } + + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, SFunction left, SFunction right) { + return rightJoin(clazz, on -> on.eq(left, right)); + } + + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, OnFunction function) { + return join(Constant.RIGHT_JOIN, clazz, function); + } + + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, SFunction left, SFunction right) { + return innerJoin(clazz, on -> on.eq(left, right)); + } + + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, OnFunction function) { + return join(Constant.INNER_JOIN, clazz, function); + } + + + /** + * ignore 参考 left join + */ + default Children fullJoin(Class clazz, SFunction left, SFunction right) { + return fullJoin(clazz, on -> on.eq(left, right)); + } + + /** + * ignore 参考 left join + */ + default Children fullJoin(Class clazz, OnFunction function) { + return join(Constant.FULL_JOIN, clazz, function); + } + + /** + * 查询基类 可以直接调用此方法实现以上所有功能 + * + * @param keyWord 连表关键字 + * @param clazz 连表实体类 + * @param function 关联条件 + */ + Children join(String keyWord, Class clazz, OnFunction function); +} diff --git a/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java similarity index 56% rename from src/main/java/com/github/yulichang/wrapper/interfaces/Query.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java index 815c269..63b1c55 100644 --- a/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java @@ -75,40 +75,20 @@ public interface Query extends Serializable { * @param column 函数作用的字段 * @param alias 别名 */ - Children selectFunc(boolean condition, BaseFuncEnum funcEnum, Object column, String alias); + Children selectFunc(BaseFuncEnum funcEnum, Object column, String alias); - Children selectFunc(boolean condition, BaseFuncEnum funcEnum, SFunction column, String alias); - - default Children selectFunc(BaseFuncEnum funcEnum, Object column, String alias) { - return selectFunc(true, funcEnum, column, alias); - } - - default Children selectFunc(BaseFuncEnum funcEnum, SFunction column, String alias) { - return selectFunc(true, funcEnum, column, alias); - } + Children selectFunc(BaseFuncEnum funcEnum, SFunction column, String alias); default Children selectFunc(BaseFuncEnum funcEnum, SFunction column, SFunction alias) { - return selectFunc(true, funcEnum, column, LambdaUtils.getName(alias)); + return selectFunc(funcEnum, column, LambdaUtils.getName(alias)); } default Children selectFunc(BaseFuncEnum funcEnum, SFunction column) { - return selectFunc(true, funcEnum, column, column); + return selectFunc(funcEnum, column, column); } default Children selectFunc(BaseFuncEnum funcEnum, Object column, SFunction alias) { - return selectFunc(true, funcEnum, column, LambdaUtils.getName(alias)); - } - - default Children selectFunc(boolean condition, BaseFuncEnum funcEnum, SFunction column, SFunction alias) { - return selectFunc(condition, funcEnum, column, LambdaUtils.getName(alias)); - } - - default Children selectFunc(boolean condition, BaseFuncEnum funcEnum, SFunction column) { - return selectFunc(condition, funcEnum, column, column); - } - - default Children selectFunc(boolean condition, BaseFuncEnum funcEnum, Object column, SFunction alias) { - return selectFunc(condition, funcEnum, column, LambdaUtils.getName(alias)); + return selectFunc(funcEnum, column, LambdaUtils.getName(alias)); } /** @@ -138,18 +118,6 @@ public interface Query extends Serializable { return selectFunc(DefaultFuncEnum.SUM, column, alias); } - default Children selectSum(boolean condition, SFunction column) { - return selectFunc(condition, DefaultFuncEnum.SUM, column); - } - - default Children selectSum(boolean condition, SFunction column, SFunction alias) { - return selectFunc(condition, DefaultFuncEnum.SUM, column, alias); - } - - default Children selectSum(boolean condition, SFunction column, String alias) { - return selectFunc(condition, DefaultFuncEnum.SUM, column, alias); - } - /** * COUNT() */ @@ -173,26 +141,6 @@ public interface Query extends Serializable { return selectFunc(DefaultFuncEnum.COUNT, column, alias); } - default Children selectCount(boolean condition, SFunction column) { - return selectFunc(condition, DefaultFuncEnum.COUNT, column); - } - - default Children selectCount(boolean condition, Object column, SFunction alias) { - return selectFunc(condition, DefaultFuncEnum.COUNT, column, alias); - } - - default Children selectCount(boolean condition, Object column, String alias) { - return selectFunc(condition, DefaultFuncEnum.COUNT, column, alias); - } - - default Children selectCount(boolean condition, SFunction column, SFunction alias) { - return selectFunc(condition, DefaultFuncEnum.COUNT, column, alias); - } - - default Children selectCount(boolean condition, SFunction column, String alias) { - return selectFunc(condition, DefaultFuncEnum.COUNT, column, alias); - } - /** * MAX() */ @@ -208,18 +156,6 @@ public interface Query extends Serializable { return selectFunc(DefaultFuncEnum.MAX, column, alias); } - default Children selectMax(boolean condition, SFunction column) { - return selectFunc(condition, DefaultFuncEnum.MAX, column); - } - - default Children selectMax(boolean condition, SFunction column, SFunction alias) { - return selectFunc(condition, DefaultFuncEnum.MAX, column, alias); - } - - default Children selectMax(boolean condition, SFunction column, String alias) { - return selectFunc(condition, DefaultFuncEnum.MAX, column, alias); - } - /** * MIN() */ @@ -235,18 +171,6 @@ public interface Query extends Serializable { return selectFunc(DefaultFuncEnum.MIN, column, alias); } - default Children selectMin(boolean condition, SFunction column) { - return selectFunc(condition, DefaultFuncEnum.MIN, column); - } - - default Children selectMin(boolean condition, SFunction column, SFunction alias) { - return selectFunc(condition, DefaultFuncEnum.MIN, column, alias); - } - - default Children selectMin(boolean condition, SFunction column, String alias) { - return selectFunc(condition, DefaultFuncEnum.MIN, column, alias); - } - /** * MIN() */ @@ -262,18 +186,6 @@ public interface Query extends Serializable { return selectFunc(DefaultFuncEnum.AVG, column, alias); } - default Children selectAvg(boolean condition, SFunction column) { - return selectFunc(condition, DefaultFuncEnum.AVG, column); - } - - default Children selectAvg(boolean condition, SFunction column, SFunction alias) { - return selectFunc(condition, DefaultFuncEnum.AVG, column, alias); - } - - default Children selectAvg(boolean condition, SFunction column, String alias) { - return selectFunc(condition, DefaultFuncEnum.AVG, column, alias); - } - /** * LEN() */ @@ -288,16 +200,4 @@ public interface Query extends Serializable { default Children selectLen(SFunction column, String alias) { return selectFunc(DefaultFuncEnum.LEN, column, alias); } - - default Children selectLen(boolean condition, SFunction column) { - return selectFunc(condition, DefaultFuncEnum.LEN, column); - } - - default Children selectLen(boolean condition, SFunction column, SFunction alias) { - return selectFunc(condition, DefaultFuncEnum.LEN, column, alias); - } - - default Children selectLen(boolean condition, SFunction column, String alias) { - return selectFunc(condition, DefaultFuncEnum.LEN, column, alias); - } } diff --git a/src/main/java/com/github/yulichang/wrapper/interfaces/on/OnCompare.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/on/OnCompare.java similarity index 100% rename from src/main/java/com/github/yulichang/wrapper/interfaces/on/OnCompare.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/on/OnCompare.java diff --git a/src/main/java/com/github/yulichang/wrapper/interfaces/on/OnFunction.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/on/OnFunction.java similarity index 100% rename from src/main/java/com/github/yulichang/wrapper/interfaces/on/OnFunction.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/on/OnFunction.java diff --git a/src/main/java/com/github/yulichang/wrapper/resultmap/MFunc.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MFunc.java similarity index 100% rename from src/main/java/com/github/yulichang/wrapper/resultmap/MFunc.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MFunc.java diff --git a/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java similarity index 88% rename from src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java index bea97ca..4e80bc8 100644 --- a/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java @@ -6,9 +6,8 @@ import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.Assert; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; -import com.github.yulichang.exception.MPJException; import com.github.yulichang.toolkit.LambdaUtils; -import com.github.yulichang.toolkit.ReflectionKit; +import com.github.yulichang.toolkit.MPJReflectionKit; import lombok.Getter; import java.lang.reflect.Field; @@ -23,7 +22,7 @@ import java.util.stream.Collectors; * collection 标签 目前先支持这几个属性 后续在扩展 * * @author yulichang - * @since 1.2.5 + * @since 1.3.0 */ @Getter public class MybatisLabel { @@ -120,18 +119,15 @@ public class MybatisLabel { public > Builder collection(Class entityClass, SFunction func) { String dtoFieldName = LambdaUtils.getName(func); Class dtoClass = LambdaUtils.getEntityClass(func); - Map fieldMap = ReflectionKit.getFieldMap(dtoClass); + Map fieldMap = MPJReflectionKit.getFieldMap(dtoClass); Field field = fieldMap.get(dtoFieldName); - Class genericType = ReflectionKit.getGenericType(field); + Class genericType = MPJReflectionKit.getGenericType(field); MybatisLabel.Builder builder; if (genericType == null || genericType.isAssignableFrom(entityClass)) { //找不到集合泛型 List List List , 直接查询数据库实体 builder = new Builder<>(dtoFieldName, entityClass, field.getType()); } else { Class ofType = (Class) genericType; - if (ReflectionKit.isPrimitiveOrWrapper(ofType)) { - throw new MPJException("collection 不支持基本数据类型"); - } builder = new Builder<>(dtoFieldName, entityClass, field.getType(), ofType, true); } mybatisLabel.mybatisLabels.add(builder.build()); @@ -144,9 +140,9 @@ public class MybatisLabel { public > Builder collection(Class entityClass, SFunction func, MFunc> mFunc) { String dtoFieldName = LambdaUtils.getName(func); Class dtoClass = LambdaUtils.getEntityClass(func); - Field field = ReflectionKit.getFieldMap(dtoClass).get(dtoFieldName); + Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName); //获取集合泛型 - Class genericType = ReflectionKit.getGenericType(field); + Class genericType = MPJReflectionKit.getGenericType(field); Class ofType = (Class) genericType; MybatisLabel.Builder builder = new MybatisLabel.Builder<>(dtoFieldName, entityClass, field.getType(), ofType, false); mybatisLabel.mybatisLabels.add(mFunc.apply(builder).build()); @@ -158,13 +154,10 @@ public class MybatisLabel { */ public Builder association(Class child, SFunction dtoField) { Class dtoClass = LambdaUtils.getEntityClass(dtoField); - Map fieldMap = ReflectionKit.getFieldMap(dtoClass); + Map fieldMap = MPJReflectionKit.getFieldMap(dtoClass); String dtoFieldName = LambdaUtils.getName(dtoField); Field field = fieldMap.get(dtoFieldName); Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类"); - if (ReflectionKit.isPrimitiveOrWrapper(field.getType())) { - throw new MPJException("association 不支持基本数据类型"); - } MybatisLabel.Builder builder; builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), (Class) field.getType(), true); mybatisLabel.mybatisLabels.add(builder.build()); @@ -178,11 +171,8 @@ public class MybatisLabel { MFunc> collection) { String dtoFieldName = LambdaUtils.getName(dtoField); Class dtoClass = LambdaUtils.getEntityClass(dtoField); - Field field = ReflectionKit.getFieldMap(dtoClass).get(dtoFieldName); + Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName); Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类"); - if (ReflectionKit.isPrimitiveOrWrapper(field.getType())) { - throw new MPJException("association 不支持基本数据类型"); - } MybatisLabel.Builder builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), (Class) child, false); mybatisLabel.mybatisLabels.add(collection.apply(builder).build()); return this; @@ -198,7 +188,7 @@ public class MybatisLabel { private void autoBuild(boolean auto, Class entityClass, Class tagClass) { TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass); - Map tagMap = ReflectionKit.getFieldMap(tagClass); + Map tagMap = MPJReflectionKit.getFieldMap(tagClass); if (auto && !tagMap.isEmpty()) { Function build = field -> { Result result = new Result(); @@ -235,4 +225,4 @@ public class MybatisLabel { return result; } } -} \ No newline at end of file +} diff --git a/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java similarity index 99% rename from src/main/java/com/github/yulichang/wrapper/resultmap/Result.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java index 99675e7..6105c79 100644 --- a/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java @@ -17,7 +17,7 @@ import org.apache.ibatis.type.TypeHandler; * result 标签 * * @author yulichang - * @since 1.2.5 + * @since 1.3.0 */ @Getter @Setter(AccessLevel.PACKAGE) diff --git a/mybatis-plus-join-test/pom.xml b/mybatis-plus-join-test/pom.xml new file mode 100644 index 0000000..53dec2d --- /dev/null +++ b/mybatis-plus-join-test/pom.xml @@ -0,0 +1,104 @@ + + + + 4.0.0 + + com.github.yulichang + mybatis-plus-join-root + 1.3.8 + + 1.3.8 + mybatis-plus-join-test + mybatis-plus-join-test + + An enhanced toolkit of Mybatis-Plus to simplify development. + https://github.com/yulichang/mybatis-plus-join + + + The Apache License, Version 2.0 + https://www.apache.org/licenses/LICENSE-2.0.txt + + + + + mybatis-plus-join + yulichang + yu_lichang@qq.com + + + + scm:git:https://github.com/yulichang/mybatis-plus-join.git + scm:git:https://github.com/yulichang/mybatis-plus-join.git + https://github.com/yulichang/mybatis-plus-join + + + + 2.5.4 + + 1.8 + 1.8 + 1.8 + 1.8 + github + UTF-8 + + + + + org.springframework.boot + spring-boot-starter + ${springboot.version} + + + org.springframework.boot + spring-boot-starter-test + ${springboot.version} + test + + + com.h2database + h2 + 2.1.214 + runtime + + + org.projectlombok + lombok + 1.18.24 + + + com.baomidou + mybatis-plus-boot-starter + 3.5.2 + + + com.github.yulichang + mybatis-plus-join-boot-starter + 1.3.8 + + + + + + + + + com.fasterxml.jackson.core + jackson-databind + 2.14.0 + + + com.baomidou + mybatis-plus-boot-starter + 3.5.2 + + + org.springframework + spring-jdbc + 5.3.4 + + + + \ No newline at end of file diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/Application.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/Application.java new file mode 100644 index 0000000..b8b9aee --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/Application.java @@ -0,0 +1,9 @@ +package com.github.yulichang.test.collection; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + + +@SpringBootApplication +public class Application { + +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/CollectionTest.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/CollectionTest.java new file mode 100644 index 0000000..1c16508 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/CollectionTest.java @@ -0,0 +1,48 @@ +package com.github.yulichang.test.collection; + +import com.github.yulichang.test.collection.dto.TableADTO; +import com.github.yulichang.test.collection.dto.TableBDTO; +import com.github.yulichang.test.collection.dto.TableCDTO; +import com.github.yulichang.test.collection.dto.TableDDTO; +import com.github.yulichang.test.collection.entity.*; +import com.github.yulichang.test.collection.mapper.TableAMapper; +import com.github.yulichang.wrapper.MPJLambdaWrapper; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 连表测试类 + *

+ * 支持mybatis-plus 查询枚举字段 + * 支持mybatis-plus typeHandle功能 + *

+ * 移除了mybatis-plus 逻辑删除支持,逻辑删除需要在连表查询时自己添加对应的条件 + */ +@SuppressWarnings("unused") +@SpringBootTest(properties = "spring.profiles.active=collection") +class CollectionTest { + @Resource + private TableAMapper tableAMapper; + + @Test + void testJoinCollection() { + //4层嵌套 a对多b b对多c c对多d d对多e + MPJLambdaWrapper wrapper = new MPJLambdaWrapper() + .selectAll(TableA.class) + .selectCollection(TableB.class, TableADTO::getBList, b -> b + .collection(TableC.class, TableBDTO::getCcList, c -> c + .collection(TableD.class, TableCDTO::getDList, d -> d + .collection(TableE.class, TableDDTO::getEList)))) + .leftJoin(TableB.class, TableB::getAid, TableA::getId) + .leftJoin(TableC.class, TableC::getBid, TableB::getId) + .leftJoin(TableD.class, TableD::getCid, TableC::getId) + .leftJoin(TableE.class, TableE::getDid, TableD::getId); + + List dtos = tableAMapper.selectJoinList(TableADTO.class, wrapper); + assert dtos.size() == 1; + System.out.println(dtos); + } +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/config/MybatisPlusConfig.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/config/MybatisPlusConfig.java new file mode 100644 index 0000000..f3600a4 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/config/MybatisPlusConfig.java @@ -0,0 +1,24 @@ +package com.github.yulichang.test.collection.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配置 + */ +@Configuration +public class MybatisPlusConfig { + + /** + * 分页插件 + */ + @Bean + public MybatisPlusInterceptor paginationInterceptor() { + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); + PaginationInnerInterceptor page = new PaginationInnerInterceptor(); + interceptor.addInnerInterceptor(page); + return interceptor; + } +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/dto/TableADTO.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/dto/TableADTO.java new file mode 100644 index 0000000..baec4bb --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/dto/TableADTO.java @@ -0,0 +1,17 @@ +package com.github.yulichang.test.collection.dto; + +import lombok.Data; +import lombok.ToString; + +import java.util.List; + +@Data +@ToString +public class TableADTO { + + private Integer id; + + private String name; + + private List bList; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/dto/TableBDTO.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/dto/TableBDTO.java new file mode 100644 index 0000000..b8643b5 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/dto/TableBDTO.java @@ -0,0 +1,19 @@ +package com.github.yulichang.test.collection.dto; + +import lombok.Data; +import lombok.ToString; + +import java.util.List; + +@Data +@ToString +public class TableBDTO { + + private Integer id; + + private Integer aid; + + private String name; + + private List ccList; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/dto/TableCDTO.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/dto/TableCDTO.java new file mode 100644 index 0000000..cb4c573 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/dto/TableCDTO.java @@ -0,0 +1,19 @@ +package com.github.yulichang.test.collection.dto; + +import lombok.Data; +import lombok.ToString; + +import java.util.List; + +@Data +@ToString +public class TableCDTO { + + private Integer id; + + private Integer bid; + + private String name; + + private List dList; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/dto/TableDDTO.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/dto/TableDDTO.java new file mode 100644 index 0000000..202eaf8 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/dto/TableDDTO.java @@ -0,0 +1,19 @@ +package com.github.yulichang.test.collection.dto; + +import lombok.Data; +import lombok.ToString; + +import java.util.List; + +@Data +@ToString +public class TableDDTO { + + private Integer id; + + private Integer cid; + + private String name; + + private List eList; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/dto/TableEDTO.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/dto/TableEDTO.java new file mode 100644 index 0000000..ed47f67 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/dto/TableEDTO.java @@ -0,0 +1,15 @@ +package com.github.yulichang.test.collection.dto; + +import lombok.Data; +import lombok.ToString; + +@Data +@ToString +public class TableEDTO { + + private Integer id; + + private Integer did; + + private String name; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/entity/TableA.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/entity/TableA.java new file mode 100644 index 0000000..70209bc --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/entity/TableA.java @@ -0,0 +1,15 @@ +package com.github.yulichang.test.collection.entity; + +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +@Data +@TableName("table_a") +public class TableA { + + @TableId + private Integer id; + + private String name; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/entity/TableB.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/entity/TableB.java new file mode 100644 index 0000000..bac6d48 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/entity/TableB.java @@ -0,0 +1,17 @@ +package com.github.yulichang.test.collection.entity; + +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +@Data +@TableName("table_b") +public class TableB { + + @TableId + private Integer id; + + private Integer aid; + + private String name; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/entity/TableC.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/entity/TableC.java new file mode 100644 index 0000000..f2d8c30 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/entity/TableC.java @@ -0,0 +1,17 @@ +package com.github.yulichang.test.collection.entity; + +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +@Data +@TableName("table_c") +public class TableC { + + @TableId + private Integer id; + + private Integer bid; + + private String name; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/entity/TableD.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/entity/TableD.java new file mode 100644 index 0000000..a73ac1d --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/entity/TableD.java @@ -0,0 +1,17 @@ +package com.github.yulichang.test.collection.entity; + +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +@Data +@TableName("table_d") +public class TableD { + + @TableId + private Integer id; + + private Integer cid; + + private String name; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/entity/TableE.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/entity/TableE.java new file mode 100644 index 0000000..6299c75 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/entity/TableE.java @@ -0,0 +1,17 @@ +package com.github.yulichang.test.collection.entity; + +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +@Data +@TableName("table_e") +public class TableE { + + @TableId + private Integer id; + + private Integer did; + + private String name; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/enums/Sex.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/enums/Sex.java new file mode 100644 index 0000000..a5382ee --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/enums/Sex.java @@ -0,0 +1,24 @@ +package com.github.yulichang.test.collection.enums; + +import com.baomidou.mybatisplus.annotation.EnumValue; +import lombok.Getter; +import lombok.ToString; + +@Getter +@ToString +public enum Sex { + + MAN(0, "男"), + + WOMAN(1, "女"); + + @EnumValue + private final int code; + private final String des; + + Sex(int code, String des) { + this.code = code; + this.des = des; + } + +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/mapper/TableAMapper.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/mapper/TableAMapper.java new file mode 100644 index 0000000..1c71440 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/mapper/TableAMapper.java @@ -0,0 +1,10 @@ +package com.github.yulichang.test.collection.mapper; + +import com.github.yulichang.base.MPJBaseMapper; +import com.github.yulichang.test.collection.entity.TableA; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface TableAMapper extends MPJBaseMapper { + +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/mapper/TableBMapper.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/mapper/TableBMapper.java new file mode 100644 index 0000000..c6680f0 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/mapper/TableBMapper.java @@ -0,0 +1,11 @@ +package com.github.yulichang.test.collection.mapper; + +import com.github.yulichang.base.MPJBaseMapper; +import com.github.yulichang.test.collection.entity.TableB; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +@SuppressWarnings("unused") +public interface TableBMapper extends MPJBaseMapper { + +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/mapper/TableCMapper.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/mapper/TableCMapper.java new file mode 100644 index 0000000..148903d --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/mapper/TableCMapper.java @@ -0,0 +1,11 @@ +package com.github.yulichang.test.collection.mapper; + +import com.github.yulichang.base.MPJBaseMapper; +import com.github.yulichang.test.collection.entity.TableC; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +@SuppressWarnings("unused") +public interface TableCMapper extends MPJBaseMapper { + +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/mapper/TableDMapper.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/mapper/TableDMapper.java new file mode 100644 index 0000000..97532cd --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/mapper/TableDMapper.java @@ -0,0 +1,11 @@ +package com.github.yulichang.test.collection.mapper; + +import com.github.yulichang.base.MPJBaseMapper; +import com.github.yulichang.test.collection.entity.TableD; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +@SuppressWarnings("unused") +public interface TableDMapper extends MPJBaseMapper { + +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/mapper/TableEMapper.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/mapper/TableEMapper.java new file mode 100644 index 0000000..10964ff --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/collection/mapper/TableEMapper.java @@ -0,0 +1,11 @@ +package com.github.yulichang.test.collection.mapper; + +import com.github.yulichang.base.MPJBaseMapper; +import com.github.yulichang.test.collection.entity.TableE; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +@SuppressWarnings("unused") +public interface TableEMapper extends MPJBaseMapper { + +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/Application.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/Application.java new file mode 100644 index 0000000..84473d9 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/Application.java @@ -0,0 +1,9 @@ +package com.github.yulichang.test.join; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + + +@SpringBootApplication +public class Application { + +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java new file mode 100644 index 0000000..847869b --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java @@ -0,0 +1,137 @@ +package com.github.yulichang.test.join; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.github.yulichang.test.join.dto.AddressDTO; +import com.github.yulichang.test.join.dto.UserDTO; +import com.github.yulichang.test.join.entity.AddressDO; +import com.github.yulichang.test.join.entity.AreaDO; +import com.github.yulichang.test.join.entity.UserDO; +import com.github.yulichang.test.join.mapper.UserMapper; +import com.github.yulichang.toolkit.MPJWrappers; +import com.github.yulichang.wrapper.MPJLambdaWrapper; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Map; + +/** + * 连表测试类 + *

+ * 支持mybatis-plus 查询枚举字段 + * 支持mybatis-plus typeHandle功能 + *

+ * 移除了mybatis-plus 逻辑删除支持,逻辑删除需要在连表查询时自己添加对应的条件 + */ +@SuppressWarnings("unused") +@SpringBootTest(properties = "spring.profiles.active=join") +class LambdaWrapperTest { + @Resource + private UserMapper userMapper; + + @Test + void testJoin() { + MPJLambdaWrapper wrapper = new MPJLambdaWrapper() + .selectAll(UserDO.class) + .selectCollection(AddressDO.class, UserDTO::getAddressList, addr -> addr + .association(AreaDO.class, AddressDTO::getArea)) + .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) + .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId) + .orderByDesc(UserDO::getId) + .last("unfdjknsnnjkfd"); + List list = userMapper.selectJoinList(UserDTO.class, wrapper); + String s = wrapper.getSqlSegment(); + System.out.println(s); + if (list.size() != 1) { + throw new RuntimeException(); + } + list.forEach(System.out::println); + } + + + /** + * 简单的分页关联查询 lambda + */ + @Test + void test1() { + IPage iPage = userMapper.selectJoinPage(new Page<>(1, 10), UserDTO.class, + MPJWrappers.lambdaJoin() + .selectAll(UserDO.class) + .select(AddressDO::getAddress) + .select(AreaDO::getProvince) + .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) + .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId) + .last("unfdjknsnnjkfd")); + iPage.getRecords().forEach(System.out::println); + } + + /** + * 简单的分页关联查询 lambda + * ON语句多条件 + */ + @Test + void test3() { + IPage page = userMapper.selectJoinPage(new Page<>(1, 10), UserDTO.class, + MPJWrappers.lambdaJoin() + .selectAll(UserDO.class) + .select(AddressDO::getAddress) + .leftJoin(AddressDO.class, on -> on + .eq(UserDO::getId, AddressDO::getUserId) + .eq(UserDO::getId, AddressDO::getUserId)) + .eq(UserDO::getId, 1) + .and(i -> i.eq(UserDO::getHeadImg, "er") + .or() + .eq(AddressDO::getUserId, 1)) + .eq(UserDO::getId, 1) + .last("ljfnkegrwbgejkwbenf")); + page.getRecords().forEach(System.out::println); + } + + /** + * 简单的函数使用 + */ + @Test + void test4() { + UserDTO one = userMapper.selectJoinOne(UserDTO.class, MPJWrappers.lambdaJoin() + .selectSum(UserDO::getId) + .selectMax(UserDO::getId, UserDTO::getHeadImg) + .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) + .last("unfdjknsnnjkfd")); + System.out.println(one); + } + + + /** + * 忽略个别查询字段 + */ + @Test + void test6() { + userMapper.selectPage(new Page<>(1, 10), new QueryWrapper<>()); + IPage page = userMapper.selectJoinPage(new Page<>(1, 10), UserDTO.class, + MPJWrappers.lambdaJoin() + .selectAll(UserDO.class) + .select(AddressDO.class, p -> true) + .select(AddressDO::getAddress) + .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) + .eq(UserDO::getId, 1) + .last("unfdjknsnnjkfd")); + page.getRecords().forEach(System.out::println); + } + + + /** + * 关联查询返回map + */ + @Test + void test7() { + List> list = userMapper.selectJoinMaps(MPJWrappers.lambdaJoin() + .selectAll(UserDO.class) + .select(AddressDO::getAddress) + .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) + .last("unfdjknsnnjkfd")); + list.forEach(System.out::println); + } +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/QueryWrapperTest.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/QueryWrapperTest.java new file mode 100644 index 0000000..a6c6c37 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/QueryWrapperTest.java @@ -0,0 +1,85 @@ +package com.github.yulichang.test.join; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.github.yulichang.query.MPJQueryWrapper; +import com.github.yulichang.test.join.dto.UserDTO; +import com.github.yulichang.test.join.entity.UserDO; +import com.github.yulichang.test.join.mapper.UserMapper; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Map; + +@SpringBootTest(properties = "spring.profiles.active=join") +class QueryWrapperTest { + @Resource + private UserMapper userMapper; + + /** + * 链表查询 + */ + @Test + void test1() { + UserDO userDO = userMapper.selectJoinOne(UserDO.class, new MPJQueryWrapper() + .selectAll(UserDO.class).last("LIMIT 1")); + System.out.println(userDO); + + UserDTO dto = userMapper.selectJoinOne(UserDTO.class, new MPJQueryWrapper() + .selectAll(UserDO.class) + .select("name AS nameName") + .last("LIMIT 1")); + System.out.println(dto); + } + + @Test + void test2() { + List userDO = userMapper.selectJoinList(UserDO.class, new MPJQueryWrapper() + .selectAll(UserDO.class) + .leftJoin("address t2 on t2.user_id = t.id") + .le("t.id ", 10)); + System.out.println(userDO); + + List dto = userMapper.selectJoinList(UserDTO.class, new MPJQueryWrapper() + .selectAll(UserDO.class) + .select("t2.address AS userAddress") + .leftJoin("address t2 on t2.user_id = t.id") + .le("t.id ", 10)); + System.out.println(dto); + } + + @Test + void test3() { + IPage userDO = userMapper.selectJoinPage(new Page<>(1, 10), UserDO.class, new MPJQueryWrapper() + .selectAll(UserDO.class) + .leftJoin("address t2 on t2.user_id = t.id") + .lt("t.id ", 5)); + System.out.println(userDO); + + IPage dto = userMapper.selectJoinPage(new Page<>(1, 10), UserDTO.class, new MPJQueryWrapper() + .selectAll(UserDO.class) + .select("t2.address AS userAddress") + .leftJoin("address t2 on t2.user_id = t.id") + .lt("t.id ", 5)); + System.out.println(dto); + } + + @Test + void test4() { + List> maps = userMapper.selectJoinMaps(new MPJQueryWrapper() + .selectAll(UserDO.class) + .leftJoin("address t2 on t2.user_id = t.id") + .lt("t.id ", 5)); + System.out.println(maps); + + List> joinMaps = userMapper.selectJoinMaps(new MPJQueryWrapper() + .selectAll(UserDO.class) + .select("t2.address AS userAddress") + .leftJoin("address t2 on t2.user_id = t.id") + .lt("t.id ", 5)); + System.out.println(joinMaps); + } + +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/config/MybatisPlusConfig.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/config/MybatisPlusConfig.java new file mode 100644 index 0000000..caf861c --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/config/MybatisPlusConfig.java @@ -0,0 +1,25 @@ +package com.github.yulichang.test.join.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配置 + */ +@Configuration +public class MybatisPlusConfig { + + /** + * 分页插件 + */ + @Bean + public MybatisPlusInterceptor paginationInterceptor() { + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); + PaginationInnerInterceptor page = new PaginationInnerInterceptor(); + page.setOptimizeJoin(false); + interceptor.addInnerInterceptor(page); + return interceptor; + } +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/dto/AddressDTO.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/dto/AddressDTO.java new file mode 100644 index 0000000..94daed2 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/dto/AddressDTO.java @@ -0,0 +1,26 @@ +package com.github.yulichang.test.join.dto; + +import com.github.yulichang.test.join.entity.AreaDO; +import lombok.Data; + +import java.util.List; + +@Data +public class AddressDTO { + + private Integer id; + + private Integer userId; + + private Integer areaId; + + private String tel; + + private String address; + + private Boolean del; + + private List areaList; + + private AreaDO area; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/dto/AreaDTO.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/dto/AreaDTO.java new file mode 100644 index 0000000..a78b7a8 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/dto/AreaDTO.java @@ -0,0 +1,27 @@ +package com.github.yulichang.test.join.dto; + +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import lombok.experimental.Accessors; + +@Data +@ToString +@Accessors(chain = true) +@EqualsAndHashCode +@TableName("area") +public class AreaDTO { + + private Integer id; + + private String province; + + private String city; + + private String area; + + private String postcode; + + private Boolean del; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/dto/TableDTO.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/dto/TableDTO.java new file mode 100644 index 0000000..c3d6aae --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/dto/TableDTO.java @@ -0,0 +1,61 @@ +package com.github.yulichang.test.join.dto; + +import lombok.Data; + +import java.util.List; + +@Data +public class TableDTO { + + private Integer id; + + private String name; + + private List bbList; + + @Data + public static class TableBDTO { + + private Integer id; + + private Integer aid; + + private String name; + + private List ccList; + } + + @Data + public static class TableCDTO { + + private Integer id; + + private Integer bid; + + private String name; + + private List ddList; + } + + @Data + public static class TableDDTO { + + private Integer id; + + private Integer cid; + + private String name; + + private List eeList; + } + + @Data + public static class TableEDTO { + + private Integer id; + + private Integer did; + + private String name; + } +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/dto/UserDTO.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/dto/UserDTO.java new file mode 100644 index 0000000..382a830 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/dto/UserDTO.java @@ -0,0 +1,41 @@ +package com.github.yulichang.test.join.dto; + +import com.github.yulichang.test.join.enums.Sex; +import lombok.Data; +import lombok.ToString; + +import java.util.List; +import java.util.Map; + + +/** + * userDTO + */ +@Data +@ToString +public class UserDTO { + /** user */ + private Integer id; + /** user */ + private Map name; + /** user */ + private Sex sex; + /** user */ + private String headImg; + /** user */ + private String userHeadImg;//同 headImg 别名测试 + /** user_address */ + private String tel; + /** user_address */ + private String address; + /** user_address */ + private String userAddress; + /** area */ + private String province; + /** area */ + private String city; + /** area */ + private String area; + + private List addressList; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/entity/AddressDO.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/entity/AddressDO.java new file mode 100644 index 0000000..b5c8ec1 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/entity/AddressDO.java @@ -0,0 +1,31 @@ +package com.github.yulichang.test.join.entity; + +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableLogic; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import lombok.experimental.Accessors; + +@Data +@ToString +@Accessors(chain = true) +@EqualsAndHashCode +@TableName("address") +public class AddressDO { + + @TableId + private Integer id; + + private Integer userId; + + private Integer areaId; + + private String tel; + + private String address; + + @TableLogic + private Boolean del; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/entity/AreaDO.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/entity/AreaDO.java new file mode 100644 index 0000000..d2c00d0 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/entity/AreaDO.java @@ -0,0 +1,31 @@ +package com.github.yulichang.test.join.entity; + +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableLogic; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import lombok.experimental.Accessors; + +@Data +@ToString +@Accessors(chain = true) +@EqualsAndHashCode +@TableName("area") +public class AreaDO { + + @TableId + private Integer id; + + private String province; + + private String city; + + private String area; + + private String postcode; + + @TableLogic + private Boolean del; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/entity/UserDO.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/entity/UserDO.java new file mode 100644 index 0000000..8f589a4 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/entity/UserDO.java @@ -0,0 +1,37 @@ +package com.github.yulichang.test.join.entity; + +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableLogic; +import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler; +import com.github.yulichang.test.join.enums.Sex; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import lombok.experimental.Accessors; + +import java.util.Map; + +@Data +@ToString +@Accessors(chain = true) +@EqualsAndHashCode +@TableName(value = "`user`",autoResultMap = true) +public class UserDO { + + @TableId + private Integer id; + + @TableField(value = "`name`", typeHandler = JacksonTypeHandler.class) + private Map aName; + + private Sex sex; + + private String headImg; + + private Integer addressId; + + @TableLogic + private Boolean del; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/enums/Sex.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/enums/Sex.java new file mode 100644 index 0000000..d6dc1cc --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/enums/Sex.java @@ -0,0 +1,24 @@ +package com.github.yulichang.test.join.enums; + +import com.baomidou.mybatisplus.annotation.EnumValue; +import lombok.Getter; +import lombok.ToString; + +@Getter +@ToString +public enum Sex { + + MAN(0, "男"), + + WOMAN(1, "女"); + + @EnumValue + private final int code; + private final String des; + + Sex(int code, String des) { + this.code = code; + this.des = des; + } + +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/mapper/AddressMapper.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/mapper/AddressMapper.java new file mode 100644 index 0000000..246de25 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/mapper/AddressMapper.java @@ -0,0 +1,10 @@ +package com.github.yulichang.test.join.mapper; + +import com.github.yulichang.base.MPJBaseMapper; +import com.github.yulichang.test.join.entity.AddressDO; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +@SuppressWarnings("unused") +public interface AddressMapper extends MPJBaseMapper { +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/mapper/AreaMapper.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/mapper/AreaMapper.java new file mode 100644 index 0000000..2eb72d7 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/mapper/AreaMapper.java @@ -0,0 +1,10 @@ +package com.github.yulichang.test.join.mapper; + +import com.github.yulichang.base.MPJBaseMapper; +import com.github.yulichang.test.join.entity.AreaDO; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +@SuppressWarnings("unused") +public interface AreaMapper extends MPJBaseMapper { +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/mapper/UserMapper.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/mapper/UserMapper.java new file mode 100644 index 0000000..acbbdfa --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/join/mapper/UserMapper.java @@ -0,0 +1,10 @@ +package com.github.yulichang.test.join.mapper; + +import com.github.yulichang.base.MPJBaseMapper; +import com.github.yulichang.test.join.entity.UserDO; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface UserMapper extends MPJBaseMapper { + +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/Application.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/Application.java new file mode 100644 index 0000000..ade9a3a --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/Application.java @@ -0,0 +1,8 @@ +package com.github.yulichang.test.mapping; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/MappingTest.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/MappingTest.java new file mode 100644 index 0000000..e59c1a6 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/MappingTest.java @@ -0,0 +1,27 @@ +package com.github.yulichang.test.mapping; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.github.yulichang.test.mapping.entity.UserDO; +import com.github.yulichang.test.mapping.mapper.UserMapper; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 注解一对一,一对多查询 + */ +@SpringBootTest(properties = "spring.profiles.active=mapping") +@SuppressWarnings("unused") +class MappingTest { + + @Resource + private UserMapper userMapper; + + @Test + public void test() { + List dos = userMapper.selectListDeep(new QueryWrapper<>()); + System.out.println(1); + } +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/config/MybatisPlusConfig.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/config/MybatisPlusConfig.java new file mode 100644 index 0000000..79685b5 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/config/MybatisPlusConfig.java @@ -0,0 +1,24 @@ +package com.github.yulichang.test.mapping.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配置 + */ +@Configuration +public class MybatisPlusConfig { + + /** + * 分页插件 + */ + @Bean + public MybatisPlusInterceptor paginationInterceptor() { + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); + PaginationInnerInterceptor page = new PaginationInnerInterceptor(); + interceptor.addInnerInterceptor(page); + return interceptor; + } +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/entity/AddressDO.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/entity/AddressDO.java new file mode 100644 index 0000000..4bfd471 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/entity/AddressDO.java @@ -0,0 +1,31 @@ +package com.github.yulichang.test.mapping.entity; + +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableLogic; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import lombok.experimental.Accessors; + +@Data +@ToString +@Accessors(chain = true) +@EqualsAndHashCode +@TableName("address") +public class AddressDO { + + @TableId + private Integer id; + + private Integer userId; + + private Integer areaId; + + private String tel; + + private String address; + + @TableLogic + private Boolean del; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/entity/AreaDO.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/entity/AreaDO.java new file mode 100644 index 0000000..fd1840f --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/entity/AreaDO.java @@ -0,0 +1,31 @@ +package com.github.yulichang.test.mapping.entity; + +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableLogic; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import lombok.experimental.Accessors; + +@Data +@ToString +@Accessors(chain = true) +@EqualsAndHashCode +@TableName("area") +public class AreaDO { + + @TableId + private Integer id; + + private String province; + + private String city; + + private String area; + + private String postcode; + + @TableLogic + private Boolean del; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/entity/UserDO.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/entity/UserDO.java new file mode 100644 index 0000000..935a406 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/entity/UserDO.java @@ -0,0 +1,49 @@ +package com.github.yulichang.test.mapping.entity; + +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableLogic; +import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler; +import com.github.yulichang.annotation.EntityMapping; +import com.github.yulichang.test.mapping.enums.Sex; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import lombok.experimental.Accessors; + +import java.util.List; +import java.util.Map; + +@Data +@ToString +@Accessors(chain = true) +@EqualsAndHashCode +@TableName(value = "`user`", autoResultMap = true) +public class UserDO { + + @TableId + private Integer id; + + private Integer pid; + + @TableField(value = "`name`", typeHandler = JacksonTypeHandler.class) + private Map name; + + private Sex sex; + + private String headImg; + + private Integer addressId; + + @TableLogic + private Boolean del; + + @TableField(exist = false) + @EntityMapping(thisField = "pid", joinField = "id") + private UserDO pUser; + + @TableField(exist = false) + @EntityMapping(thisField = "id", joinField = "pid") + private List userList; +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/enums/Sex.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/enums/Sex.java new file mode 100644 index 0000000..64c1b59 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/enums/Sex.java @@ -0,0 +1,24 @@ +package com.github.yulichang.test.mapping.enums; + +import com.baomidou.mybatisplus.annotation.EnumValue; +import lombok.Getter; +import lombok.ToString; + +@Getter +@ToString +public enum Sex { + + MAN(0, "男"), + + WOMAN(1, "女"); + + @EnumValue + private final int code; + private final String des; + + Sex(int code, String des) { + this.code = code; + this.des = des; + } + +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/mapper/AddressMapper.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/mapper/AddressMapper.java new file mode 100644 index 0000000..a190b48 --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/mapper/AddressMapper.java @@ -0,0 +1,10 @@ +package com.github.yulichang.test.mapping.mapper; + +import com.github.yulichang.base.MPJBaseMapper; +import com.github.yulichang.test.mapping.entity.AddressDO; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +@SuppressWarnings("unused") +public interface AddressMapper extends MPJBaseMapper { +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/mapper/AreaMapper.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/mapper/AreaMapper.java new file mode 100644 index 0000000..56a9e0f --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/mapper/AreaMapper.java @@ -0,0 +1,10 @@ +package com.github.yulichang.test.mapping.mapper; + +import com.github.yulichang.base.MPJBaseMapper; +import com.github.yulichang.test.mapping.entity.AreaDO; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +@SuppressWarnings("unused") +public interface AreaMapper extends MPJBaseMapper { +} diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/mapper/UserMapper.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/mapper/UserMapper.java new file mode 100644 index 0000000..e13c26a --- /dev/null +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/mapping/mapper/UserMapper.java @@ -0,0 +1,10 @@ +package com.github.yulichang.test.mapping.mapper; + +import com.github.yulichang.base.MPJBaseMapper; +import com.github.yulichang.test.mapping.entity.UserDO; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface UserMapper extends MPJBaseMapper { + +} diff --git a/mybatis-plus-join-test/src/test/resources/application-collection.yml b/mybatis-plus-join-test/src/test/resources/application-collection.yml new file mode 100644 index 0000000..00f280f --- /dev/null +++ b/mybatis-plus-join-test/src/test/resources/application-collection.yml @@ -0,0 +1,5 @@ +spring: + sql: + init: + schema-locations: classpath:db/collection/schema.sql + data-locations: classpath:db/collection/data.sql \ No newline at end of file diff --git a/mybatis-plus-join-test/src/test/resources/application-join.yml b/mybatis-plus-join-test/src/test/resources/application-join.yml new file mode 100644 index 0000000..5f17720 --- /dev/null +++ b/mybatis-plus-join-test/src/test/resources/application-join.yml @@ -0,0 +1,5 @@ +spring: + sql: + init: + schema-locations: classpath:db/join/schema.sql + data-locations: classpath:db/join/data.sql \ No newline at end of file diff --git a/mybatis-plus-join-test/src/test/resources/application-mapping.yml b/mybatis-plus-join-test/src/test/resources/application-mapping.yml new file mode 100644 index 0000000..083aef4 --- /dev/null +++ b/mybatis-plus-join-test/src/test/resources/application-mapping.yml @@ -0,0 +1,5 @@ +spring: + sql: + init: + schema-locations: classpath:db/mapping/schema.sql + data-locations: classpath:db/mapping/data.sql \ No newline at end of file diff --git a/mybatis-plus-join-test/src/test/resources/application.yml b/mybatis-plus-join-test/src/test/resources/application.yml new file mode 100644 index 0000000..21a04ba --- /dev/null +++ b/mybatis-plus-join-test/src/test/resources/application.yml @@ -0,0 +1,22 @@ +spring: + datasource: + driver-class-name: org.h2.Driver + url: jdbc:h2:mem:test + username: root + password: test + +mybatis-plus: + typeEnumsPackage: com.github.yulichang.test + global-config: + db-config: + logic-delete-field: del + logic-delete-value: true + logic-not-delete-value: false + banner: true + configuration: + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl + +# 打印 mybatis plus join banner +mybatis-plus-join: + banner: true + sub-table-logic: true \ No newline at end of file diff --git a/mybatis-plus-join-test/src/test/resources/db/collection/data.sql b/mybatis-plus-join-test/src/test/resources/db/collection/data.sql new file mode 100644 index 0000000..8e9b4c1 --- /dev/null +++ b/mybatis-plus-join-test/src/test/resources/db/collection/data.sql @@ -0,0 +1,29 @@ +-- noinspection SqlDialectInspectionForFile + +-- noinspection SqlNoDataSourceInspectionForFile + +DELETE FROM table_a; +insert into table_a (id, `name`) values (1, 'tableA1'); +insert into table_a (id, `name`) values (2, 'tableA2'); +insert into table_a (id, `name`) values (3, 'tableA3'); + +DELETE FROM table_b; +insert into table_b (id, aid, `name`) values (1, 1, 'tableB1'); +insert into table_b (id, aid, `name`) values (2, 1, 'tableB2'); +insert into table_b (id, aid, `name`) values (3, 2, 'tableB3'); + +DELETE FROM table_c; +insert into table_c (id, bid, `name`) values (1, 1, 'tableC1'); +insert into table_c (id, bid, `name`) values (2, 1, 'tableC2'); +insert into table_c (id, bid, `name`) values (3, 2, 'tableC3'); + +DELETE FROM table_d; +insert into table_d (id, cid, `name`) values (1, 1, 'tableD1'); +insert into table_d (id, cid, `name`) values (2, 1, 'tableD2'); +insert into table_d (id, cid, `name`) values (3, 2, 'tableD3'); + +DELETE FROM table_e; +insert into table_e (id, did, `name`) values (1, 1, 'tableE1'); +insert into table_e (id, did, `name`) values (2, 1, 'tableE2'); +insert into table_e (id, did, `name`) values (3, 2, 'tableE3'); + diff --git a/mybatis-plus-join-test/src/test/resources/db/collection/schema.sql b/mybatis-plus-join-test/src/test/resources/db/collection/schema.sql new file mode 100644 index 0000000..d8a1fc8 --- /dev/null +++ b/mybatis-plus-join-test/src/test/resources/db/collection/schema.sql @@ -0,0 +1,48 @@ +-- noinspection SqlDialectInspectionForFile + +-- noinspection SqlNoDataSourceInspectionForFile + + +DROP TABLE IF EXISTS table_a; +create table table_a +( + id int auto_increment + primary key, + `name` varchar(255) null +); + +DROP TABLE IF EXISTS table_b; +create table table_b +( + id int auto_increment + primary key, + `aid` int not null, + `name` varchar(255) null +); + +DROP TABLE IF EXISTS table_c; +create table table_c +( + id int auto_increment + primary key, + `bid` int not null, + `name` varchar(255) null +); + +DROP TABLE IF EXISTS table_d; +create table table_d +( + id int auto_increment + primary key, + `cid` int not null, + `name` varchar(255) null +); + +DROP TABLE IF EXISTS table_e; +create table table_e +( + id int auto_increment + primary key, + `did` int not null, + `name` varchar(255) null +); \ No newline at end of file diff --git a/mybatis-plus-join-test/src/test/resources/db/join/data.sql b/mybatis-plus-join-test/src/test/resources/db/join/data.sql new file mode 100644 index 0000000..6bd397b --- /dev/null +++ b/mybatis-plus-join-test/src/test/resources/db/join/data.sql @@ -0,0 +1,80 @@ +-- noinspection SqlDialectInspectionForFile + +-- noinspection SqlNoDataSourceInspectionForFile + +DELETE FROM area; + +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10001, '北京市01', '北京01', '朝阳01', '80001', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10002, '北京市02', '北京02', '朝阳02', '80002', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10003, '北京市03', '北京03', '朝阳03', '80003', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10004, '北京市04', '北京04', '朝阳04', '80004', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10005, '北京市05', '北京05', '朝阳05', '80005', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10006, '北京市06', '北京06', '朝阳06', '80006', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10007, '北京市07', '北京07', '朝阳07', '80007', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10008, '北京市08', '北京08', '朝阳08', '80008', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10009, '北京市09', '北京09', '朝阳09', '80009', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10010, '北京市10', '北京10', '朝阳10', '80010', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10011, '北京市11', '北京11', '朝阳11', '80011', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10012, '北京市12', '北京12', '朝阳12', '80012', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10013, '北京市13', '北京13', '朝阳13', '80013', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10014, '北京市14', '北京14', '朝阳14', '80014', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10015, '北京市15', '北京15', '朝阳15', '80015', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10016, '北京市16', '北京16', '朝阳16', '80016', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10017, '北京市17', '北京17', '朝阳17', '80017', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10018, '北京市18', '北京18', '朝阳18', '80018', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10019, '北京市19', '北京19', '朝阳19', '80019', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10020, '北京市20', '北京20', '朝阳20', '80020', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10021, '北京市21', '北京21', '朝阳21', '80021', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10022, '北京市22', '北京22', '朝阳22', '80022', false); + +DELETE FROM `user`; + +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 1, '{"aa":"aaa","bb":"bbb"}', 1, 1, 'https://url-01', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 2, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-02', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 3, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-03', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 4, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-04', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 5, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-05', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 6, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-06', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 7, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-07', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 8, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-08', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 9, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-09', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (10, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-10', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (11, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-11', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (12, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-12', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (13, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-13', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (14, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-14', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (15, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-15', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (16, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-16', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (17, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-17', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (18, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-18', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (19, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-19', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (20, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-20', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (21, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-21', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (22, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-22', false); + + +DELETE FROM address; + +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 1, 1, 10001, '10000000001', '曹县01', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 2, 1, 10002, '10000000002', '曹县02', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 3, 1, 10003, '10000000003', '曹县03', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 4, 1, 10004, '10000000004', '曹县04', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 5, 1, 10005, '10000000005', '曹县05', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 6, 1, 10006, '10000000006', '曹县06', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 7, 1, 10007, '10000000007', '曹县07', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 8, 1, 10008, '10000000008', '曹县08', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 9, 1, 10009, '10000000009', '曹县09', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (10,10, 10010, '10000000010', '曹县10', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (11,11, 10011, '10000000011', '曹县11', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (12,12, 10012, '10000000012', '曹县12', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (13,13, 10013, '10000000013', '曹县13', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (14,14, 10014, '10000000014', '曹县14', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (15,15, 10015, '10000000015', '曹县15', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (16,16, 10016, '10000000016', '曹县16', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (17,17, 10017, '10000000017', '曹县17', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (18,18, 10018, '10000000018', '曹县18', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (19,19, 10019, '10000000019', '曹县19', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (20,20, 10020, '10000000020', '曹县20', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (21,21, 10021, '10000000021', '曹县21', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (22,22, 10022, '10000000022', '曹县22', false); + diff --git a/mybatis-plus-join-test/src/test/resources/db/join/schema.sql b/mybatis-plus-join-test/src/test/resources/db/join/schema.sql new file mode 100644 index 0000000..f728079 --- /dev/null +++ b/mybatis-plus-join-test/src/test/resources/db/join/schema.sql @@ -0,0 +1,42 @@ +-- noinspection SqlDialectInspectionForFile + +-- noinspection SqlNoDataSourceInspectionForFile + +DROP TABLE IF EXISTS area; + +create table area +( + id int auto_increment + primary key, + province varchar(255) null, + city varchar(255) null, + area varchar(255) null, + postcode varchar(255) null, + del bit +); + +DROP TABLE IF EXISTS `user`; + +create table `user` +( + id int auto_increment + primary key, + `name` varchar(255) not null, + `address_id` int not null, + sex tinyint not null, + head_img varchar(255) not null, + del bit +); + +DROP TABLE IF EXISTS address; + +create table address +( + id int auto_increment + primary key, + user_id int null, + area_id int null, + tel varchar(255) null, + address varchar(255) null, + del bit +); \ No newline at end of file diff --git a/mybatis-plus-join-test/src/test/resources/db/mapping/data.sql b/mybatis-plus-join-test/src/test/resources/db/mapping/data.sql new file mode 100644 index 0000000..5b76f9a --- /dev/null +++ b/mybatis-plus-join-test/src/test/resources/db/mapping/data.sql @@ -0,0 +1,79 @@ +-- noinspection SqlDialectInspectionForFile + +-- noinspection SqlNoDataSourceInspectionForFile + +DELETE FROM area; + +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10001, '北京市01', '北京01', '朝阳01', '80001', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10002, '北京市02', '北京02', '朝阳02', '80002', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10003, '北京市03', '北京03', '朝阳03', '80003', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10004, '北京市04', '北京04', '朝阳04', '80004', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10005, '北京市05', '北京05', '朝阳05', '80005', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10006, '北京市06', '北京06', '朝阳06', '80006', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10007, '北京市07', '北京07', '朝阳07', '80007', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10008, '北京市08', '北京08', '朝阳08', '80008', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10009, '北京市09', '北京09', '朝阳09', '80009', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10010, '北京市10', '北京10', '朝阳10', '80010', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10011, '北京市11', '北京11', '朝阳11', '80011', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10012, '北京市12', '北京12', '朝阳12', '80012', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10013, '北京市13', '北京13', '朝阳13', '80013', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10014, '北京市14', '北京14', '朝阳14', '80014', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10015, '北京市15', '北京15', '朝阳15', '80015', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10016, '北京市16', '北京16', '朝阳16', '80016', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10017, '北京市17', '北京17', '朝阳17', '80017', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10018, '北京市18', '北京18', '朝阳18', '80018', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10019, '北京市19', '北京19', '朝阳19', '80019', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10020, '北京市20', '北京20', '朝阳20', '80020', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10021, '北京市21', '北京21', '朝阳21', '80021', false); +INSERT INTO area (id, province, city, area, postcode, del) VALUES (10022, '北京市22', '北京22', '朝阳22', '80022', false); + +DELETE FROM `user`; + +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 1, 1, '{"aa":"aaa","bb":"bbb"}', 1, 1, 'https://url-01', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 2, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-02', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 3, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-03', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 4, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-04', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 5, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-05', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 6, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-06', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 7, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-07', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 8, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-08', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 9, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-09', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (10, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-10', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (11, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-11', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (12, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-12', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (13, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-13', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (14, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-14', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (15, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-15', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (16, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-16', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (17, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-17', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (18, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-18', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (19, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-19', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (20, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-20', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (21, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-21', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (22, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-22', false); + + +DELETE FROM address; + +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 1, 1, 10001, '10000000001', '曹县01', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 2, 1, 10002, '10000000002', '曹县02', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 3, 1, 10003, '10000000003', '曹县03', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 4, 1, 10004, '10000000004', '曹县04', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 5, 1, 10005, '10000000005', '曹县05', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 6, 1, 10006, '10000000006', '曹县06', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 7, 1, 10007, '10000000007', '曹县07', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 8, 1, 10008, '10000000008', '曹县08', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 9, 1, 10009, '10000000009', '曹县09', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (10,10, 10010, '10000000010', '曹县10', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (11,11, 10011, '10000000011', '曹县11', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (12,12, 10012, '10000000012', '曹县12', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (13,13, 10013, '10000000013', '曹县13', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (14,14, 10014, '10000000014', '曹县14', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (15,15, 10015, '10000000015', '曹县15', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (16,16, 10016, '10000000016', '曹县16', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (17,17, 10017, '10000000017', '曹县17', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (18,18, 10018, '10000000018', '曹县18', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (19,19, 10019, '10000000019', '曹县19', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (20,20, 10020, '10000000020', '曹县20', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (21,21, 10021, '10000000021', '曹县21', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (22,22, 10022, '10000000022', '曹县22', false); \ No newline at end of file diff --git a/mybatis-plus-join-test/src/test/resources/db/mapping/schema.sql b/mybatis-plus-join-test/src/test/resources/db/mapping/schema.sql new file mode 100644 index 0000000..f077cd5 --- /dev/null +++ b/mybatis-plus-join-test/src/test/resources/db/mapping/schema.sql @@ -0,0 +1,43 @@ +-- noinspection SqlDialectInspectionForFile + +-- noinspection SqlNoDataSourceInspectionForFile + +DROP TABLE IF EXISTS area; + +create table area +( + id int auto_increment + primary key, + province varchar(255) null, + city varchar(255) null, + area varchar(255) null, + postcode varchar(255) null, + del bit +); + +DROP TABLE IF EXISTS `user`; + +create table `user` +( + id int auto_increment + primary key, + pid int null, + `name` varchar(255) not null, + `address_id` int not null, + sex tinyint not null, + head_img varchar(255) not null, + del bit +); + +DROP TABLE IF EXISTS address; + +create table address +( + id int auto_increment + primary key, + user_id int null, + area_id int null, + tel varchar(255) null, + address varchar(255) null, + del bit +); \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8ae761c..c0b5e32 100644 --- a/pom.xml +++ b/pom.xml @@ -3,9 +3,11 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.github.yulichang - mybatis-plus-join - 1.3.5 - mybatis-plus-join + mybatis-plus-join-root + pom + 1.3.8 + mybatis-plus-join-root + An enhanced toolkit of Mybatis-Plus to simplify development. https://github.com/yulichang/mybatis-plus-join @@ -26,30 +28,24 @@ scm:git:https://github.com/yulichang/mybatis-plus-join.git https://github.com/yulichang/mybatis-plus-join + + + mybatis-plus-join-boot-starter + mybatis-plus-join-core + mybatis-plus-join-annotation + mybatis-plus-join + + - UTF-8 - github 1.8 1.8 - 1.8 1.8 + github + UTF-8 - - - com.baomidou - mybatis-plus-boot-starter - 3.5.2 - provided - - - org.projectlombok - lombok - 1.18.24 - provided - diff --git a/src/main/java/com/github/yulichang/toolkit/SpringContentUtils.java b/src/main/java/com/github/yulichang/toolkit/SpringContentUtils.java deleted file mode 100644 index a66a235..0000000 --- a/src/main/java/com/github/yulichang/toolkit/SpringContentUtils.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.github.yulichang.toolkit; - -import org.springframework.beans.BeansException; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; - -/** - * spring容器工具类 - * - * @author yulichang - * @since 1.2.0 - */ -public class SpringContentUtils implements ApplicationContextAware { - - private static ApplicationContext context; - - @Override - @SuppressWarnings("NullableProblems") - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - context = applicationContext; - } - - public static ApplicationContext getApplicationContext() { - return context; - } -} diff --git a/src/main/java/com/github/yulichang/toolkit/support/SerializedLambda.java b/src/main/java/com/github/yulichang/toolkit/support/SerializedLambda.java deleted file mode 100644 index c1d5195..0000000 --- a/src/main/java/com/github/yulichang/toolkit/support/SerializedLambda.java +++ /dev/null @@ -1,123 +0,0 @@ -package com.github.yulichang.toolkit.support; - -import com.baomidou.mybatisplus.core.toolkit.ClassUtils; -import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils; -import com.baomidou.mybatisplus.core.toolkit.SerializationUtils; -import com.baomidou.mybatisplus.core.toolkit.support.SFunction; - -import java.io.*; - -/** - * copy mp before 3.4.3 - */ -@SuppressWarnings("unused") -public class SerializedLambda implements Serializable { - - private static final long serialVersionUID = 8025925345765570181L; - - private Class capturingClass; - private String functionalInterfaceClass; - private String functionalInterfaceMethodName; - private String functionalInterfaceMethodSignature; - private String implClass; - private String implMethodName; - private String implMethodSignature; - private int implMethodKind; - private String instantiatedMethodType; - private Object[] capturedArgs; - - /** - * 通过反序列化转换 lambda 表达式,该方法只能序列化 lambda 表达式,不能序列化接口实现或者正常非 lambda 写法的对象 - * - * @param lambda lambda对象 - * @return 返回解析后的 SerializedLambda - */ - public static SerializedLambda resolve(SFunction lambda) { - if (!lambda.getClass().isSynthetic()) { - throw ExceptionUtils.mpe("该方法仅能传入 lambda 表达式产生的合成类"); - } - try (ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(SerializationUtils.serialize(lambda))) { - @Override - protected Class resolveClass(ObjectStreamClass objectStreamClass) throws IOException, ClassNotFoundException { - Class clazz; - try { - clazz = ClassUtils.toClassConfident(objectStreamClass.getName()); - } catch (Exception ex) { - clazz = super.resolveClass(objectStreamClass); - } - return clazz == java.lang.invoke.SerializedLambda.class ? SerializedLambda.class : clazz; - } - }) { - return (SerializedLambda) objIn.readObject(); - } catch (ClassNotFoundException | IOException e) { - throw ExceptionUtils.mpe("This is impossible to happen", e); - } - } - - /** - * 获取接口 class - * - * @return 返回 class 名称 - */ - public String getFunctionalInterfaceClassName() { - return normalizedName(functionalInterfaceClass); - } - - /** - * 获取实现的 class - * - * @return 实现类 - */ - public Class getImplClass() { - return ClassUtils.toClassConfident(getImplClassName()); - } - - /** - * 获取 class 的名称 - * - * @return 类名 - */ - public String getImplClassName() { - return normalizedName(implClass); - } - - /** - * 获取实现者的方法名称 - * - * @return 方法名称 - */ - public String getImplMethodName() { - return implMethodName; - } - - /** - * 正常化类名称,将类名称中的 / 替换为 . - * - * @param name 名称 - * @return 正常的类名 - */ - private String normalizedName(String name) { - return name.replace('/', '.'); - } - - /** - * @return 获取实例化方法的类型 - */ - public Class getInstantiatedType() { - String instantiatedTypeName = normalizedName(instantiatedMethodType.substring(2, instantiatedMethodType.indexOf(';'))); - return ClassUtils.toClassConfident(instantiatedTypeName); - } - - /** - * @return 字符串形式 - */ - @Override - public String toString() { - String interfaceName = getFunctionalInterfaceClassName(); - String implName = getImplClassName(); - return String.format("%s -> %s::%s", - interfaceName.substring(interfaceName.lastIndexOf('.') + 1), - implName.substring(implName.lastIndexOf('.') + 1), - implMethodName); - } -} diff --git a/src/main/java/com/github/yulichang/wrapper/interfaces/LambdaJoin.java b/src/main/java/com/github/yulichang/wrapper/interfaces/LambdaJoin.java deleted file mode 100644 index b15397f..0000000 --- a/src/main/java/com/github/yulichang/wrapper/interfaces/LambdaJoin.java +++ /dev/null @@ -1,155 +0,0 @@ -package com.github.yulichang.wrapper.interfaces; - -import com.baomidou.mybatisplus.core.toolkit.support.SFunction; -import com.github.yulichang.interfaces.MPJBaseJoin; -import com.github.yulichang.toolkit.Constant; -import com.github.yulichang.wrapper.interfaces.on.OnFunction; - -/** - * @author yulichang - */ -@SuppressWarnings("unused") -public interface LambdaJoin extends MPJBaseJoin { - - /** - * left join - * - * @param clazz 关联的实体类 - * @param left 条件 - * @param right 条件 - */ - default Children leftJoin(Class clazz, SFunction left, SFunction right) { - return leftJoin(true, clazz, left, right); - } - - /** - * left join - *

- * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) - * - * @param clazz 关联的实体类 - * @param function 条件 - */ - default Children leftJoin(Class clazz, OnFunction function) { - return leftJoin(true, clazz, function); - } - - /** - * left join - * - * @param condition 是否执行 - * @param clazz 关联的实体类 - * @param left 条件 - * @param right 条件 - */ - default Children leftJoin(boolean condition, Class clazz, SFunction left, SFunction right) { - return leftJoin(condition, clazz, on -> on.eq(left, right)); - } - - /** - * left join - *

- * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) - * - * @param condition 是否执行 - * @param clazz 关联实体类 - * @param function 条件 - */ - default Children leftJoin(boolean condition, Class clazz, OnFunction function) { - return join(Constant.LEFT_JOIN, condition, clazz, function); - } - - /** - * ignore 参考 left join - */ - default Children rightJoin(Class clazz, SFunction left, SFunction right) { - return rightJoin(true, clazz, left, right); - } - - /** - * ignore 参考 left join - */ - default Children rightJoin(Class clazz, OnFunction function) { - return rightJoin(true, clazz, function); - } - - /** - * ignore 参考 left join - */ - default Children rightJoin(boolean condition, Class clazz, SFunction left, SFunction right) { - return rightJoin(condition, clazz, on -> on.eq(left, right)); - } - - /** - * ignore 参考 left join - */ - default Children rightJoin(boolean condition, Class clazz, OnFunction function) { - return join(Constant.RIGHT_JOIN, condition, clazz, function); - } - - /** - * ignore 参考 left join - */ - default Children innerJoin(Class clazz, SFunction left, SFunction right) { - return innerJoin(true, clazz, left, right); - } - - /** - * ignore 参考 left join - */ - default Children innerJoin(Class clazz, OnFunction function) { - return innerJoin(true, clazz, function); - } - - /** - * ignore 参考 left join - */ - default Children innerJoin(boolean condition, Class clazz, SFunction left, SFunction right) { - return innerJoin(condition, clazz, on -> on.eq(left, right)); - } - - /** - * ignore 参考 left join - */ - default Children innerJoin(boolean condition, Class clazz, OnFunction function) { - return join(Constant.INNER_JOIN, condition, clazz, function); - } - - /** - * ignore 参考 left join - */ - default Children fullJoin(Class clazz, SFunction left, SFunction right) { - return fullJoin(true, clazz, left, right); - } - - /** - * ignore 参考 left join - */ - default Children fullJoin(Class clazz, OnFunction function) { - return fullJoin(true, clazz, function); - } - - /** - * ignore 参考 left join - */ - default Children fullJoin(boolean condition, Class clazz, SFunction left, SFunction right) { - return fullJoin(condition, clazz, on -> on.eq(left, right)); - } - - /** - * ignore 参考 left join - */ - default Children fullJoin(boolean condition, Class clazz, OnFunction function) { - return join(Constant.FULL_JOIN, condition, clazz, function); - } - - /** - * 查询基类 可以直接调用此方法实现以上所有功能 - * - * @param keyWord 连表关键字 - * @param condition 是否执行 - * @param clazz 连表实体类 - * @param function 关联条件 - */ - Children join(String keyWord, boolean condition, Class clazz, OnFunction function); -} diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories deleted file mode 100644 index 327caa0..0000000 --- a/src/main/resources/META-INF/spring.factories +++ /dev/null @@ -1,6 +0,0 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - com.github.yulichang.interceptor.MPJInterceptor,\ - com.github.yulichang.injector.MPJSqlInjector,\ - com.github.yulichang.config.InterceptorConfig,\ - com.github.yulichang.config.MappingConfig,\ - com.github.yulichang.toolkit.SpringContentUtils