com.mybaits.jpa.service.LinkTableSelect Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mybatis-jpa Show documentation
Show all versions of mybatis-jpa Show documentation
Mybaits JPA: a convenient development tool for mybatis puls
package com.mybaits.jpa.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mybaits.jpa.annotation.*;
import com.mybaits.jpa.util.MyBaitsJpaContext;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Administrator on 2019/11/17 0017.
*/
public class LinkTableSelect {
public BaseMapper baseMapper;
public void linkSelect(StackTraceElement stackTraceElement, T t){
String methodName = stackTraceElement.getMethodName();
try{
Class clazz=Class.forName(stackTraceElement.getClassName());
Method[] methods=clazz.getMethods();
Method method=null;
for (Method method1 : methods) {
if(method1.getName().equals(methodName)){
method=method1;
break;
}
}
LinkSelect linkSelect=null;
if(method!=null){
linkSelect=method.getAnnotation(LinkSelect.class);
if(linkSelect==null){
return;
}
}
if(t==null){
return;
}
String className=t.getClass().getName();
if(className==null){
return;
}
Class tClass=Class.forName(className);
List classList=new ArrayList<>();
linkNumberSelect(linkSelect,0,classList,tClass,t);
}catch (Exception e){
e.printStackTrace();
}
}
public void linkSelect(StackTraceElement stackTraceElement, List list){
String methodName = stackTraceElement.getMethodName();
try{
Class clazz=Class.forName(stackTraceElement.getClassName());
Method[] methods=clazz.getMethods();
Method method=null;
for (Method method1 : methods) {
if(method1.getName().equals(methodName)){
method=method1;
break;
}
}
LinkSelect linkSelect=null;
if(method!=null){
linkSelect=method.getAnnotation(LinkSelect.class);
if(linkSelect==null){
return;
}
}
String className=null;
if(list!=null&&list.size()>0){
className=list.get(0).getClass().getName();
}
if(className==null){
return;
}
Class tClass=Class.forName(className);
List classList=new ArrayList<>();
linkNumberSelect(linkSelect,0,classList,tClass,list);
}catch (Exception e){
e.printStackTrace();
}
}
public void linkNumberSelect(LinkSelect linkSelect,int number,List classList,Class downClass,Object object){
if(number>=linkSelect.linkLv().getValue()){
return;
}
List classNewList=new ArrayList<>();
classNewList.addAll(classList);
classNewList.add(downClass);
Field[] fields=downClass.getDeclaredFields();
for (Field field : fields) {
OneByOne oneByOne=field.getAnnotation(OneByOne.class);
if(oneByOne!=null){
selectOneByOny(linkSelect,oneByOne,classNewList,object,field,number);
}
OneByMany oneByMany=field.getAnnotation(OneByMany.class);
if(oneByMany!=null){
selectOneByMany(linkSelect,oneByMany,classNewList,object,field,number);
}
ManyByOne manyByOne=field.getAnnotation(ManyByOne.class);
if(manyByOne!=null){
selectManyByOne(linkSelect,manyByOne,classNewList,object,field,number);
}
ManyByMany manyByMany=field.getAnnotation(ManyByMany.class);
if(manyByMany!=null){
selectManyByMany(linkSelect,manyByMany,classNewList,object,field,number);
}
}
}
public void linkNumberSelect(LinkSelect linkSelect,int number,List classList,Class downClass,List list){
if(number>=linkSelect.linkLv().getValue()){
return;
}
List classNewList=new ArrayList<>();
classNewList.addAll(classList);
classNewList.add(downClass);
Field[] fields=downClass.getDeclaredFields();
for (Field field : fields) {
OneByOne oneByOne=field.getAnnotation(OneByOne.class);
if(oneByOne!=null){
selectOneByOny(linkSelect,oneByOne,classNewList,list,field,number);
}
OneByMany oneByMany=field.getAnnotation(OneByMany.class);
if(oneByMany!=null){
selectOneByMany(linkSelect,oneByMany,classNewList,list,field,number);
}
ManyByOne manyByOne=field.getAnnotation(ManyByOne.class);
if(manyByOne!=null){
selectManyByOne(linkSelect,manyByOne,classNewList,list,field,number);
}
ManyByMany manyByMany=field.getAnnotation(ManyByMany.class);
if(manyByMany!=null){
selectManyByMany(linkSelect,manyByMany,classNewList,list,field,number);
}
}
}
public void selectOneByOny(LinkSelect linkSelect,OneByOne oneByOne,List classList,List list,Field field,int number){
if(!chackAnnotation(oneByOne)){
return;
}
if(baseMapper==null){
Object objectBean= oneByOne.aimEntity().getAnnotation(DaoClass.class);
if(objectBean!=null){
DaoClass daoClass= (DaoClass) objectBean;
baseMapper= (BaseMapper) MyBaitsJpaContext.getBean(daoClass.daoClass());
}else{
String classByName=oneByOne.aimEntity().getName().substring(oneByOne.aimEntity().getName().lastIndexOf(".")+1);
baseMapper= (BaseMapper) MyBaitsJpaContext.getBean("I"+classByName+"Dao");
}
if(baseMapper==null){
System.err.println("未找到"+oneByOne.aimEntity().getName()+"对应dao");
System.err.println("请检查实体与dao命名是否一致如:IDemoDao Demo");
System.err.println("或者在实体上添加注解:@DaoClass(daoClass=\"IDemoDao.class\")");
System.err.println("连表查询失败");
return;
}
}
List keys=new ArrayList();
for (Object o : list) {
keys.add(getGetMethod(o,oneByOne.field()));
}
QueryWrapper queryWrapper=new QueryWrapper();
queryWrapper.in(oneByOne.aimField(),keys);
List listResult=baseMapper.selectList(queryWrapper);
if(listResult==null||listResult.size()==0){
return;
}
for (Class aClass : classList) {
if(aClass.getName().equals(listResult.get(0).getClass().getName())){
return;
}
}
number++;
linkNumberSelect(linkSelect,number,classList,listResult.get(0).getClass(),listResult);
for (Object o : list) {
for (Object o1 : listResult) {
if(getGetMethod(o,oneByOne.field()).equals(getGetMethod(o1,oneByOne.aimField()))){
setValue(o,field.getName(),o1.getClass(),o1);
}
}
}
}
public void selectOneByOny(LinkSelect linkSelect,OneByOne oneByOne,List classList,Object object,Field field,int number){
if(!chackAnnotation(oneByOne)){
return;
}
BaseMapper baseMapper=null;
Object objectBean= oneByOne.aimEntity().getAnnotation(DaoClass.class);
if(objectBean!=null){
DaoClass daoClass= (DaoClass) objectBean;
baseMapper= (BaseMapper) MyBaitsJpaContext.getBean(daoClass.daoClass());
}else{
String classByName=oneByOne.aimEntity().getName().substring(oneByOne.aimEntity().getName().lastIndexOf(".")+1);
baseMapper= (BaseMapper) MyBaitsJpaContext.getBean("I"+classByName+"Dao");
}
if(baseMapper==null){
System.err.println("未找到"+oneByOne.aimEntity().getName()+"对应dao");
System.err.println("请检查实体与dao命名是否一致如:IDemoDao Demo");
System.err.println("或者在实体上添加注解:@DaoClass(daoClass=\"IDemoDao.class\")");
System.err.println("连表查询失败");
return;
}
QueryWrapper queryWrapper=new QueryWrapper();
queryWrapper.eq(oneByOne.aimField(),getGetMethod(object,oneByOne.field()));
Object objectResult=baseMapper.selectOne(queryWrapper);
if(objectResult==null){
return;
}
for (Class aClass : classList) {
if(aClass.getName().equals(objectResult.getClass().getName())){
return;
}
}
number++;
linkNumberSelect(linkSelect,number,classList,objectResult.getClass(),objectResult);
setValue(object,field.getName(),objectResult.getClass(),objectResult);
}
public void selectOneByMany(LinkSelect linkSelect,OneByMany oneByMany,List classList,List list,Field field,int number){
if(!chackAnnotation(oneByMany)){
return;
}
BaseMapper baseMapper=null;
Object objectBean= oneByMany.aimEntity().getAnnotation(DaoClass.class);
if(objectBean!=null){
DaoClass daoBean= (DaoClass) objectBean;
baseMapper= (BaseMapper) MyBaitsJpaContext.getBean(daoBean.daoClass());
}else{
String classByName=oneByMany.aimEntity().getName().substring(oneByMany.aimEntity().getName().lastIndexOf(".")+1);
baseMapper= (BaseMapper) MyBaitsJpaContext.getBean("I"+classByName+"Dao");
}
if(baseMapper==null){
System.err.println("未找到"+oneByMany.aimEntity().getName()+"对应dao");
System.err.println("请检查实体与dao命名是否一致如:IDemoDao Demo");
System.err.println("或者在实体上添加注解:@DaoClass(daoClass=\"IDemoDao.class\")");
System.err.println("连表查询失败");
return;
}
List keys=new ArrayList();
for (Object o : list) {
keys.add(getGetMethod(o,oneByMany.field()));
}
QueryWrapper queryWrapper=new QueryWrapper();
queryWrapper.in(oneByMany.aimField(),keys);
List listResult=baseMapper.selectList(queryWrapper);
if(listResult==null||listResult.size()==0){
return;
}
for (Class aClass : classList) {
if(aClass.getName().equals(listResult.get(0).getClass().getName())){
return;
}
}
number++;
linkNumberSelect(linkSelect,number,classList,listResult.get(0).getClass(),listResult);
for (Object o : list) {
List thisList=new ArrayList();
for (Object o1 : listResult) {
if(getGetMethod(o,oneByMany.field()).equals(getGetMethod(o1,oneByMany.aimField()))){
thisList.add(o1);
}
}
if(thisList.size()>0){
setValue(o,field.getName(),List.class,thisList);
}
}
}
public void selectOneByMany(LinkSelect linkSelect,OneByMany oneByMany,List classList,Object object,Field field,int number){
if(!chackAnnotation(oneByMany)){
return;
}
BaseMapper baseMapper=null;
Object objectBean= oneByMany.aimEntity().getAnnotation(DaoClass.class);
if(objectBean!=null){
DaoClass daoBean= (DaoClass) objectBean;
baseMapper= (BaseMapper) MyBaitsJpaContext.getBean(daoBean.daoClass());
}else{
String classByName=oneByMany.aimEntity().getName().substring(oneByMany.aimEntity().getName().lastIndexOf(".")+1);
baseMapper= (BaseMapper) MyBaitsJpaContext.getBean("I"+classByName+"Dao");
}
if(baseMapper==null){
System.err.println("未找到"+oneByMany.aimEntity().getName()+"对应dao");
System.err.println("请检查实体与dao命名是否一致如:IDemoDao Demo");
System.err.println("或者在实体上添加注解:@DaoClass(daoClass=\"IDemoDao.class\")");
System.err.println("连表查询失败");
return;
}
QueryWrapper queryWrapper=new QueryWrapper();
queryWrapper.eq(oneByMany.aimField(),getGetMethod(object,oneByMany.field()));
List listResult=baseMapper.selectList(queryWrapper);
if(listResult==null||listResult.size()==0){
return;
}
for (Class aClass : classList) {
if(aClass.getName().equals(listResult.get(0).getClass().getName())){
return;
}
}
number++;
linkNumberSelect(linkSelect,number,classList,listResult.get(0).getClass(),listResult);
setValue(object,field.getName(),List.class,listResult);
}
public void selectManyByOne(LinkSelect linkSelect,ManyByOne manyByOne,List classList,List list,Field field,int number){
if(!chackAnnotation(manyByOne)){
return;
}
BaseMapper baseMapper=null;
Object objectBean= manyByOne.aimEntity().getAnnotation(DaoClass.class);
if(objectBean!=null){
DaoClass daoBean= (DaoClass) objectBean;
baseMapper= (BaseMapper) MyBaitsJpaContext.getBean(daoBean.daoClass());
}else{
String classByName=manyByOne.aimEntity().getName().substring(manyByOne.aimEntity().getName().lastIndexOf(".")+1);
baseMapper= (BaseMapper) MyBaitsJpaContext.getBean("I"+classByName+"Dao");
}
if(baseMapper==null){
System.err.println("未找到"+manyByOne.aimEntity().getName()+"对应dao");
System.err.println("请检查实体与dao命名是否一致如:IDemoDao Demo");
System.err.println("或者在实体上添加注解:@DaoClass(daoClass=\"IDemoDao.class\")");
System.err.println("连表查询失败");
return;
}
Map