mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
1.0.9
This commit is contained in:
parent
782352a01e
commit
fc9ae1a143
@ -1,4 +1,4 @@
|
||||
package com.github.yulichang.common.support;
|
||||
package com.github.yulichang.common.support.alisa;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
@ -1,4 +1,4 @@
|
||||
package com.github.yulichang.common.support;
|
||||
package com.github.yulichang.common.support.alisa;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
@ -22,6 +22,11 @@ public class AliasQueryWrapper<T> extends QueryWrapper<T> {
|
||||
|
||||
/**
|
||||
* 重写字段序列化方法
|
||||
* 可以自定义过滤策略可以是空格或其他(以下方法是只为查询字段没有带点 . 的加别名,带点的则不会加)
|
||||
* setAlias("u")
|
||||
* <p>
|
||||
* .eq("id") --> u.id
|
||||
* .eq("ee.id") --> ee.id
|
||||
*/
|
||||
@Override
|
||||
protected String columnToString(String column) {
|
@ -0,0 +1,50 @@
|
||||
package com.github.yulichang.common.support.func;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda;
|
||||
import org.apache.ibatis.reflection.property.PropertyNamer;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 使用表全名,不适用别名
|
||||
* UserDO::getId --> user.id
|
||||
* UserAddrDO::getTel --> user_addr.tel
|
||||
*
|
||||
* @author yulichang
|
||||
*/
|
||||
public class F {
|
||||
|
||||
public static <T, R> String s(SFunction<T, R> sFunction) {
|
||||
Assert.notNull(sFunction, "function is null");
|
||||
return TableInfoHelper.getTableInfo(getEntityClass(sFunction)).getTableName()
|
||||
+ StringPool.DOT + getColumn(sFunction);
|
||||
}
|
||||
|
||||
/**
|
||||
* 与S的getColumn一致,连个都用,保留一个就行了
|
||||
*/
|
||||
public static <T> String getColumn(SFunction<T, ?> fn) {
|
||||
SerializedLambda lambda = LambdaUtils.resolve(fn);
|
||||
String fieldName = PropertyNamer.methodToProperty(lambda.getImplMethodName());
|
||||
try {
|
||||
TableField annotation = lambda.getImplClass().getDeclaredField(fieldName).getAnnotation(TableField.class);
|
||||
if (Objects.nonNull(annotation) && StringUtils.isNotBlank(annotation.value())) {
|
||||
return annotation.value();
|
||||
}
|
||||
} catch (NoSuchFieldException ignored) {
|
||||
}
|
||||
return StringUtils.camelToUnderline(fieldName);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> Class<T> getEntityClass(SFunction<T, ?> fn) {
|
||||
return (Class<T>) com.baomidou.mybatisplus.core.toolkit.LambdaUtils.resolve(fn).getInstantiatedType();
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package com.github.yulichang.common.support.func;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda;
|
||||
import org.apache.ibatis.reflection.property.PropertyNamer;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 预定义以下别名 可以自行设置别名
|
||||
* S.a(UserDO::getId) --> a.id
|
||||
* S.b(UserDO::getId) --> b.id
|
||||
* S.c(UserDO::getId) --> c.id
|
||||
* S.d(UserDO::getId) --> d.id
|
||||
* S.e(UserDO::getId) --> e.id
|
||||
* S.f(UserDO::getId) --> f.id
|
||||
*
|
||||
* @author yulichang
|
||||
*/
|
||||
public class S {
|
||||
|
||||
public static <T, R> String a(SFunction<T, R> sFunction) {
|
||||
Assert.notNull(sFunction, "function is null");
|
||||
return "a" + StringPool.DOT + getColumn(sFunction);
|
||||
}
|
||||
|
||||
public static <T, R> String b(SFunction<T, R> sFunction) {
|
||||
Assert.notNull(sFunction, "function is null");
|
||||
return "b" + StringPool.DOT + getColumn(sFunction);
|
||||
}
|
||||
|
||||
public static <T, R> String c(SFunction<T, R> sFunction) {
|
||||
Assert.notNull(sFunction, "function is null");
|
||||
return "c" + StringPool.DOT + getColumn(sFunction);
|
||||
}
|
||||
|
||||
public static <T, R> String d(SFunction<T, R> sFunction) {
|
||||
Assert.notNull(sFunction, "function is null");
|
||||
return "d" + StringPool.DOT + getColumn(sFunction);
|
||||
}
|
||||
|
||||
public static <T, R> String e(SFunction<T, R> sFunction) {
|
||||
Assert.notNull(sFunction, "function is null");
|
||||
return "e" + StringPool.DOT + getColumn(sFunction);
|
||||
}
|
||||
|
||||
public static <T, R> String f(SFunction<T, R> sFunction) {
|
||||
Assert.notNull(sFunction, "function is null");
|
||||
return "f" + StringPool.DOT + getColumn(sFunction);
|
||||
}
|
||||
|
||||
/**
|
||||
* 与F的getColumn一致,连个都用,保留一个就行了
|
||||
*/
|
||||
public static <T> String getColumn(SFunction<T, ?> fn) {
|
||||
SerializedLambda lambda = LambdaUtils.resolve(fn);
|
||||
String fieldName = PropertyNamer.methodToProperty(lambda.getImplMethodName());
|
||||
try {
|
||||
TableField annotation = lambda.getImplClass().getDeclaredField(fieldName).getAnnotation(TableField.class);
|
||||
if (Objects.nonNull(annotation) && StringUtils.isNotBlank(annotation.value())) {
|
||||
return annotation.value();
|
||||
}
|
||||
} catch (NoSuchFieldException ignored) {
|
||||
}
|
||||
return StringUtils.camelToUnderline(fieldName);
|
||||
}
|
||||
}
|
@ -0,0 +1,134 @@
|
||||
## 支持lambda的QueryWrapper
|
||||
|
||||
让QueryWrapper也能使用lambda
|
||||
|
||||
### 使用方法
|
||||
|
||||
拷贝以下类即可
|
||||
[com.github.yulichang.common.support.func.F]()
|
||||
[com.github.yulichang.common.support.func.S]()
|
||||
|
||||
原理:
|
||||
F类以表名+列明的形式序列化 (@TableName与@TableField注解是有效的,会优先使用注解的值)
|
||||
|
||||
* F.s(UserDO::getId) --> user.id
|
||||
* F.s(UserAddrDO::getId) --> user_addr.id
|
||||
|
||||
S类以预定义表名+列明的形式序列化
|
||||
忽略表名,以预定义别名替代(@TableField注解是有效的,会优先使用注解的值)
|
||||
|
||||
* S.a(UserDO::getId) --> a.id
|
||||
* S.b(UserDO::getId) --> b.id
|
||||
* S.c(UserDO::getId) --> c.id
|
||||
* S.d(UserDO::getId) --> d.id
|
||||
* S.e(UserDO::getId) --> e.id
|
||||
* S.f(UserDO::getId) --> f.id
|
||||
|
||||
注解:
|
||||
|
||||
```java
|
||||
|
||||
@Mapper
|
||||
public interface UserMapper extends BaseMapper<UserDO> {
|
||||
/**
|
||||
* 使用 S
|
||||
*/
|
||||
@Select("select a.*,b.tel from user a " +
|
||||
"left join user_address b on a.id = b.user_id ${ew.customSqlSegment}")
|
||||
UserDTO userLeftJoin(@Param(Constants.WRAPPER) Wrapper<?> queryWrapper);
|
||||
|
||||
/**
|
||||
* 使用 F
|
||||
*/
|
||||
@Select("select user.*,user_address.tel from user " +
|
||||
"left join user_address on user.id = user_address.user_id ${ew.customSqlSegment}")
|
||||
UserDTO userLeftJoin(@Param(Constants.WRAPPER) Wrapper<?> queryWrapper);
|
||||
}
|
||||
```
|
||||
|
||||
或者xml
|
||||
|
||||
```
|
||||
<!-- 使用 S -->
|
||||
<select id="userLeftJoin" resultType="UserDTO">
|
||||
select
|
||||
a.*,
|
||||
b.tel
|
||||
from
|
||||
user a
|
||||
left join user_address b on a.id = b.user_id
|
||||
${ew.customSqlSegment}
|
||||
</select>
|
||||
|
||||
<!-- 使用 F -->
|
||||
<select id="userLeftJoin" resultType="UserDTO">
|
||||
select
|
||||
user.*,
|
||||
user_address.tel
|
||||
from
|
||||
user
|
||||
left join user_address on user.id = user_address.user_id
|
||||
${ew.customSqlSegment}
|
||||
</select>
|
||||
```
|
||||
|
||||
使用wrapper:
|
||||
|
||||
```java
|
||||
class MpJoinTest {
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
|
||||
/**
|
||||
* 使用S
|
||||
*/
|
||||
@Test
|
||||
void testS() {
|
||||
UserDTO dto = userMapper.userLeftJoin(new QueryWrapper<UserDO>()
|
||||
.eq(S.a(UserDO::getId), "1")
|
||||
.gt(S.a(UserDO::getSex), "3")
|
||||
.eq(S.b(UserAddressDO::getTel), "10086")
|
||||
.like(S.b(UserAddressDO::getAddress), "北京"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用F
|
||||
*/
|
||||
@Test
|
||||
void testF() {
|
||||
UserDTO dto = userMapper.userLeftJoin(new QueryWrapper<UserDO>()
|
||||
.eq(F.s(UserDO::getId), "1")
|
||||
.gt(F.s(UserDO::getSex), "3")
|
||||
.eq(F.s(UserAddressDO::getTel), "10086")
|
||||
.like(F.s(UserAddressDO::getAddress), "北京"));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
对应sql:
|
||||
|
||||
```
|
||||
# 使用S
|
||||
select
|
||||
a.*,
|
||||
b.tel
|
||||
from user a
|
||||
left join user_address b on a.id = b.user_id
|
||||
WHERE (
|
||||
a.id = ?
|
||||
AND a.sex > ?
|
||||
AND b.tel = ?
|
||||
AND b.address LIKE ?)
|
||||
|
||||
# 使用F
|
||||
select
|
||||
user.*,
|
||||
user_address.tel
|
||||
from user
|
||||
left join user_address on user.id = user_address.user_id
|
||||
WHERE (
|
||||
user.id = ?
|
||||
AND user.sex > ?
|
||||
AND user_address.tel = ?
|
||||
AND user_address.address LIKE ?)
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user