diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinCount.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinCount.java index b8c5baaa45aae1dbd8e18b25ed7f9c3e8fc43b58..8dddc017cba4462cb2c41a87a33e37dc477958e5 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinCount.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinCount.java @@ -1,6 +1,10 @@ package com.github.yulichang.method; import com.baomidou.mybatisplus.core.metadata.TableInfo; +import com.baomidou.mybatisplus.core.toolkit.Constants; +import com.baomidou.mybatisplus.core.toolkit.StringPool; +import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; +import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlSource; @@ -25,9 +29,15 @@ public class SelectJoinCount extends MPJAbstractMethod { @Override public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) { SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_COUNT; - String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlCount(), + String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true), mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment()); SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass); return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Long.class); } + + @Override + protected String sqlComment() { + return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)", + Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), true); + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SqlMethod.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SqlMethod.java index 26dc29b467da014441731193364e0194d9dd8072..39da281ad69c7703dddba6cec1ad0d7c725f9b38 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SqlMethod.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SqlMethod.java @@ -14,7 +14,7 @@ public enum SqlMethod { * 连表查询 */ SELECT_JOIN_COUNT("selectJoinCount", "查询满足条件总记录数", - ""), + ""), SELECT_JOIN_ONE("selectJoinOne", "返回一条记录", ""), diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/T2.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/T2.java new file mode 100644 index 0000000000000000000000000000000000000000..f1c5a313d83602d6ecf6fb731fd74ff2356b335f --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/T2.java @@ -0,0 +1,12 @@ +package com.github.yulichang.test.join.entity; + +import lombok.Data; + +/** + * date: 2024/7/19 + */ +@Data +public class T2 { + private String column_1; + private String column_2; +} diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/User.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/User.java new file mode 100644 index 0000000000000000000000000000000000000000..160d411c7f0e21fac1e088f273d690e02642a572 --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/User.java @@ -0,0 +1,33 @@ +package com.github.yulichang.test.join.entity; + +import com.baomidou.mybatisplus.annotation.TableField; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.SuperBuilder; + +/** + * @Author Andy + * @Date 2020/05/01 13:38 + * @Desc + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@SuperBuilder +public class User { + private Long id; + private String name; + private int age; + private String email; + + public User(Long id, String name, int age, String email) { + this.id = id; + this.name = name; + this.age = age; + this.email = email; + } + + @TableField(exist = false) + private String email2; +} diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/T2Mapper.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/T2Mapper.java new file mode 100644 index 0000000000000000000000000000000000000000..aad44a49fc6c416b97ada9c0991efef2d4310cd0 --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/T2Mapper.java @@ -0,0 +1,11 @@ +package com.github.yulichang.test.join.mapper; + +import com.github.yulichang.test.join.entity.T2; +import org.apache.ibatis.annotations.Mapper; + +/** + * date: 2024/7/19 + */ +@Mapper +public interface T2Mapper extends MyBaseMapper{ +} diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/TUserMapper.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/TUserMapper.java new file mode 100644 index 0000000000000000000000000000000000000000..d171b3e1b660464b262c0325be0b28fa63052510 --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/TUserMapper.java @@ -0,0 +1,11 @@ +package com.github.yulichang.test.join.mapper; + +import com.github.yulichang.test.join.entity.User; +import org.apache.ibatis.annotations.Mapper; + +/** + * date: 2024/7/19 + */ +@Mapper +public interface TUserMapper extends MyBaseMapper{ +} diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/mysql/SelectJoinCountTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/mysql/SelectJoinCountTest.java new file mode 100644 index 0000000000000000000000000000000000000000..ce9ac7e3a62cf0c332a655c607086abde5f2e4bf --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/mysql/SelectJoinCountTest.java @@ -0,0 +1,42 @@ +package com.github.yulichang.test.join.mysql; + +import com.github.yulichang.test.join.entity.T2; +import com.github.yulichang.test.join.entity.User; +import com.github.yulichang.test.join.mapper.TUserMapper; +import com.github.yulichang.toolkit.JoinWrappers; +import com.github.yulichang.wrapper.MPJLambdaWrapper; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +/** + * date: 2024/7/19 + */ +@SpringBootTest +public class SelectJoinCountTest { + + @Autowired + private TUserMapper tUserMapper; + + + + @Test + void 测试selectJoinCount_union表() { + MPJLambdaWrapper wrapper = JoinWrappers.lambda(User.class); + wrapper.select(User::getId); + + MPJLambdaWrapper wrapper1 = JoinWrappers.lambda(T2.class); + wrapper1.selectAs(T2::getColumn_1, User::getId); + wrapper.union(wrapper1); + System.out.println(tUserMapper.selectJoinCount(wrapper)); + } + + @Test + void 测试selectJoinCount_单表() { + MPJLambdaWrapper wrapper = JoinWrappers.lambda(User.class); + wrapper.select(User::getId); + + System.out.println(tUserMapper.selectJoinCount(wrapper)); + } + +}