关于QueryWrapper,实现MybatisPlus多表关联查询⽅式
⽬录
QueryWrapper实现MybatisPlus多表关联查询
1.dao层接⼝使⽤Select注解写SQL
2.service层代码⽰例
3.反射⼯具类
4.判空⼯具类
MybatisPlusQueryWrapper简单⽤法
QueryWrapper实现MybatisPlus多表关联查询
1.dao层接⼝使⽤Select注解写SQL
重点:@Param("ew") Wrapper参数是必须,因为${ew.customSqlSegment} 底层其实就是where 条件,所以为了保证Wrapper不为空,service层代码中的Wrapper⾄少需要有⼀个条件:1 = 1
@Override
@Select("de as code , b.name as name , b.barcode as barcode , a.ware_code as wareCode , c.name as wareName , a.qty as qty , a.oprice as oprice , a.total as total , " + " a.id as id , a.create_by as createBy , a.create_date as createDate , a.update_by as updateBy , a.update_date as updateDate , a.status as status , a.remarks as remarks " + "from sku_stock a , goods b , warehouse c " +
"${ew.customSqlSegment} de = b.code and a.ware_code = c.code")
IPage<SkuStock> selectPage(IPage<SkuStock> page, @Param("ew")Wrapper<SkuStock> queryWrapper);
2.service层代码⽰例
service⽗类封装的findPage⽅法:
/**
* 封装findPage
* @param entity
* @param search Map中的key:";"为保留关键字,拆分数组,仅⽀持最⼤长度2的数组,
* 下标0:QueryWrapper查询条件中的列名(⽀持多表关联查询的表别名 + 列名⽅式,需要dao层接⼝⽀持)
* 下标1: QueryWrapper中不同的查询条件,eq:等于,ge:⼤于等..... todo:请⾃⾏完善Mybatis eq、ne、gt、lt、ge、le等
* Map中的value:QueryWrapper需要查询的值
* @param args QueryWrapper中order by 排序数组
* @return
*/
public IPage<T> findPage(T entity , Map<String , Object> search , args){
long current = 1L;
long size = 10L;
if (EmptyUtil.FieldValue(entity , "page")) && (long) FieldValue(entity , "page") != 0){
current = (long) FieldValue(entity , "page");
}
if (EmptyUtil.FieldValue(entity , "limit")) && (long) FieldValue(entity , "limit") != 0){
size = (long) FieldValue(entity , "limit");
}
QueryWrapper<T> queryWrapper;
if (EmptyUtil.isNoEmpty(search)){
queryWrapper = new QueryWrapper<>();
for (Map.Entry<String , Object> Set()
) {
String[] key = Key().split(";");
if (key.length > 1){
if (key[1].equals("eq")){
queryWrapper.eq(key[0] , Value());
}else if (key[1].equals("ge")){
<(key[0] , Value());
}else if (key[1].equals("lt")){
queryWrapper.lt(key[0] , Value());
}
}else {
queryWrapper.Key() , Value());
}
}
}else {
queryWrapper = new QueryWrapper<>(entity);
}
return super.page(new Page<T>(current , size) , queryWrapper);
}
service实现类⽅法:
public IPage<SkuStock> findPage(SkuStock entity, args) {
Map<String , Object> search = null;
search = new HashedMap();
search.put("1;eq" , "1");
if (EmptyUtil.Code())
|| EmptyUtil.WareCode())
){
if (EmptyUtil.Code())){
search.put("code" , Code());
if (EmptyUtil.WareCode())){
search.put("ware_code" , WareCode());
}
}else {
long limit = Limit();
long page = Page();
entity = new SkuStock();
entity.setLimit(limit);
entity.setPage(page);
}
return super.findPage(entity , search , args);
}
3.反射⼯具类
package org.bluedreamm.utils;
import flect.Field;
import flect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @ClassName ReflexUtil
* @Description TODO
* @Author foxsand
* @Data 2021-06-09 15:17
* @Version
*/
public class ReflexUtil {
/**
* 返回 entity 对象的所有属性,包含⽗类
* @param obj
* @return
*/
public static List<Field> getObjectFields(Object obj){
Class clazz = Class();
List<Field> fieldList = new ArrayList<>() ;
while (clazz != null) {//当⽗类为null的时候说明到达了最上层的⽗类(Object类).
fieldList.addAll(Arrays.asList(clazz .getDeclaredFields()));
clazz = Superclass(); //得到⽗类,然后赋给⾃⼰
}
return fieldList;
}
public static List<Field> getObjectFields(Class<?> clazz){
List<Field> fieldList = new ArrayList<>() ;
while (clazz != null){
fieldList.addAll(Arrays.asList(clazz .getDeclaredFields()));
clazz = Superclass(); //得到⽗类,然后赋给⾃⼰
}
return fieldList;
}
/**
* 判断 Class entity 是否存在名称为 fieldName 的属性
* @param fieldName
* @param entity
* @return
*/
public static Boolean isField(String fieldName , Object entity){
List<Field> fieldList = getObjectFields(entity);
for (Field f1:fieldList
) {
if (fieldName.Name()))
return true;
}
return false;
}
/**
* 返回 entity 对象中的所有⽅法,包含⽗类
* @param entity
* @return
*/
public static List<Method> getObjectMethods(Object entity){
Class<?> clazz = Class();
List<Method> methods = new ArrayList<>();
while (clazz != null && clazz != Object.class) {//当⽗类为null的时候说明到达了最上层的⽗类(Object类). methods.addAll(Arrays.asList(clazz .getDeclaredMethods()));
clazz = Superclass(); //得到⽗类,然后赋给⾃⼰
}
return methods;
}
public static List<Method> getObjectMethods(Class<?> clazz){
List<Method> methods = new ArrayList<>();
while (clazz != null && clazz != Object.class) {//当⽗类为null的时候说明到达了最上层的⽗类(Object类). methods.addAll(Arrays.asList(clazz .getDeclaredMethods()));
clazz = Superclass(); //得到⽗类,然后赋给⾃⼰
return methods;
}
/**
* 判断 Class entity 是否存在名称为 methodName 的⽅法
* @param methodName
* @param entity
* @return
*/
public static Boolean isMethod(String methodName , Object entity){
List<Method> methods = getObjectMethods(entity);
for (Method m1:methods
) {
if (methodName.Name()))
return true;
}
return false;
}
/**
* 循环向上转型, 获取对象的 DeclaredMethod
* @param obj
* @param methodName
* @param parameterTypes ⽅法参数类型
* @return
*/
public static Method getDeclaredMethod(Object obj , String methodName , Class<?>...parameterTypes) {
for (Class<?> clazz = Class(); clazz != Object.class && clazz != null; clazz = Superclass()) { try {
DeclaredMethod(methodName, parameterTypes);
} catch (Exception e) {
// 这⾥甚么都不要做!并且这⾥的异常必须这样写,不能抛出去。
// 如果这⾥的异常打印或者往外抛,则就不会执⾏Superclass(),最后就不会进⼊到⽗类
中了 }
}
return null;
}
public static Object invoke(Object object, String methodName, Class<?>[] parameterTypes,
Object[] parameters){
Method method = getDeclaredMethod(object, methodName, parameterTypes);
try {
if (method != null){
method.setAccessible(true);
// 调⽤object 的 method 所代表的⽅法,其⽅法的参数是 parameters
return method.invoke(object, parameters);
}
}catch (Exception e1){
e1.printStackTrace();
}
return null;
}
/**
* 循环向上转型, 获取对象的 DeclaredField
*
* @param object
* : ⼦类对象
* @param fieldName
* : ⽗类中的属性名
* @return ⽗类中的属性对象
*/
public static Field getDeclaredField(Object object, String fieldName) {
Field field = null;
Class<?> clazz = Class();
for (; clazz != Object.class && clazz != null; clazz = Superclass()) {
try {
field = DeclaredField(fieldName);
return field;
} catch (Exception e) {
// 这⾥甚么都不要做!并且这⾥的异常必须这样写,不能抛出去。
// 如果这⾥的异常打印或者往外抛,则就不会执⾏clazz = Superclass(),最后就不会进⼊到⽗类中了 }
}
return null;
}
/**
* 直接设置对象属性值, 忽略 private/protected 修饰符, 也不经过 setter
*
* @param object
* : ⼦类对象
* @param fieldName
* : ⽗类中的属性名
* @param value
* : 将要设置的值
*/
public static void setFieldValue(Object object, String fieldName, Object value) {
if (field != null){
// 抑制Java对其的检查
field.setAccessible(true);
try {
/
/ 将 object 中 field 所代表的值设置为 value
field.set(object, value);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
/**
* 直接读取对象的属性值, 忽略 private/protected 修饰符, 也不经过 getter *
* @param object
* : ⼦类对象
* @param fieldName
* : ⽗类中的属性名
* @return : ⽗类中的属性值
*/
public static Object getFieldValue(Object object, String fieldName) {
// 根据对象和属性名通过反射调⽤上⾯的⽅法获取 Field对象
Field field = getDeclaredField(object, fieldName);
if (field != null){
// 抑制Java对其的检查
field.setAccessible(true);
try {
// 获取 object 中 field 所代表的属性值
(object);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
4.判空⼯具类
package org.bluedreamm.utils;
import java.util.Collection;
import java.util.Map;
public class EmptyUtil {
//Suppress default constructor for noninstantiability
private EmptyUtil(){
throw new AssertionError();
}
public static boolean isEmpty(Object object){
if (object == null){
return true;
}
if (object instanceof int[]){
return ((int[]) object).length == 0;
}
if (object instanceof double[]){
return ((double[]) object).length == 0;
}
if (object instanceof long[]){
return ((long[]) object).length == 0;
}
if (object instanceof byte[]){
return ((byte[]) object).length == 0;
}
if (object instanceof short[]){
return ((short[]) object).length == 0;
}
if (object instanceof float[]){
return ((float[]) object).length == 0;
}
if (object instanceof char[]){
return ((char[]) object).length == 0;
}
if (object instanceof Object[]){
return ((Object[]) object).length == 0;
}
if (object instanceof CharSequence) {
return ((CharSequence) object).length() == 0;
}
if (object instanceof Collection ){
return ((Collection) object).isEmpty();
}
if (object instanceof Map){
return false;
}
public static boolean isNoEmpty(Object object){
return !isEmpty(object);
}
}
MybatisPlus QueryWrapper简单⽤法
查询⽅式说明
setSqlSelect设置 SELECT 查询字段
where WHERE 语句,拼接 +?WHERE 条件
and AND 语句,拼接 +?AND 字段=值
andNew AND 语句,拼接 +?AND (字段=值)
or OR 语句,拼接 +?OR 字段=值
orNew OR 语句,拼接 +?OR (字段=值)
eq等于=
allEq基于 map 内容等于=
ne不等于<>
gt⼤于>
ge⼤于等于>=
lt⼩于<
le⼩于等于<=
like模糊查询 LIKE
notLike模糊查询 NOT LIKE
in IN 查询
notIn NOT IN 查询
isNull NULL 值查询
isNotNull IS NOT NULL
groupBy分组 GROUP BY
having HAVING 关键词
java反射获取父类属性orderBy排序 ORDER BY
orderAsc ASC 排序 ORDER BY
orderDesc DESC 排序 ORDER BY
exists EXISTS 条件语句
notExists NOT EXISTS 条件语句
between BETWEEN 条件语句
notBetween NOT BETWEEN 条件语句
addFilter⾃由拼接 SQL
last拼接在最后,例如:last(“LIMIT 1”)
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论