mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat:添加 selectOneByQueryAs 等方法,方便在 left join 等场景直接转换为 dto vo 等; close #I72XOK
This commit is contained in:
parent
9917b4ed62
commit
c1c93487d4
@ -323,6 +323,19 @@ public interface BaseMapper<T> {
|
|||||||
return (entities == null || entities.isEmpty()) ? null : entities.get(0);
|
return (entities == null || entities.isEmpty()) ? null : entities.get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据 queryWrapper 构建的条件来查询 1 条数据
|
||||||
|
*
|
||||||
|
* @param queryWrapper query 条件
|
||||||
|
* @param asType 接收类型
|
||||||
|
* @return 数据内容
|
||||||
|
*/
|
||||||
|
default <R> R selectOneByQueryAs(QueryWrapper queryWrapper, Class<R> asType) {
|
||||||
|
List<R> entities = selectListByQueryAs(queryWrapper.limit(1), asType);
|
||||||
|
return (entities == null || entities.isEmpty()) ? null : entities.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据多个主键来查询多条数据
|
* 根据多个主键来查询多条数据
|
||||||
*
|
*
|
||||||
@ -390,6 +403,17 @@ public interface BaseMapper<T> {
|
|||||||
List<T> selectListByQuery(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper);
|
List<T> selectListByQuery(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据 query 来构建条件查询数据列表,要求返回的数据为 asType
|
||||||
|
* 这种场景一般用在 left join 时,有多出了 entity 本身的字段内容,可以转换为 dto、vo 等场景时
|
||||||
|
*
|
||||||
|
* @param queryWrapper 查询条件
|
||||||
|
* @param asType 接收数据类型
|
||||||
|
* @return 数据列表
|
||||||
|
*/
|
||||||
|
@SelectProvider(type = EntitySqlProvider.class, method = "selectListByQuery")
|
||||||
|
<R> List<R> selectListByQueryAs(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper, Class<R> asType);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询全部数据
|
* 查询全部数据
|
||||||
*
|
*
|
||||||
@ -545,10 +569,44 @@ public interface BaseMapper<T> {
|
|||||||
* @return page 数据
|
* @return page 数据
|
||||||
*/
|
*/
|
||||||
default Page<T> paginate(Page<T> page, QueryWrapper queryWrapper) {
|
default Page<T> paginate(Page<T> page, QueryWrapper queryWrapper) {
|
||||||
|
|
||||||
List<QueryColumn> groupByColumns = CPI.getGroupByColumns(queryWrapper);
|
List<QueryColumn> groupByColumns = CPI.getGroupByColumns(queryWrapper);
|
||||||
List<QueryColumn> selectColumns = CPI.getSelectColumns(queryWrapper);
|
List<QueryColumn> selectColumns = CPI.getSelectColumns(queryWrapper);
|
||||||
|
|
||||||
|
// 只有 totalRow 小于 0 的时候才会去查询总量
|
||||||
|
// 这样方便用户做总数缓存,而非每次都要去查询总量
|
||||||
|
// 一般的分页场景中,只有第一页的时候有必要去查询总量,第二页以后是不需要的
|
||||||
|
if (page.getTotalRow() < 0) {
|
||||||
|
|
||||||
|
//清除group by 去查询数据
|
||||||
|
CPI.setGroupByColumns(queryWrapper, null);
|
||||||
|
CPI.setSelectColumns(queryWrapper, Arrays.asList(count()));
|
||||||
|
|
||||||
|
long count = selectCountByQuery(queryWrapper);
|
||||||
|
page.setTotalRow(count);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (page.getTotalRow() == 0 || page.getPageNumber() > page.getTotalPage()) {
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//恢复数量查询清除的 groupBy
|
||||||
|
CPI.setGroupByColumns(queryWrapper, groupByColumns);
|
||||||
|
|
||||||
|
//重置 selectColumns
|
||||||
|
CPI.setSelectColumns(queryWrapper, selectColumns);
|
||||||
|
|
||||||
|
int offset = page.getPageSize() * (page.getPageNumber() - 1);
|
||||||
|
queryWrapper.limit(offset, page.getPageSize());
|
||||||
|
List<T> rows = selectListByQuery(queryWrapper);
|
||||||
|
page.setRecords(rows);
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
default <R> Page<R> paginateAs(Page<R> page, QueryWrapper queryWrapper, Class<R> asType) {
|
||||||
|
List<QueryColumn> groupByColumns = CPI.getGroupByColumns(queryWrapper);
|
||||||
|
List<QueryColumn> selectColumns = CPI.getSelectColumns(queryWrapper);
|
||||||
|
|
||||||
// 只有 totalRow 小于 0 的时候才会去查询总量
|
// 只有 totalRow 小于 0 的时候才会去查询总量
|
||||||
// 这样方便用户做总数缓存,而非每次都要去查询总量
|
// 这样方便用户做总数缓存,而非每次都要去查询总量
|
||||||
@ -575,8 +633,8 @@ public interface BaseMapper<T> {
|
|||||||
|
|
||||||
int offset = page.getPageSize() * (page.getPageNumber() - 1);
|
int offset = page.getPageSize() * (page.getPageNumber() - 1);
|
||||||
queryWrapper.limit(offset, page.getPageSize());
|
queryWrapper.limit(offset, page.getPageSize());
|
||||||
List<T> rows = selectListByQuery(queryWrapper);
|
List<R> records = selectListByQueryAs(queryWrapper, asType);
|
||||||
page.setRecords(rows);
|
page.setRecords(records);
|
||||||
return page;
|
return page;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -39,6 +39,7 @@ public class FlexConsts {
|
|||||||
public static final String IGNORE_NULLS = "$$ignoreNulls";
|
public static final String IGNORE_NULLS = "$$ignoreNulls";
|
||||||
|
|
||||||
public static final String METHOD_INSERT_BATCH = "insertBatch";
|
public static final String METHOD_INSERT_BATCH = "insertBatch";
|
||||||
|
public static final String METHOD_SELECT_LIST_BY_QUERY_AS = "selectListByQueryAs";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 当 entity 使用逻辑删除时,0 为 entity 的正常状态
|
* 当 entity 使用逻辑删除时,0 为 entity 的正常状态
|
||||||
|
|||||||
@ -126,6 +126,20 @@ public class FlexConfiguration extends Configuration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MappedStatement getMappedStatement(String id) {
|
||||||
|
MappedStatement ms = super.getMappedStatement(id);
|
||||||
|
|
||||||
|
//动态 resultsMap
|
||||||
|
if (id.endsWith(FlexConsts.METHOD_SELECT_LIST_BY_QUERY_AS)) {
|
||||||
|
Class<?> asType = MappedStatementTypes.getCurrentType();
|
||||||
|
ms = replaceResultMap(ms, TableInfoFactory.ofEntityClass(asType));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addMappedStatement(MappedStatement ms) {
|
public void addMappedStatement(MappedStatement ms) {
|
||||||
//替换 RowMapper.insert 的主键生成器
|
//替换 RowMapper.insert 的主键生成器
|
||||||
@ -141,7 +155,7 @@ public class FlexConfiguration extends Configuration {
|
|||||||
//entity select
|
//entity select
|
||||||
else if (StringUtil.endsWithAny(ms.getId(), "selectOneById", "selectListByIds"
|
else if (StringUtil.endsWithAny(ms.getId(), "selectOneById", "selectListByIds"
|
||||||
, "selectListByQuery")) {
|
, "selectListByQuery")) {
|
||||||
ms = replaceResultMap(ms);
|
ms = replaceResultMap(ms, getTableInfo(ms));
|
||||||
}
|
}
|
||||||
|
|
||||||
super.addMappedStatement(ms);
|
super.addMappedStatement(ms);
|
||||||
@ -151,9 +165,8 @@ public class FlexConfiguration extends Configuration {
|
|||||||
/**
|
/**
|
||||||
* 替换 entity 查询的 ResultMap
|
* 替换 entity 查询的 ResultMap
|
||||||
*/
|
*/
|
||||||
private MappedStatement replaceResultMap(MappedStatement ms) {
|
private MappedStatement replaceResultMap(MappedStatement ms, TableInfo tableInfo) {
|
||||||
|
|
||||||
TableInfo tableInfo = getTableInfo(ms);
|
|
||||||
if (tableInfo == null) {
|
if (tableInfo == null) {
|
||||||
return ms;
|
return ms;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,34 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
|
* <p>
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* <p>
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* <p>
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.mybatisflex.core.mybatis;
|
||||||
|
|
||||||
|
public class MappedStatementTypes {
|
||||||
|
|
||||||
|
private static ThreadLocal<Class<?>> currentTypeTL = new ThreadLocal<>();
|
||||||
|
|
||||||
|
static void setCurrentType(Class<?> type){
|
||||||
|
currentTypeTL.set(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Class<?> getCurrentType(){
|
||||||
|
return currentTypeTL.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void clear(){
|
||||||
|
currentTypeTL.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -16,6 +16,7 @@
|
|||||||
package com.mybatisflex.core.mybatis;
|
package com.mybatisflex.core.mybatis;
|
||||||
|
|
||||||
import com.mybatisflex.annotation.UseDataSource;
|
import com.mybatisflex.annotation.UseDataSource;
|
||||||
|
import com.mybatisflex.core.FlexConsts;
|
||||||
import com.mybatisflex.core.FlexGlobalConfig;
|
import com.mybatisflex.core.FlexGlobalConfig;
|
||||||
import com.mybatisflex.core.datasource.DataSourceKey;
|
import com.mybatisflex.core.datasource.DataSourceKey;
|
||||||
import com.mybatisflex.core.datasource.FlexDataSource;
|
import com.mybatisflex.core.datasource.FlexDataSource;
|
||||||
@ -45,7 +46,11 @@ public class MapperInvocationHandler implements InvocationHandler {
|
|||||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||||
boolean clearDsKey = false;
|
boolean clearDsKey = false;
|
||||||
boolean clearDbType = false;
|
boolean clearDbType = false;
|
||||||
|
boolean isSelectListByQueryAsMethod = FlexConsts.METHOD_SELECT_LIST_BY_QUERY_AS.equals(method.getName());
|
||||||
try {
|
try {
|
||||||
|
if (isSelectListByQueryAsMethod){
|
||||||
|
MappedStatementTypes.setCurrentType((Class<?>) args[1]);
|
||||||
|
}
|
||||||
//获取用户动态指定,由用户指定数据源,则应该有用户清除
|
//获取用户动态指定,由用户指定数据源,则应该有用户清除
|
||||||
String dataSourceKey = DataSourceKey.get();
|
String dataSourceKey = DataSourceKey.get();
|
||||||
|
|
||||||
@ -73,6 +78,9 @@ public class MapperInvocationHandler implements InvocationHandler {
|
|||||||
}
|
}
|
||||||
return method.invoke(mapper, args);
|
return method.invoke(mapper, args);
|
||||||
} finally {
|
} finally {
|
||||||
|
if (isSelectListByQueryAsMethod){
|
||||||
|
MappedStatementTypes.clear();
|
||||||
|
}
|
||||||
if (clearDbType) {
|
if (clearDbType) {
|
||||||
DialectFactory.clearHintDbType();
|
DialectFactory.clearHintDbType();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -117,7 +117,6 @@ public class TableInfoFactory {
|
|||||||
tableInfo.setEntityClass(entityClass);
|
tableInfo.setEntityClass(entityClass);
|
||||||
tableInfo.setReflector(new Reflector(entityClass));
|
tableInfo.setReflector(new Reflector(entityClass));
|
||||||
|
|
||||||
|
|
||||||
//初始化表名
|
//初始化表名
|
||||||
Table table = entityClass.getAnnotation(Table.class);
|
Table table = entityClass.getAnnotation(Table.class);
|
||||||
if (table != null) {
|
if (table != null) {
|
||||||
|
|||||||
@ -0,0 +1,90 @@
|
|||||||
|
package com.mybatisflex.test;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class ArticleDTO {
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private Long accountId;
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
|
||||||
|
private String userName;
|
||||||
|
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
private Date birthday;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getAccountId() {
|
||||||
|
return accountId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAccountId(Long accountId) {
|
||||||
|
this.accountId = accountId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(String content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getBirthday() {
|
||||||
|
return birthday;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBirthday(Date birthday) {
|
||||||
|
this.birthday = birthday;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ArticleDTO{" +
|
||||||
|
"id=" + id +
|
||||||
|
", accountId=" + accountId +
|
||||||
|
", title='" + title + '\'' +
|
||||||
|
", content='" + content + '\'' +
|
||||||
|
", userName='" + userName + '\'' +
|
||||||
|
", age=" + age +
|
||||||
|
", birthday=" + birthday +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -28,6 +28,7 @@ import javax.sql.DataSource;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static com.mybatisflex.test.table.Tables.ACCOUNT;
|
import static com.mybatisflex.test.table.Tables.ACCOUNT;
|
||||||
|
import static com.mybatisflex.test.table.Tables.ARTICLE;
|
||||||
|
|
||||||
public class EntityTestStarter {
|
public class EntityTestStarter {
|
||||||
|
|
||||||
@ -81,6 +82,16 @@ public class EntityTestStarter {
|
|||||||
Object object = accountMapper.selectObjectByQuery(wrapper2);
|
Object object = accountMapper.selectObjectByQuery(wrapper2);
|
||||||
System.out.println(object);
|
System.out.println(object);
|
||||||
|
|
||||||
|
QueryWrapper asWrapper = QueryWrapper.create()
|
||||||
|
.select(ARTICLE.ALL_COLUMNS)
|
||||||
|
.select(ACCOUNT.USER_NAME,ACCOUNT.AGE,ACCOUNT.BIRTHDAY)
|
||||||
|
.from(ARTICLE)
|
||||||
|
.leftJoin(ACCOUNT).on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID))
|
||||||
|
.where(ACCOUNT.ID.ge(0));
|
||||||
|
|
||||||
|
List<ArticleDTO> articleDTOS = accountMapper.selectListByQueryAs(asWrapper, ArticleDTO.class);
|
||||||
|
System.out.println(articleDTOS);
|
||||||
|
|
||||||
|
|
||||||
// QueryWrapper queryWrapper = new QueryWrapper();
|
// QueryWrapper queryWrapper = new QueryWrapper();
|
||||||
// queryWrapper.where(ACCOUNT.ID.in(
|
// queryWrapper.where(ACCOUNT.ID.in(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user