|
@@ -12,26 +12,30 @@ 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.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
/**
|
|
|
* 扩展mbp ServiceImpl
|
|
|
+ *
|
|
|
* @param <M>
|
|
|
* @param <T>
|
|
|
* @author 忠畅
|
|
|
*/
|
|
|
-public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T> implements IService<T> {
|
|
|
+public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements IService<T> {
|
|
|
private ApplicationContext applicationContext;
|
|
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
+
|
|
|
@Autowired
|
|
|
public void setApplicationContext(ApplicationContext applicationContext) {
|
|
|
this.applicationContext = applicationContext;
|
|
|
}
|
|
|
- private JdbcTemplate jdbcTemplate;
|
|
|
+
|
|
|
@Autowired
|
|
|
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
|
|
|
this.jdbcTemplate = jdbcTemplate;
|
|
@@ -39,6 +43,7 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
|
|
|
/**
|
|
|
* 一对一字典查询处理
|
|
|
+ *
|
|
|
* @param queryWrapper
|
|
|
* @return
|
|
|
*/
|
|
@@ -53,6 +58,7 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
|
|
|
/**
|
|
|
* 一对多字典查询处理
|
|
|
+ *
|
|
|
* @param queryWrapper
|
|
|
* @return
|
|
|
*/
|
|
@@ -67,13 +73,14 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
|
|
|
/**
|
|
|
* 分页查询处理
|
|
|
+ *
|
|
|
* @param queryWrapper
|
|
|
* @param page
|
|
|
* @return
|
|
|
*/
|
|
|
- public IPage<T> getPageLink(Wrapper<T> queryWrapper,IPage<T> page) {
|
|
|
+ public IPage<T> getPageLink(Wrapper<T> queryWrapper, IPage<T> page) {
|
|
|
IPage<T> iPage = super.page(page, queryWrapper);
|
|
|
- if (iPage == null ) {
|
|
|
+ if (iPage == null) {
|
|
|
return new Page();
|
|
|
}
|
|
|
List<T> records = iPage.getRecords();
|
|
@@ -81,12 +88,14 @@ 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) {
|
|
|
+ public T getOneLinkSQL(Wrapper<T> queryWrapper, Object... params) {
|
|
|
T one = super.getOne(queryWrapper);
|
|
|
if (one == null) {
|
|
|
return null;
|
|
@@ -94,37 +103,43 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
doOneLink(one, params);
|
|
|
return one;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 一对多字典查询处理
|
|
|
+ *
|
|
|
* @param queryWrapper
|
|
|
* @return
|
|
|
*/
|
|
|
- public List<T> getListLinkSQL(Wrapper<T> queryWrapper,Object... params) {
|
|
|
+ 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);
|
|
|
+ doManyLink(list, params);
|
|
|
return list;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 分页查询处理
|
|
|
+ *
|
|
|
* @param queryWrapper
|
|
|
* @param page
|
|
|
* @return
|
|
|
*/
|
|
|
- public IPage<T> getPageLinkSQL(Wrapper<T> queryWrapper,IPage<T> page,Object... params) {
|
|
|
+ public IPage<T> getPageLinkSQL(Wrapper<T> queryWrapper, IPage<T> page, Object... params) {
|
|
|
IPage<T> iPage = super.page(page, queryWrapper);
|
|
|
- if (iPage == null ) {
|
|
|
+ if (iPage == null) {
|
|
|
return new Page();
|
|
|
}
|
|
|
List<T> records = iPage.getRecords();
|
|
|
- doManyLink(records,params);
|
|
|
+ doManyLink(records, params);
|
|
|
iPage.setRecords(records);
|
|
|
return iPage;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 多对多查询字典处理
|
|
|
+ *
|
|
|
* @param list
|
|
|
*/
|
|
|
private void doManyLink(List<T> list) {
|
|
@@ -132,18 +147,21 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
doOneLink(many);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 多对多查询字典处理
|
|
|
+ *
|
|
|
* @param list
|
|
|
*/
|
|
|
- private void doManyLink(List<T> list,Object... params) {
|
|
|
+ private void doManyLink(List<T> list, Object... params) {
|
|
|
for (T many : list) {
|
|
|
- doOneLink(many,params);
|
|
|
+ doOneLink(many, params);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 单个字典处理
|
|
|
+ *
|
|
|
* @param one
|
|
|
*/
|
|
|
private void doOneLink(T one) {
|
|
@@ -152,28 +170,29 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
for (Field field : declaredFields) {
|
|
|
field.setAccessible(true);
|
|
|
doLinkOneToOne(one, field);
|
|
|
- doLinkOneToMany(one,field);
|
|
|
- doConst(one,field);
|
|
|
- doManyToMany(one,field);
|
|
|
+ doLinkOneToMany(one, field);
|
|
|
+ doConst(one, field);
|
|
|
+ doManyToMany(one, field);
|
|
|
try {
|
|
|
- doLinkSQL(one,null,field);
|
|
|
- }catch (Exception e){
|
|
|
+ doLinkSQL(one, null, field);
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- private void doOneLink(T one,Object... params) {
|
|
|
+
|
|
|
+ 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);
|
|
|
+ doLinkOneToMany(one, field);
|
|
|
+ doConst(one, field);
|
|
|
+ doManyToMany(one, field);
|
|
|
try {
|
|
|
- doLinkSQL(one,params,field);
|
|
|
- }catch (Exception e){
|
|
|
+ doLinkSQL(one, params, field);
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
@@ -181,10 +200,11 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
|
|
|
/**
|
|
|
* 常量字典处理
|
|
|
+ *
|
|
|
* @param one
|
|
|
* @param field
|
|
|
*/
|
|
|
- private void doConst(T one,Field field) {
|
|
|
+ private void doConst(T one, Field field) {
|
|
|
try {
|
|
|
LinkConst linkConst = field.getAnnotation(LinkConst.class);
|
|
|
if (linkConst != null) {
|
|
@@ -202,11 +222,11 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
throw new RuntimeException("常量字典没有这个类");
|
|
|
}
|
|
|
ConcurrentHashMap objectObjectHashMap = new ConcurrentHashMap();
|
|
|
- objectObjectHashMap.put("key",Integer.valueOf(declaredField.get(one).toString()));
|
|
|
+ objectObjectHashMap.put("key", Integer.valueOf(declaredField.get(one).toString()));
|
|
|
objectObjectHashMap.put("value", getValue);
|
|
|
field.set(one, objectObjectHashMap);
|
|
|
}
|
|
|
- }catch (Exception e) {
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
@@ -214,6 +234,7 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
|
|
|
/**
|
|
|
* 处理一对多字典情况
|
|
|
+ *
|
|
|
* @param one
|
|
|
* @param field
|
|
|
*/
|
|
@@ -231,16 +252,16 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
if (applicationContextBean != null) {
|
|
|
List<Map> oneToManyResult = applicationContextBean.selectMaps(new QueryWrapper<T>().eq(linkMany.linkPrimaryField(), object));
|
|
|
if (oneToManyResult != null && !oneToManyResult.isEmpty()) {
|
|
|
- field.set(one,oneToManyResult);
|
|
|
+ field.set(one, oneToManyResult);
|
|
|
}
|
|
|
- } else {
|
|
|
+ } else {
|
|
|
return;
|
|
|
}
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
- }catch (Exception e) {
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return;
|
|
|
}
|
|
@@ -249,10 +270,11 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
|
|
|
/**
|
|
|
* 处理一对一字典情况
|
|
|
+ *
|
|
|
* @param one
|
|
|
* @param field
|
|
|
*/
|
|
|
- private void doLinkOneToOne(T one,Field field) {
|
|
|
+ private void doLinkOneToOne(T one, Field field) {
|
|
|
try {
|
|
|
LinkOne linkOne = field.getAnnotation(LinkOne.class);
|
|
|
if (linkOne != null) {
|
|
@@ -267,16 +289,16 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
List<Map> list = applicationContextBean.selectMaps(new QueryWrapper<T>().eq(linkOne.linkPrimaryField(), object));
|
|
|
if (list != null && !list.isEmpty()) {
|
|
|
Object oneToOneResult = list.get(0);
|
|
|
- field.set(one,oneToOneResult);
|
|
|
+ field.set(one, oneToOneResult);
|
|
|
}
|
|
|
- } else {
|
|
|
+ } else {
|
|
|
return;
|
|
|
}
|
|
|
} else {
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
- }catch (Exception e) {
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return;
|
|
|
}
|
|
@@ -284,7 +306,7 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
}
|
|
|
|
|
|
|
|
|
- public void doManyToMany(T one,Field field) {
|
|
|
+ public void doManyToMany(T one, Field field) {
|
|
|
ManyToMany annotation = field.getAnnotation(ManyToMany.class);
|
|
|
if (annotation == null) {
|
|
|
return;
|
|
@@ -304,31 +326,30 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
Class finalMapperClass = annotation.finalMapper();
|
|
|
Object finalMapper = applicationContext.getBean(finalMapperClass);
|
|
|
if (finalMapper instanceof BaseMapper) {
|
|
|
- BaseMapper<T> finallyMapper = (BaseMapper<T>)finalMapper;
|
|
|
+ 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 {
|
|
|
+ field.set(one, selectList);
|
|
|
+ } else {
|
|
|
throw new RuntimeException("mapper 转换异常");
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
return;
|
|
|
}
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
throw new RuntimeException("mapper 转换异常");
|
|
|
}
|
|
|
|
|
|
- }catch (Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
}
|
|
|
|
|
|
|
|
|
- public void doLinkSQL(T one,Object[] params,Field field) throws IllegalAccessException {
|
|
|
+ public void doLinkSQL(T one, Object[] params, Field field) throws IllegalAccessException {
|
|
|
LinkSQL linkSQL = field.getAnnotation(LinkSQL.class);
|
|
|
if (linkSQL == null) {
|
|
|
return;
|
|
@@ -337,14 +358,13 @@ public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T>
|
|
|
if (StringUtils.isBlank(value)) {
|
|
|
return;
|
|
|
}
|
|
|
- List<Map<String, Object>> maps = jdbcTemplate.queryForList(value,params);
|
|
|
+ List<Map<String, Object>> maps = jdbcTemplate.queryForList(value, params);
|
|
|
if (maps != null && !maps.isEmpty()) {
|
|
|
- field.set(one,maps);
|
|
|
- }else {
|
|
|
- field.set(one,new ArrayList<>());
|
|
|
+ field.set(one, maps);
|
|
|
+ } else {
|
|
|
+ field.set(one, new ArrayList<>());
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
}
|
|
|
}
|