优化别名

This commit is contained in:
yulichang 2023-04-14 16:59:32 +08:00
parent 59a85f6414
commit e8d92f10e1
8 changed files with 106 additions and 23 deletions

View File

@ -9,8 +9,4 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@SuppressWarnings("unused")
public class MPJBaseServiceImpl<M extends MPJBaseMapper<T>, T> extends ServiceImpl<M, T> implements MPJBaseService<T> {
@Override
public Class<T> currentModelClass() {
return super.currentModelClass();
}
}

View File

@ -123,7 +123,7 @@ public class TableList {
}
} else if (list.size() > 1) {
for (Node n : list) {
if (n.getClazz() != node.getClazz()) {
if (n.getIndex() != node.getIndex()) {
return n.isHasAlias() ? n.getAlias() : (n.getAlias() + n.getIndex());
}
}

View File

@ -2,7 +2,9 @@ package com.github.yulichang.wrapper.interfaces;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.github.yulichang.base.MPJBaseMapper;
import com.github.yulichang.interfaces.MPJBaseJoin;
import com.github.yulichang.toolkit.SqlHelper;
@ -24,6 +26,22 @@ public interface Chain<T> {
Class<T> getEntityClass();
/**
* 链式调用 等效于MP mapper的 selectCount()
* <p>
* 构造方法必须传 class entity 否则会报错
* new MPJLambdaWrapper(User.class)
* new MPJQueryWrapper(User.class)
* JoinWrappers.\<User\>lambdaJoin(User.class)
* JoinWrappers.\<User\>queryJoin(User.class)
*/
default Long count() {
return SqlHelper.exec(getEntityClass(), mapper -> {
Assert.isTrue(mapper instanceof MPJBaseMapper, "mapper <%s> is not extends MPJBaseMapper", mapper.getClass().getSimpleName());
return ((MPJBaseMapper<T>) mapper).selectJoinCount((MPJBaseJoin<T>) this);
});
}
/**
* 链式调用 等效于 selectOne
* <p>

View File

@ -1,7 +1,12 @@
//package com.github.yulichang.test.config;
//
//import com.baomidou.mybatisplus.core.injector.AbstractMethod;
//import com.baomidou.mybatisplus.core.injector.ISqlInjector;
//import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
//import com.baomidou.mybatisplus.core.toolkit.StringUtils;
//import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
//import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
//import com.github.yulichang.injector.MPJSqlInjector;
//import com.github.yulichang.test.util.ThreadLocalUtils;
//import org.apache.ibatis.cache.CacheKey;
//import org.apache.ibatis.executor.Executor;
@ -13,9 +18,13 @@
//import org.apache.ibatis.session.RowBounds;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.core.Ordered;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//
//import java.sql.Connection;
//import java.util.List;
//import java.util.Objects;
//import java.util.StringTokenizer;
//
@ -30,6 +39,21 @@
// return new PaginationInterceptor();
// }
//
// @Bean
// @Primary
// @Order(Ordered.HIGHEST_PRECEDENCE)
// public ISqlInjector sqlInjector() {
// return new MPJSqlInjector() {
// @Override
// public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
// List<AbstractMethod> list = super.getMethodList(mapperClass);
// //添加你的方法
// list.add(new InsertBatchSomeColumn());
// return list;
// }
// };
// }
//
// @Component
// @Intercepts({
// @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}),
@ -47,23 +71,36 @@
// if (target instanceof StatementHandler) {
// boundSql = ((StatementHandler) target).getBoundSql();
// String sql = boundSql.getSql();
// String s = ThreadLocalUtils.get();
// if (sql != null && s != null) {
// String s1 = formatSql(sql);
// String s2 = formatSql(s);
// if (StringUtils.isNotBlank(s)) {
// if (!Objects.equals(s1.toLowerCase(), s2.toLowerCase())) {
// System.err.println("执行sql: " + removeExtraWhitespaces(sql));
// System.err.println("预期sql: " + removeExtraWhitespaces(s));
// throw new RuntimeException("sql error");
// }else {
// System.out.println("===============================================");
// System.out.println();
// System.out.println("pass");
// System.out.println();
// System.out.println("===============================================");
// List<String> strings = ThreadLocalUtils.get();
// if (CollectionUtils.isNotEmpty(strings)) {
// boolean flag = false;
// String ss = null;
// for (String s : strings) {
// if (sql != null && s != null) {
// String s1 = formatSql(sql);
// String s2 = formatSql(s);
// if (StringUtils.isNotBlank(s)) {
// if (!Objects.equals(s1.toLowerCase(), s2.toLowerCase())) {
// ss = s;
// } else {
// flag = true;
// break;
//
// }
// }
// }
// }
// if (flag) {
// System.out.println("===============================================");
// System.out.println();
// System.out.println("pass");
// System.out.println();
// System.out.println("===============================================");
// } else {
// System.err.println("执行sql: " + removeExtraWhitespaces(sql));
// System.err.println("预期sql: " + removeExtraWhitespaces(ss));
// throw new RuntimeException("sql error");
// }
// }
// }
// System.out.println(boundSql);

View File

@ -54,6 +54,14 @@ public class MybatisPlusConfig {
@Order(Ordered.HIGHEST_PRECEDENCE)
public ISqlInjector sqlInjector() {
return new MPJSqlInjector() {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
List<AbstractMethod> list = super.getMethodList(mapperClass);
//添加你的方法
list.add(new InsertBatchSomeColumn());
return list;
}
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> list = super.getMethodList(mapperClass, tableInfo);

View File

@ -1,5 +1,9 @@
package com.github.yulichang.test.join.service;
public interface UserService {
import com.github.yulichang.base.MPJBaseService;
import com.github.yulichang.test.join.entity.UserDO;
public interface UserService extends MPJBaseService<UserDO> {
}

View File

@ -1,9 +1,13 @@
package com.github.yulichang.test.join.service.impl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.test.join.entity.UserDO;
import com.github.yulichang.test.join.mapper.UserMapper;
import com.github.yulichang.test.join.service.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
public class UserServiceImpl extends MPJBaseServiceImpl<UserMapper, UserDO> implements UserService {
}

View File

@ -685,7 +685,8 @@ class LambdaWrapperTest {
.lt(UserDO::getId, 8));
assert dos.size() == 4;
ThreadLocalUtils.set("SELECT id,pid,`name`,`json`,sex,head_img,create_time,address_id,address_id2,del,create_by,update_by FROM `user` t WHERE t.del=false AND (t.id > ? AND t.id < ?)");
ThreadLocalUtils.set("SELECT id,pid,`name`,`json`,sex,head_img,create_time,address_id,address_id2,del,create_by,update_by FROM `user` t WHERE t.del=false AND (t.id > ? AND t.id < ?)",
"SELECT * FROM `user` t WHERE t.del=false AND (t.id > ? AND t.id < ?) ");
List<UserDO> dos1 = userMapper.selectList(new MPJLambdaWrapper<UserDO>()
.gt(UserDO::getId, 3)
.lt(UserDO::getId, 8));
@ -893,4 +894,19 @@ class LambdaWrapperTest {
assert list.get(0).getAddressList() != null && list.get(0).getAddressList().get(0).getId() != null;
list.forEach(System.out::println);
}
@Test
void joinRandomMap111() {
ThreadLocalUtils.set("SELECT t.id,t.user_id,t.area_id,t.tel,t.address,t.del FROM address t LEFT JOIN `user` t1 ON (t1.address_id = t.id) LEFT JOIN `user` t2 ON (t2.pid = t1.id) WHERE t.del=false AND t1.del=false AND t2.del=false");
MPJLambdaWrapper<AddressDO> wrapper = JoinWrappers.lambda(AddressDO.class)
.selectAll(AddressDO.class)
// .selectAssociation(AddressDO::getUsera, map -> map
// .all("t1", UserDO.class)
// .result("t2", UserDO::getName, UserDO::getPname))
.leftJoin(UserDO.class, UserDO::getAddressId, AddressDO::getId)
.leftJoin(UserDO.class, UserDO::getPid, UserDO::getId);
List<AddressDO> addressDOS = wrapper.list();
System.out.println(1);
}
}