|
@@ -4,21 +4,19 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.IService;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import edu.travel.annotation.LinkConst;
|
|
|
-import edu.travel.annotation.LinkMany;
|
|
|
-import edu.travel.annotation.LinkOne;
|
|
|
+import edu.travel.annotation.*;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.jdbc.core.RowCallbackHandler;
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.lang.reflect.Method;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
/**
|
|
@@ -33,6 +31,11 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
public void setApplicationContext(ApplicationContext applicationContext) {
|
|
|
this.applicationContext = applicationContext;
|
|
|
}
|
|
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
+ @Autowired
|
|
|
+ public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
|
|
|
+ this.jdbcTemplate = jdbcTemplate;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 一对一字典查询处理
|
|
@@ -78,7 +81,48 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
iPage.setRecords(records);
|
|
|
return iPage;
|
|
|
}
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 一对一字典查询处理
|
|
|
+ * @param queryWrapper
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public T getOneLinkSQL(Wrapper<T> queryWrapper,Object... params) {
|
|
|
+ T one = super.getOne(queryWrapper);
|
|
|
+ if (one == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ doOneLink(one, params);
|
|
|
+ return one;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 一对多字典查询处理
|
|
|
+ * @param queryWrapper
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<T> getListLinkSQL(Wrapper<T> queryWrapper,Object... params) {
|
|
|
+ List<T> list = super.list(queryWrapper);
|
|
|
+ if (list == null || list.isEmpty()) {
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+ doManyLink(list,params);
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 分页查询处理
|
|
|
+ * @param queryWrapper
|
|
|
+ * @param page
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public IPage<T> getPageLinkSQL(Wrapper<T> queryWrapper,IPage<T> page,Object... params) {
|
|
|
+ IPage<T> iPage = super.page(page, queryWrapper);
|
|
|
+ if (iPage == null ) {
|
|
|
+ return new Page();
|
|
|
+ }
|
|
|
+ List<T> records = iPage.getRecords();
|
|
|
+ doManyLink(records,params);
|
|
|
+ iPage.setRecords(records);
|
|
|
+ return iPage;
|
|
|
+ }
|
|
|
/**
|
|
|
* 多对多查询字典处理
|
|
|
* @param list
|
|
@@ -88,6 +132,15 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
doOneLink(many);
|
|
|
}
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 多对多查询字典处理
|
|
|
+ * @param list
|
|
|
+ */
|
|
|
+ private void doManyLink(List<T> list,Object... params) {
|
|
|
+ for (T many : list) {
|
|
|
+ doOneLink(many,params);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 单个字典处理
|
|
@@ -101,6 +154,28 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
doLinkOneToOne(one, field);
|
|
|
doLinkOneToMany(one,field);
|
|
|
doConst(one,field);
|
|
|
+ doManyToMany(one,field);
|
|
|
+ try {
|
|
|
+ doLinkSQL(one,null,field);
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private void doOneLink(T one,Object... params) {
|
|
|
+ Class<?> clazz = one.getClass();
|
|
|
+ Field[] declaredFields = clazz.getDeclaredFields();
|
|
|
+ for (Field field : declaredFields) {
|
|
|
+ field.setAccessible(true);
|
|
|
+ doLinkOneToOne(one, field);
|
|
|
+ doLinkOneToMany(one,field);
|
|
|
+ doConst(one,field);
|
|
|
+ doManyToMany(one,field);
|
|
|
+ try {
|
|
|
+ doLinkSQL(one,params,field);
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -207,4 +282,69 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ public void doManyToMany(T one,Field field) {
|
|
|
+ ManyToMany annotation = field.getAnnotation(ManyToMany.class);
|
|
|
+ if (annotation == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String primaryField = annotation.primaryField();
|
|
|
+ try {
|
|
|
+ Field declaredField = one.getClass().getDeclaredField(primaryField);
|
|
|
+ declaredField.setAccessible(true);
|
|
|
+ Object object = declaredField.get(one);
|
|
|
+ Class middleMapperClass = annotation.linkMiddleMapper();
|
|
|
+ Object midMapper = applicationContext.getBean(middleMapperClass);
|
|
|
+ if (midMapper instanceof BaseMapper) {
|
|
|
+ BaseMapper<T> middleMapper = (BaseMapper<T>) midMapper;
|
|
|
+ String middleField = annotation.middleField();
|
|
|
+ List<Object> list = middleMapper.selectObjs(new QueryWrapper<T>().select(middleField).eq(primaryField, object));
|
|
|
+ if (list != null && !list.isEmpty()) {
|
|
|
+ Class finalMapperClass = annotation.finalMapper();
|
|
|
+ Object finalMapper = applicationContext.getBean(finalMapperClass);
|
|
|
+ if (finalMapper instanceof BaseMapper) {
|
|
|
+ BaseMapper<T> finallyMapper = (BaseMapper<T>)finalMapper;
|
|
|
+ String finalField = annotation.finalField();
|
|
|
+ List<T> selectList = finallyMapper.selectList(new QueryWrapper<T>().select(finalField).in(finalField, list));
|
|
|
+ field.set(one,selectList);
|
|
|
+ }else {
|
|
|
+ throw new RuntimeException("mapper 转换异常");
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ throw new RuntimeException("mapper 转换异常");
|
|
|
+ }
|
|
|
+
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void doLinkSQL(T one,Object[] params,Field field) throws IllegalAccessException {
|
|
|
+ LinkSQL linkSQL = field.getAnnotation(LinkSQL.class);
|
|
|
+ if (linkSQL == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String value = linkSQL.value();
|
|
|
+ if (StringUtils.isBlank(value)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<Map<String, Object>> maps = jdbcTemplate.queryForList(value,params);
|
|
|
+ if (maps != null && !maps.isEmpty()) {
|
|
|
+ field.set(one,maps);
|
|
|
+ }else {
|
|
|
+ field.set(one,new ArrayList<>());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
}
|