分离mybatis-plus,实现QueryWrapperUtil,注意该类还需要更多的测试;
This commit is contained in:
parent
574625555b
commit
c9494d8f45
14
pom.xml
14
pom.xml
@ -37,7 +37,6 @@
|
||||
<jwt.version>0.9.1</jwt.version>
|
||||
<knife4j.version>4.5.0</knife4j.version>
|
||||
<mybatis-spring-boot.version>3.0.3</mybatis-spring-boot.version>
|
||||
<mybatis-plus-boot-starter.version>3.5.5</mybatis-plus-boot-starter.version>
|
||||
<mysql.version>8.2.0</mysql.version>
|
||||
<jaxb-api.version>2.3.1</jaxb-api.version>
|
||||
<jaxb.version>4.0.5</jaxb.version>
|
||||
@ -65,19 +64,6 @@
|
||||
<version>${mybatis-spring-boot.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- mybatis-plus 增强CRUD -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<version>${mybatis-plus-boot-starter.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- jaxb 处理xml -->
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
|
@ -139,12 +139,6 @@
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- mybatis-plus 增强CRUD -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||
|
@ -69,6 +69,11 @@
|
||||
<artifactId>ruoyi-system</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-plugins-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
@ -13,6 +13,7 @@
|
||||
<properties>
|
||||
<ruoyi.version>3.8.7.3.4</ruoyi.version>
|
||||
<ehcache.version>3.10.8</ehcache.version>
|
||||
<mybatis-plus-boot-starter.version>3.5.5</mybatis-plus-boot-starter.version>
|
||||
</properties>
|
||||
|
||||
<description>
|
||||
@ -27,27 +28,40 @@
|
||||
<version>${ehcache.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- ehcache-->
|
||||
<!-- mybatis-plus 增强CRUD -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<version>${mybatis-plus-boot-starter.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- ruoyi-ehcache-->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-ehcache</artifactId>
|
||||
<version>${ruoyi.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- ehcache-->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-ehcache</artifactId>
|
||||
<version>${ruoyi.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- ehcache-->
|
||||
<!-- mybatis-jpa-->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-mybatis-jpa</artifactId>
|
||||
<version>${ruoyi.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- mybatis-plus-->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-mybatis-plus</artifactId>
|
||||
<version>${ruoyi.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- websocket -->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
@ -68,6 +82,7 @@
|
||||
<modules>
|
||||
<module>ruoyi-ehcache</module>
|
||||
<module>ruoyi-mybatis-jpa</module>
|
||||
<module>ruoyi-mybatis-plus</module>
|
||||
<module>ruoyi-websocket</module>
|
||||
<module>ruoyi-plugins-starter</module>
|
||||
</modules>
|
||||
|
@ -11,7 +11,7 @@
|
||||
<artifactId>ruoyi-mybatis-jpa</artifactId>
|
||||
|
||||
<description>
|
||||
ruoyi-mybatis-plus模块
|
||||
ruoyi-mybatis-jpa
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
@ -22,6 +22,13 @@
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- mybatis-plus 增强CRUD -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
@ -12,6 +12,14 @@ public class BaseColumnInfo {
|
||||
protected Query query;
|
||||
protected String querySql;
|
||||
|
||||
public Field getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
public Query getQuery() {
|
||||
return query;
|
||||
}
|
||||
|
||||
public String getTemplate() {
|
||||
return getTemplate(false);
|
||||
}
|
||||
|
@ -1,69 +1,84 @@
|
||||
// package com.ruoyi.mybatis.utils;
|
||||
package com.ruoyi.mybatis.utils;
|
||||
|
||||
// import java.util.Arrays;
|
||||
// import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
// import org.springframework.core.annotation.AnnotationUtils;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.ruoyi.common.core.domain.BaseEntity;
|
||||
import com.ruoyi.mybatis.annotation.Query;
|
||||
import com.ruoyi.mybatis.domain.TableInfo;
|
||||
|
||||
// import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
// import com.ruoyi.common.core.domain.BaseEntity;
|
||||
// import com.ruoyi.mybatis.annotation.Query;
|
||||
public class QueryWrapperUtil {
|
||||
public static List<String> getArrayFromParam(Object obj) {
|
||||
List<String> list = new ArrayList<>();
|
||||
if (obj instanceof String) {
|
||||
for (String split : ((String) obj).split(",")) {
|
||||
list.add(split);
|
||||
}
|
||||
} else if (obj instanceof Collection) {
|
||||
for (Object split : ((Collection<?>) obj)) {
|
||||
list.add(split.toString());
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
// public class QueryWrapperUtil {
|
||||
// public static <T extends BaseEntity> QueryWrapper<T> initQueryWrapper(T entity) {
|
||||
// QueryWrapper<T> queryWrapper = new QueryWrapper<>();
|
||||
public static <T extends BaseEntity> QueryWrapper<T> initQueryWrapper(T entity) {
|
||||
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
|
||||
TableInfo tableInfo = TableContainer.getTableInfo(entity);
|
||||
Map<String, Object> params = entity.getParams();
|
||||
|
||||
// Class<?> clazz = entity.getClass();
|
||||
// Map<String, Object> params = entity.getParams();
|
||||
// Arrays.stream(clazz.getDeclaredFields())
|
||||
// .filter(field -> AnnotationUtils.findAnnotation(field, Query.class) != null)
|
||||
// .forEach(field -> {
|
||||
// field.setAccessible(true);
|
||||
// Query queryAnnotation = field.getAnnotation(Query.class);
|
||||
tableInfo.getNotNullColumnsForQuery(entity).stream()
|
||||
.forEach(field -> {
|
||||
Object fieldValue = null;
|
||||
try {
|
||||
fieldValue = field.getField().get(entity);
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Query queryAnnotation = field.getQuery();
|
||||
switch (queryAnnotation.operation()) {
|
||||
case eq -> queryWrapper.eq(field.getUnqualifiedColumnName(), fieldValue);
|
||||
case ne -> queryWrapper.ne(field.getUnqualifiedColumnName(), fieldValue);
|
||||
case gt -> queryWrapper.gt(field.getUnqualifiedColumnName(), fieldValue);
|
||||
case ge -> queryWrapper.ge(field.getUnqualifiedColumnName(), fieldValue);
|
||||
case le -> queryWrapper.le(field.getUnqualifiedColumnName(), fieldValue);
|
||||
case lt -> queryWrapper.lt(field.getUnqualifiedColumnName(), fieldValue);
|
||||
case between -> {
|
||||
String begin = queryAnnotation.sections()[0];
|
||||
String end = queryAnnotation.sections()[1];
|
||||
queryWrapper.between(field.getUnqualifiedColumnName(), params.get(begin), params.get(end));
|
||||
}
|
||||
case notBetween -> {
|
||||
String begin = queryAnnotation.sections()[0];
|
||||
String end = queryAnnotation.sections()[1];
|
||||
queryWrapper.notBetween(field.getUnqualifiedColumnName(), params.get(begin),
|
||||
params.get(end));
|
||||
}
|
||||
case like -> queryWrapper.like(field.getUnqualifiedColumnName(), fieldValue);
|
||||
case notLike -> queryWrapper.notLike(field.getUnqualifiedColumnName(), fieldValue);
|
||||
case likeLeft -> queryWrapper.likeLeft(field.getUnqualifiedColumnName(), fieldValue);
|
||||
case likeRight -> queryWrapper.likeRight(field.getUnqualifiedColumnName(), fieldValue);
|
||||
case notLikeLeft -> queryWrapper.notLikeLeft(field.getUnqualifiedColumnName(), fieldValue);
|
||||
case notLikeRight -> queryWrapper.notLikeRight(field.getUnqualifiedColumnName(), fieldValue);
|
||||
case isNull -> queryWrapper.isNull(field.getUnqualifiedColumnName());
|
||||
case isNotNull -> queryWrapper.isNotNull(field.getUnqualifiedColumnName());
|
||||
case in -> queryWrapper.in(field.getUnqualifiedColumnName(),
|
||||
getArrayFromParam(params.get(queryAnnotation.section())));
|
||||
case notIn -> queryWrapper.notIn(field.getUnqualifiedColumnName(),
|
||||
getArrayFromParam(params.get(queryAnnotation.section())));
|
||||
case inSql -> queryWrapper.inSql(field.getUnqualifiedColumnName(), queryAnnotation.sql());
|
||||
case notInSql -> queryWrapper.notInSql(field.getUnqualifiedColumnName(), queryAnnotation.sql());
|
||||
default ->
|
||||
throw new IllegalArgumentException(
|
||||
"Unsupported operation: " + queryAnnotation.operation());
|
||||
}
|
||||
});
|
||||
|
||||
// try {
|
||||
// Object fieldValue = field.get(entity);
|
||||
// if (fieldValue != null) { // 判断属性值是否非空
|
||||
// switch (queryAnnotation.operation()) {
|
||||
// case eq -> queryWrapper.eq(queryAnnotation.column(), fieldValue);
|
||||
// case ne -> queryWrapper.ne(queryAnnotation.column(), fieldValue);
|
||||
// case gt -> queryWrapper.gt(queryAnnotation.column(), fieldValue);
|
||||
// case ge -> queryWrapper.ge(queryAnnotation.column(), fieldValue);
|
||||
// case le -> queryWrapper.le(queryAnnotation.column(), fieldValue);
|
||||
// case lt -> queryWrapper.lt(queryAnnotation.column(), fieldValue);
|
||||
// case between -> {
|
||||
// String begin = queryAnnotation.section()[0];
|
||||
// String end = queryAnnotation.section()[1];
|
||||
// queryWrapper.between(queryAnnotation.column(), params.get(begin), params.get(end));
|
||||
// }
|
||||
// case notBetween -> {
|
||||
// String begin = queryAnnotation.section()[0];
|
||||
// String end = queryAnnotation.section()[1];
|
||||
// queryWrapper.notBetween(queryAnnotation.column(), params.get(begin),
|
||||
// params.get(end));
|
||||
// }
|
||||
// case like -> queryWrapper.like(queryAnnotation.column(), fieldValue);
|
||||
// case notLike -> queryWrapper.notLike(queryAnnotation.column(), fieldValue);
|
||||
// case likeLeft -> queryWrapper.likeLeft(queryAnnotation.column(), fieldValue);
|
||||
// case likeRight -> queryWrapper.likeRight(queryAnnotation.column(), fieldValue);
|
||||
// case notLikeLeft -> queryWrapper.notLikeLeft(queryAnnotation.column(), fieldValue);
|
||||
// case notLikeRight -> queryWrapper.notLikeRight(queryAnnotation.column(), fieldValue);
|
||||
// case isNull -> queryWrapper.isNull(queryAnnotation.column());
|
||||
// case isNotNull -> queryWrapper.isNotNull(queryAnnotation.column());
|
||||
// case in -> queryWrapper.in(queryAnnotation.column(), (Object[]) fieldValue);
|
||||
// case notIn -> queryWrapper.notIn(queryAnnotation.column(), (Object[]) fieldValue);
|
||||
// case inSql -> queryWrapper.inSql(queryAnnotation.column(), queryAnnotation.sql());
|
||||
// case notInSql -> queryWrapper.notInSql(queryAnnotation.column(), queryAnnotation.sql());
|
||||
// default ->
|
||||
// throw new IllegalArgumentException(
|
||||
// "Unsupported operation: " + queryAnnotation.operation());
|
||||
// }
|
||||
// }
|
||||
// } catch (IllegalAccessException e) {
|
||||
// throw new RuntimeException("Failed to access field for building query conditions.", e);
|
||||
// }
|
||||
// });
|
||||
|
||||
// return queryWrapper;
|
||||
// }
|
||||
// }
|
||||
return queryWrapper;
|
||||
}
|
||||
}
|
||||
|
33
ruoyi-plugins/ruoyi-mybatis-plus/pom.xml
Normal file
33
ruoyi-plugins/ruoyi-mybatis-plus/pom.xml
Normal file
@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>ruoyi-plugins</artifactId>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<version>3.8.7.3.4</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>ruoyi-mybatis-plus</artifactId>
|
||||
|
||||
<description>
|
||||
ruoyi-mybatis-plus模块
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- 通用工具-->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- mybatis-plus 增强CRUD -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -1,13 +1,14 @@
|
||||
package com.ruoyi.framework.config;
|
||||
package com.ruoyi.mybatisplus.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* Mybatis Plus 配置
|
@ -39,6 +39,12 @@
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-mybatis-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- mybatis-plus -->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-mybatis-plus</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
Loading…
Reference in New Issue
Block a user