mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
commit
19ca5f9373
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
在很多场景下,我们可能会用到 `一对多`、`一对一`、`多对一`、`多对多`等场景的关联查询,MyBatis-Flex 内置了相关的方法,用于支持此类场景。
|
在很多场景下,我们可能会用到 `一对多`、`一对一`、`多对一`、`多对多`等场景的关联查询,MyBatis-Flex 内置了相关的方法,用于支持此类场景。
|
||||||
|
|
||||||
## 代码示例(多对多)
|
## 代码示例(Field Query)
|
||||||
|
|
||||||
以下是文章的示例,一篇文章可能归属于多个分类,一个类可以有多篇文章,需要用到中间表 `article_category_mapping`。
|
以下是文章的示例,一篇文章可能归属于多个分类,一个类可以有多篇文章,需要用到中间表 `article_category_mapping`。
|
||||||
|
|
||||||
@ -104,4 +104,100 @@ List<Article> articles = mapper.selectListByQuery(query
|
|||||||
## 更多场景
|
## 更多场景
|
||||||
|
|
||||||
通过以上内容看出,`Article` 的任何属性,都是可以通过传入 `FieldQueryBuilder` 来构建 `QueryWrapper` 进行再次查询,
|
通过以上内容看出,`Article` 的任何属性,都是可以通过传入 `FieldQueryBuilder` 来构建 `QueryWrapper` 进行再次查询,
|
||||||
这些不仅仅只适用于 `一对多`、`一对一`、`多对一`、`多对多`等场景。任何 `Article` 对象里的属性,需要二次查询赋值的,都是可以通过这种方式进行。
|
这些不仅仅只适用于 `一对多`、`一对一`、`多对一`、`多对多`等场景。任何 `Article` 对象里的属性,需要二次查询赋值的,都是可以通过这种方式进行。
|
||||||
|
|
||||||
|
## 结果映射
|
||||||
|
|
||||||
|
您也可以继续使用联表查询,如果是原生 MyBatis 的话,可以使用 `<resultMap>` 标签来构建结果映射,在 MyBatis-Flex 中提供了自动结果映射功能,这样您就可以只关注于 SQL 语句的构建。
|
||||||
|
|
||||||
|
## 代码示例(Join Query)
|
||||||
|
|
||||||
|
这里以用户和角色的 `多对多` 关系作为例子,首先有用户表和角色表,分别对应着用户类和角色类:
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Table("sys_user")
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private Integer userId;
|
||||||
|
private String userName;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Table("sys_role")
|
||||||
|
public class Role {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private Integer roleId;
|
||||||
|
private String roleKey;
|
||||||
|
private String roleName;
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
现在需要查询所有用户,以及每个用户对应的角色信息,并通过 UserVO 对象返回:
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class UserVO {
|
||||||
|
|
||||||
|
private String userId;
|
||||||
|
private String userName;
|
||||||
|
private List<Role> roleList;
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
这个操作只需要联表查询即可完成,对于联表查询的结果映射,MyBatis-Flex 会自动帮您完成:
|
||||||
|
|
||||||
|
```java
|
||||||
|
@SpringBootTest
|
||||||
|
class UserMapperTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSelectList() {
|
||||||
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
|
.select(USER.USER_ID, USER.USER_NAME, ROLE.ALL_COLUMNS)
|
||||||
|
.from(USER.as("u"))
|
||||||
|
.leftJoin(USER_ROLE).as("ur").on(USER_ROLE.USER_ID.eq(USER.USER_ID))
|
||||||
|
.leftJoin(ROLE).as("r").on(USER_ROLE.ROLE_ID.eq(ROLE.ROLE_ID));
|
||||||
|
List<UserVO> userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO.class);
|
||||||
|
userVOS.forEach(System.err::println);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
构建的联表查询 SQL 语句为:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
SELECT `u`.`user_id`,
|
||||||
|
`u`.`user_name`,
|
||||||
|
`r`.*
|
||||||
|
FROM `sys_user` AS `u`
|
||||||
|
LEFT JOIN `sys_user_role` AS `ur` ON `ur`.`user_id` = `u`.`user_id`
|
||||||
|
LEFT JOIN `sys_role` AS `r` ON `ur`.`role_id` = `r`.`role_id`;
|
||||||
|
```
|
||||||
|
|
||||||
|
最终自动映射的结果为:
|
||||||
|
|
||||||
|
```text
|
||||||
|
UserVO{userId='1', userName='admin', roleList=[Role{roleId=1, roleKey='admin', roleName='超级管理员'}]}
|
||||||
|
UserVO{userId='2', userName='ry', roleList=[Role{roleId=2, roleKey='common', roleName='普通角色'}]}
|
||||||
|
UserVO{userId='3', userName='test', roleList=[Role{roleId=1, roleKey='admin', roleName='超级管理员'}, Role{roleId=2, roleKey='common', roleName='普通角色'}]}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 特别注意!!!
|
||||||
|
|
||||||
|
使用 `join` 联表查询的时候,只能使用 `selectListXxx` 方法查询 List 数据,不能使用 `selectOneXxx` 方法查询单个数据。
|
||||||
|
|
||||||
|
```java
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
因为这个 `selectOneXxx` 方法都是调用的对应的 `selectListXxx` 方法,其中添加了 `queryWrapper.limit(1)` 约束,将数据限制在了一条,而联表查询数据有可能是多条的,这样自动映射就会出现数据丢失。
|
||||||
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
* <p>
|
* <p>
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
* <p>
|
* <p>
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* <p>
|
* <p>
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.mybatisflex.core.mybatis;
|
package com.mybatisflex.core.mybatis;
|
||||||
|
|
||||||
@ -27,7 +27,6 @@ import com.mybatisflex.core.row.RowMapper;
|
|||||||
import com.mybatisflex.core.table.EntityWrapperFactory;
|
import com.mybatisflex.core.table.EntityWrapperFactory;
|
||||||
import com.mybatisflex.core.table.TableInfo;
|
import com.mybatisflex.core.table.TableInfo;
|
||||||
import com.mybatisflex.core.table.TableInfoFactory;
|
import com.mybatisflex.core.table.TableInfoFactory;
|
||||||
import com.mybatisflex.core.util.CollectionUtil;
|
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
import org.apache.ibatis.executor.CachingExecutor;
|
import org.apache.ibatis.executor.CachingExecutor;
|
||||||
import org.apache.ibatis.executor.Executor;
|
import org.apache.ibatis.executor.Executor;
|
||||||
@ -36,15 +35,15 @@ import org.apache.ibatis.executor.keygen.NoKeyGenerator;
|
|||||||
import org.apache.ibatis.executor.keygen.SelectKeyGenerator;
|
import org.apache.ibatis.executor.keygen.SelectKeyGenerator;
|
||||||
import org.apache.ibatis.executor.parameter.ParameterHandler;
|
import org.apache.ibatis.executor.parameter.ParameterHandler;
|
||||||
import org.apache.ibatis.executor.statement.StatementHandler;
|
import org.apache.ibatis.executor.statement.StatementHandler;
|
||||||
import org.apache.ibatis.mapping.BoundSql;
|
import org.apache.ibatis.mapping.*;
|
||||||
import org.apache.ibatis.mapping.Environment;
|
|
||||||
import org.apache.ibatis.mapping.MappedStatement;
|
|
||||||
import org.apache.ibatis.mapping.ResultMap;
|
|
||||||
import org.apache.ibatis.session.*;
|
import org.apache.ibatis.session.*;
|
||||||
import org.apache.ibatis.transaction.Transaction;
|
import org.apache.ibatis.transaction.Transaction;
|
||||||
import org.apache.ibatis.util.MapUtil;
|
import org.apache.ibatis.util.MapUtil;
|
||||||
|
|
||||||
import java.lang.reflect.Proxy;
|
import java.lang.reflect.Proxy;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
@ -178,14 +177,40 @@ public class FlexConfiguration extends Configuration {
|
|||||||
|
|
||||||
String resultMapId = tableInfo.getEntityClass().getName();
|
String resultMapId = tableInfo.getEntityClass().getName();
|
||||||
|
|
||||||
ResultMap resultMap;
|
/*ResultMap resultMap;
|
||||||
if (hasResultMap(resultMapId)) {
|
if (hasResultMap(resultMapId)) {
|
||||||
resultMap = getResultMap(resultMapId);
|
resultMap = getResultMap(resultMapId);
|
||||||
} else {
|
} else {
|
||||||
resultMap = tableInfo.buildResultMap(this);
|
resultMap = tableInfo.buildResultMap(this);
|
||||||
this.addResultMap(resultMap);
|
this.addResultMap(resultMap);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
// 变量名与属性名区分开
|
||||||
|
List<ResultMap> resultMapList;
|
||||||
|
if (hasResultMap(resultMapId)) {
|
||||||
|
resultMapList = new ArrayList<>();
|
||||||
|
fillResultMapList(resultMapId, resultMapList);
|
||||||
|
} else {
|
||||||
|
resultMapList = tableInfo.buildResultMapList(this);
|
||||||
|
for (ResultMap resultMap : resultMapList) {
|
||||||
|
if (!hasResultMap(resultMap.getId())) {
|
||||||
|
addResultMap(resultMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 这里解释一下为什么要反转这个集合:
|
||||||
|
*
|
||||||
|
* MyBatis 在解析 ResultMaps 的时候,是按照顺序一个一个进行解析的,对于有嵌套
|
||||||
|
* 的 ResultMap 对象,也就是 nestResultMap 需要放在靠前的位置,首先解析。
|
||||||
|
*
|
||||||
|
* 而我们进行递归 buildResultMapList 也好,fillResultMapList 也好,都是
|
||||||
|
* 非嵌套 ResultMap 在集合最开始的位置,所以要反转一下集合,将 hasNestedResultMaps
|
||||||
|
* 的 ResultMap 对象放到集合的最前面。
|
||||||
|
*/
|
||||||
|
Collections.reverse(resultMapList);
|
||||||
|
|
||||||
return new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), ms.getSqlSource(), ms.getSqlCommandType())
|
return new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), ms.getSqlSource(), ms.getSqlCommandType())
|
||||||
.resource(ms.getResource())
|
.resource(ms.getResource())
|
||||||
.fetchSize(ms.getFetchSize())
|
.fetchSize(ms.getFetchSize())
|
||||||
@ -198,7 +223,8 @@ public class FlexConfiguration extends Configuration {
|
|||||||
.lang(ms.getLang())
|
.lang(ms.getLang())
|
||||||
.resultOrdered(ms.isResultOrdered())
|
.resultOrdered(ms.isResultOrdered())
|
||||||
.resultSets(ms.getResultSets() == null ? null : String.join(",", ms.getResultSets()))
|
.resultSets(ms.getResultSets() == null ? null : String.join(",", ms.getResultSets()))
|
||||||
.resultMaps(CollectionUtil.newArrayList(resultMap)) // 替换resultMap
|
//.resultMaps(CollectionUtil.newArrayList(resultMap)) // 替换resultMap
|
||||||
|
.resultMaps(resultMapList)
|
||||||
.resultSetType(ms.getResultSetType())
|
.resultSetType(ms.getResultSetType())
|
||||||
.flushCacheRequired(ms.isFlushCacheRequired())
|
.flushCacheRequired(ms.isFlushCacheRequired())
|
||||||
.useCache(ms.isUseCache())
|
.useCache(ms.isUseCache())
|
||||||
@ -206,6 +232,19 @@ public class FlexConfiguration extends Configuration {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void fillResultMapList(String resultMapId, List<ResultMap> resultMapList) {
|
||||||
|
ResultMap resultMap = this.getResultMap(resultMapId);
|
||||||
|
resultMapList.add(resultMap);
|
||||||
|
if (resultMap.hasNestedResultMaps()) {
|
||||||
|
for (ResultMapping resultMapping : resultMap.getResultMappings()) {
|
||||||
|
String nestedResultMapId = resultMapping.getNestedResultMapId();
|
||||||
|
if (nestedResultMapId != null) {
|
||||||
|
fillResultMapList(nestedResultMapId, resultMapList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成新的、已替换主键生成器的 MappedStatement
|
* 生成新的、已替换主键生成器的 MappedStatement
|
||||||
*
|
*
|
||||||
|
|||||||
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
* <p>
|
* <p>
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
* <p>
|
* <p>
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* <p>
|
* <p>
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.mybatisflex.core.table;
|
package com.mybatisflex.core.table;
|
||||||
|
|
||||||
@ -39,6 +39,7 @@ import org.apache.ibatis.session.Configuration;
|
|||||||
import org.apache.ibatis.type.TypeHandler;
|
import org.apache.ibatis.type.TypeHandler;
|
||||||
import org.apache.ibatis.util.MapUtil;
|
import org.apache.ibatis.util.MapUtil;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Proxy;
|
import java.lang.reflect.Proxy;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@ -96,9 +97,24 @@ public class TableInfo {
|
|||||||
private List<UpdateListener> onUpdateListeners;
|
private List<UpdateListener> onUpdateListeners;
|
||||||
private List<SetListener> onSetListeners;
|
private List<SetListener> onSetListeners;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated 该功能有更好的方式实现,此属性可能会被移除。
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
private Map<String, Class<?>> joinTypes;
|
private Map<String, Class<?>> joinTypes;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对应 MapperXML 配置文件中 {@code <resultMap>} 标签下的 {@code <association>} 标签。
|
||||||
|
*/
|
||||||
|
private Map<String, Class<?>> associationType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对应 MapperXML 配置文件中 {@code <resultMap>} 标签下的 {@code <collection>} 标签。
|
||||||
|
*/
|
||||||
|
private Map<Field, Class<?>> collectionType;
|
||||||
|
|
||||||
|
|
||||||
private final ReflectorFactory reflectorFactory = new BaseReflectorFactory() {
|
private final ReflectorFactory reflectorFactory = new BaseReflectorFactory() {
|
||||||
@Override
|
@Override
|
||||||
public Reflector findForClass(Class<?> type) {
|
public Reflector findForClass(Class<?> type) {
|
||||||
@ -119,11 +135,11 @@ public class TableInfo {
|
|||||||
return tableName;
|
return tableName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getWrapSchemaAndTableName(IDialect dialect){
|
public String getWrapSchemaAndTableName(IDialect dialect) {
|
||||||
if (StringUtil.isNotBlank(schema)){
|
if (StringUtil.isNotBlank(schema)) {
|
||||||
return dialect.wrap(dialect.getRealSchema(schema)) +"." + dialect.wrap(dialect.getRealTable(tableName));
|
return dialect.wrap(dialect.getRealSchema(schema)) + "." + dialect.wrap(dialect.getRealTable(tableName));
|
||||||
}else {
|
} else {
|
||||||
return dialect.wrap(dialect.getRealTable(tableName));
|
return dialect.wrap(dialect.getRealTable(tableName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -296,6 +312,36 @@ public class TableInfo {
|
|||||||
joinTypes.put(fieldName, clazz);
|
joinTypes.put(fieldName, clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<String, Class<?>> getAssociationType() {
|
||||||
|
return associationType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAssociationType(Map<String, Class<?>> associationType) {
|
||||||
|
this.associationType = associationType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addAssociationType(String fieldName, Class<?> clazz) {
|
||||||
|
if (associationType == null) {
|
||||||
|
associationType = new HashMap<>();
|
||||||
|
}
|
||||||
|
associationType.put(fieldName, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Field, Class<?>> getCollectionType() {
|
||||||
|
return collectionType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCollectionType(Map<Field, Class<?>> collectionType) {
|
||||||
|
this.collectionType = collectionType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addCollectionType(Field field, Class<?> genericClass) {
|
||||||
|
if (collectionType == null) {
|
||||||
|
collectionType = new HashMap<>();
|
||||||
|
}
|
||||||
|
collectionType.put(field, genericClass);
|
||||||
|
}
|
||||||
|
|
||||||
void setColumnInfoList(List<ColumnInfo> columnInfoList) {
|
void setColumnInfoList(List<ColumnInfo> columnInfoList) {
|
||||||
this.columnInfoList = columnInfoList;
|
this.columnInfoList = columnInfoList;
|
||||||
this.columns = new String[columnInfoList.size()];
|
this.columns = new String[columnInfoList.size()];
|
||||||
@ -660,6 +706,10 @@ public class TableInfo {
|
|||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated 该功能有更好的方式实现,此方法可能会被移除。
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public ResultMap buildResultMap(Configuration configuration) {
|
public ResultMap buildResultMap(Configuration configuration) {
|
||||||
String resultMapId = entityClass.getName();
|
String resultMapId = entityClass.getName();
|
||||||
List<ResultMapping> resultMappings = new ArrayList<>();
|
List<ResultMapping> resultMappings = new ArrayList<>();
|
||||||
@ -727,6 +777,90 @@ public class TableInfo {
|
|||||||
return new ResultMap.Builder(configuration, resultMapId, entityClass, resultMappings).build();
|
return new ResultMap.Builder(configuration, resultMapId, entityClass, resultMappings).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<ResultMap> buildResultMapList(Configuration configuration) {
|
||||||
|
String resultMapId = entityClass.getName();
|
||||||
|
List<ResultMap> resultMaps = new ArrayList<>();
|
||||||
|
List<ResultMapping> resultMappings = new ArrayList<>();
|
||||||
|
|
||||||
|
// <resultMap> 标签下的 <result> 标签映射
|
||||||
|
for (ColumnInfo columnInfo : columnInfoList) {
|
||||||
|
ResultMapping mapping = new ResultMapping.Builder(configuration, columnInfo.property,
|
||||||
|
columnInfo.column, columnInfo.propertyType)
|
||||||
|
.jdbcType(columnInfo.getJdbcType())
|
||||||
|
.typeHandler(columnInfo.buildTypeHandler())
|
||||||
|
.build();
|
||||||
|
resultMappings.add(mapping);
|
||||||
|
|
||||||
|
//add property mapper for sql: select xxx as property ...
|
||||||
|
if (!Objects.equals(columnInfo.getColumn(), columnInfo.getProperty())) {
|
||||||
|
ResultMapping propertyMapping = new ResultMapping.Builder(configuration, columnInfo.property,
|
||||||
|
columnInfo.property, columnInfo.propertyType)
|
||||||
|
.jdbcType(columnInfo.getJdbcType())
|
||||||
|
.typeHandler(columnInfo.buildTypeHandler())
|
||||||
|
.build();
|
||||||
|
resultMappings.add(propertyMapping);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// <resultMap> 标签下的 <id> 标签映射
|
||||||
|
for (IdInfo idInfo : primaryKeyList) {
|
||||||
|
ResultMapping mapping = new ResultMapping.Builder(configuration, idInfo.property,
|
||||||
|
idInfo.column, idInfo.propertyType)
|
||||||
|
.flags(CollectionUtil.newArrayList(ResultFlag.ID))
|
||||||
|
.jdbcType(idInfo.getJdbcType())
|
||||||
|
.typeHandler(idInfo.buildTypeHandler())
|
||||||
|
.build();
|
||||||
|
resultMappings.add(mapping);
|
||||||
|
}
|
||||||
|
|
||||||
|
// <resultMap> 标签下的 <association> 标签映射
|
||||||
|
if (associationType != null) {
|
||||||
|
associationType.forEach((fieldName, fieldType) -> {
|
||||||
|
// 获取嵌套类型的信息,也就是 javaType 属性
|
||||||
|
TableInfo tableInfo = TableInfoFactory.ofEntityClass(fieldType);
|
||||||
|
// 构建嵌套类型的 ResultMap 对象,也就是 <association> 标签下的内容
|
||||||
|
// 这里是递归调用,直到嵌套类型里面没有其他嵌套类型或者集合类型为止
|
||||||
|
List<ResultMap> resultMapList = tableInfo.buildResultMapList(configuration);
|
||||||
|
// 寻找是否有嵌套 ResultMap 引用
|
||||||
|
Optional<ResultMap> nestedResultMap = resultMapList.stream()
|
||||||
|
.filter(e -> fieldType.getName().equals(e.getId()))
|
||||||
|
.findFirst();
|
||||||
|
// 处理嵌套类型 ResultMapping 引用
|
||||||
|
nestedResultMap.ifPresent(resultMap -> resultMappings.add(new ResultMapping.Builder(configuration, fieldName)
|
||||||
|
.javaType(fieldType)
|
||||||
|
.nestedResultMapId(resultMap.getId())
|
||||||
|
.build()));
|
||||||
|
// 全部添加到 ResultMap 集合当中
|
||||||
|
resultMaps.addAll(resultMapList);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// <resultMap> 标签下的 <collection> 标签映射
|
||||||
|
if (collectionType != null) {
|
||||||
|
collectionType.forEach((field, genericClass) -> {
|
||||||
|
// 获取集合泛型类型的信息,也就是 ofType 属性
|
||||||
|
TableInfo tableInfo = TableInfoFactory.ofEntityClass(genericClass);
|
||||||
|
// 构建嵌套类型的 ResultMap 对象,也就是 <collection> 标签下的内容
|
||||||
|
// 这里是递归调用,直到集合类型里面没有其他嵌套类型或者集合类型为止
|
||||||
|
List<ResultMap> resultMapList = tableInfo.buildResultMapList(configuration);
|
||||||
|
// 寻找是否有嵌套 ResultMap 引用
|
||||||
|
Optional<ResultMap> nestedResultMap = resultMapList.stream()
|
||||||
|
.filter(e -> genericClass.getName().equals(e.getId()))
|
||||||
|
.findFirst();
|
||||||
|
// 处理嵌套类型 ResultMapping 引用
|
||||||
|
nestedResultMap.ifPresent(resultMap -> resultMappings.add(new ResultMapping.Builder(configuration, field.getName())
|
||||||
|
.javaType(field.getType())
|
||||||
|
.nestedResultMapId(resultMap.getId())
|
||||||
|
.build()));
|
||||||
|
// 全部添加到 ResultMap 集合当中
|
||||||
|
resultMaps.addAll(resultMapList);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
resultMaps.add(new ResultMap.Builder(configuration, resultMapId, entityClass, resultMappings).build());
|
||||||
|
|
||||||
|
return resultMaps;
|
||||||
|
}
|
||||||
|
|
||||||
private static boolean existColumn(List<ResultMapping> resultMappings, String name) {
|
private static boolean existColumn(List<ResultMapping> resultMappings, String name) {
|
||||||
for (ResultMapping resultMapping : resultMappings) {
|
for (ResultMapping resultMapping : resultMappings) {
|
||||||
|
|||||||
@ -212,11 +212,19 @@ public class TableInfoFactory {
|
|||||||
&& !fieldType.isEnum() // 类型不是枚举
|
&& !fieldType.isEnum() // 类型不是枚举
|
||||||
&& !defaultSupportColumnTypes.contains(fieldType) //默认的自动类型不包含该类型
|
&& !defaultSupportColumnTypes.contains(fieldType) //默认的自动类型不包含该类型
|
||||||
) {
|
) {
|
||||||
if (!Map.class.isAssignableFrom(fieldType)
|
// 集合嵌套
|
||||||
&& !Collection.class.isAssignableFrom(fieldType)
|
if (Collection.class.isAssignableFrom(fieldType)) {
|
||||||
&& !fieldType.isArray()) {
|
ParameterizedType genericType = (ParameterizedType) field.getGenericType();
|
||||||
tableInfo.addJoinType(field.getName(), fieldType);
|
Type actualTypeArgument = genericType.getActualTypeArguments()[0];
|
||||||
|
tableInfo.addCollectionType(field, (Class<?>) actualTypeArgument);
|
||||||
}
|
}
|
||||||
|
// 实体类嵌套
|
||||||
|
else if (!Map.class.isAssignableFrom(fieldType)
|
||||||
|
&& !fieldType.isArray()) {
|
||||||
|
// tableInfo.addJoinType(field.getName(), fieldType);
|
||||||
|
tableInfo.addAssociationType(field.getName(), fieldType);
|
||||||
|
}
|
||||||
|
// 不支持的类型直接跳过
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -45,13 +45,18 @@
|
|||||||
<artifactId>spring-boot-starter-data-jdbc</artifactId>
|
<artifactId>spring-boot-starter-data-jdbc</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.h2database</groupId>
|
<groupId>mysql</groupId>
|
||||||
<artifactId>h2</artifactId>
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
<version>2.1.214</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- <dependency>-->
|
||||||
|
<!-- <groupId>com.h2database</groupId>-->
|
||||||
|
<!-- <artifactId>h2</artifactId>-->
|
||||||
|
<!-- <version>2.1.214</version>-->
|
||||||
|
<!-- </dependency>-->
|
||||||
|
|
||||||
<!-- <dependency>-->
|
<!-- <dependency>-->
|
||||||
<!-- <groupId>org.yaml</groupId>-->
|
<!-- <groupId>org.yaml</groupId>-->
|
||||||
<!-- <artifactId>snakeyaml</artifactId>-->
|
<!-- <artifactId>snakeyaml</artifactId>-->
|
||||||
|
|||||||
@ -1,23 +1,23 @@
|
|||||||
/**
|
/*
|
||||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
* <p>
|
* <p>
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
* <p>
|
* <p>
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* <p>
|
* <p>
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.mybatisflex.test.model;
|
package com.mybatisflex.test.model;
|
||||||
|
|
||||||
import com.mybatisflex.annotation.Id;
|
import com.mybatisflex.annotation.Id;
|
||||||
import com.mybatisflex.annotation.Table;
|
|
||||||
import com.mybatisflex.annotation.KeyType;
|
import com.mybatisflex.annotation.KeyType;
|
||||||
|
import com.mybatisflex.annotation.Table;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.model;
|
||||||
|
|
||||||
|
import com.mybatisflex.annotation.Id;
|
||||||
|
import com.mybatisflex.annotation.Table;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品。
|
||||||
|
*
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-06-07
|
||||||
|
*/
|
||||||
|
@Table("tb_good")
|
||||||
|
public class Good {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private Integer goodId;
|
||||||
|
private String name;
|
||||||
|
private double price;
|
||||||
|
|
||||||
|
public Integer getGoodId() {
|
||||||
|
return goodId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGoodId(Integer goodId) {
|
||||||
|
this.goodId = goodId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrice(double price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Good{" +
|
||||||
|
"goodId=" + goodId +
|
||||||
|
", name='" + name + '\'' +
|
||||||
|
", price=" + price +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.model;
|
||||||
|
|
||||||
|
import com.mybatisflex.annotation.Id;
|
||||||
|
import com.mybatisflex.annotation.Table;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订单。
|
||||||
|
*
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-06-07
|
||||||
|
*/
|
||||||
|
@Table("tb_order")
|
||||||
|
public class Order {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private int orderId;
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
|
||||||
|
public int getOrderId() {
|
||||||
|
return orderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderId(int orderId) {
|
||||||
|
this.orderId = orderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getCreateTime() {
|
||||||
|
return createTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreateTime(LocalDateTime createTime) {
|
||||||
|
this.createTime = createTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Order{" +
|
||||||
|
"orderId=" + orderId +
|
||||||
|
", createTime=" + createTime +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.model;
|
||||||
|
|
||||||
|
import com.mybatisflex.annotation.Table;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订单与商品连接表。
|
||||||
|
*
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-06-07
|
||||||
|
*/
|
||||||
|
@Table("tb_order_good")
|
||||||
|
public class OrderGood {
|
||||||
|
|
||||||
|
private Integer orderId;
|
||||||
|
private Integer goodId;
|
||||||
|
|
||||||
|
public Integer getOrderId() {
|
||||||
|
return orderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderId(Integer orderId) {
|
||||||
|
this.orderId = orderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getGoodId() {
|
||||||
|
return goodId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGoodId(Integer goodId) {
|
||||||
|
this.goodId = goodId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.model;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订单信息。
|
||||||
|
*
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-06-07
|
||||||
|
*/
|
||||||
|
public class OrderInfo {
|
||||||
|
|
||||||
|
private Integer orderId;
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
|
||||||
|
private List<Good> goodList;
|
||||||
|
|
||||||
|
public Integer getOrderId() {
|
||||||
|
return orderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderId(Integer orderId) {
|
||||||
|
this.orderId = orderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getCreateTime() {
|
||||||
|
return createTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreateTime(LocalDateTime createTime) {
|
||||||
|
this.createTime = createTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Good> getGoodList() {
|
||||||
|
return goodList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGoodList(List<Good> goodList) {
|
||||||
|
this.goodList = goodList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "OrderInfo{" +
|
||||||
|
"orderId=" + orderId +
|
||||||
|
", createTime=" + createTime +
|
||||||
|
", goodList=" + goodList +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.model;
|
||||||
|
|
||||||
|
import com.mybatisflex.annotation.Id;
|
||||||
|
import com.mybatisflex.annotation.Table;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 角色。
|
||||||
|
*
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-06-07
|
||||||
|
*/
|
||||||
|
@Table("tb_role")
|
||||||
|
public class Role {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private Integer roleId;
|
||||||
|
private String roleKey;
|
||||||
|
private String roleName;
|
||||||
|
|
||||||
|
public Integer getRoleId() {
|
||||||
|
return roleId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoleId(Integer roleId) {
|
||||||
|
this.roleId = roleId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRoleKey() {
|
||||||
|
return roleKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoleKey(String roleKey) {
|
||||||
|
this.roleKey = roleKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRoleName() {
|
||||||
|
return roleName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoleName(String roleName) {
|
||||||
|
this.roleName = roleName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Role{" +
|
||||||
|
"roleId=" + roleId +
|
||||||
|
", roleKey='" + roleKey + '\'' +
|
||||||
|
", roleName='" + roleName + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.model;
|
||||||
|
|
||||||
|
import com.mybatisflex.annotation.Id;
|
||||||
|
import com.mybatisflex.annotation.Table;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户。
|
||||||
|
*
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-06-07
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Table("tb_user")
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private Integer userId;
|
||||||
|
private String userName;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
public Integer getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(Integer userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User{" +
|
||||||
|
"userId=" + userId +
|
||||||
|
", userName='" + userName + '\'' +
|
||||||
|
", password='" + password + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.model;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户信息。
|
||||||
|
*
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-06-07
|
||||||
|
*/
|
||||||
|
public class UserInfo {
|
||||||
|
|
||||||
|
private Integer userId;
|
||||||
|
private String userName;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
private List<Role> roleList;
|
||||||
|
private List<OrderInfo> orderInfoList;
|
||||||
|
|
||||||
|
public Integer getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(Integer userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Role> getRoleList() {
|
||||||
|
return roleList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoleList(List<Role> roleList) {
|
||||||
|
this.roleList = roleList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<OrderInfo> getOrderInfoList() {
|
||||||
|
return orderInfoList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderInfoList(List<OrderInfo> orderInfoList) {
|
||||||
|
this.orderInfoList = orderInfoList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserInfo{" +
|
||||||
|
"userId=" + userId +
|
||||||
|
", userName='" + userName + '\'' +
|
||||||
|
", password='" + password + '\'' +
|
||||||
|
", roleList=" + roleList +
|
||||||
|
", orderInfoList=" + orderInfoList +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.model;
|
||||||
|
|
||||||
|
import com.mybatisflex.annotation.Table;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户订单映射。
|
||||||
|
*
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-06-07
|
||||||
|
*/
|
||||||
|
@Table("tb_user_order")
|
||||||
|
public class UserOrder {
|
||||||
|
|
||||||
|
private Integer userId;
|
||||||
|
private Integer orderId;
|
||||||
|
|
||||||
|
public Integer getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(Integer userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getOrderId() {
|
||||||
|
return orderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderId(Integer orderId) {
|
||||||
|
this.orderId = orderId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.model;
|
||||||
|
|
||||||
|
import com.mybatisflex.annotation.Table;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户与角色连接表。
|
||||||
|
*
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-06-07
|
||||||
|
*/
|
||||||
|
@Table("tb_user_role")
|
||||||
|
public class UserRole {
|
||||||
|
|
||||||
|
private Integer userId;
|
||||||
|
private Integer roleId;
|
||||||
|
|
||||||
|
public Integer getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(Integer userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getRoleId() {
|
||||||
|
return roleId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoleId(Integer roleId) {
|
||||||
|
this.roleId = roleId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.model;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户 VO 对象。
|
||||||
|
*
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-06-07
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class UserVO {
|
||||||
|
|
||||||
|
private String userId;
|
||||||
|
private String userName;
|
||||||
|
private List<Role> roleList;
|
||||||
|
|
||||||
|
public String getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(String userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Role> getRoleList() {
|
||||||
|
return roleList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoleList(List<Role> roleList) {
|
||||||
|
this.roleList = roleList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserVO{" +
|
||||||
|
"userId='" + userId + '\'' +
|
||||||
|
", userName='" + userName + '\'' +
|
||||||
|
", roleList=" + roleList +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.model;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-06-07
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class UserVO1 {
|
||||||
|
|
||||||
|
private String userId;
|
||||||
|
private String userName;
|
||||||
|
private List<Role> roleList;
|
||||||
|
private Role role;
|
||||||
|
|
||||||
|
public String getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(String userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Role> getRoleList() {
|
||||||
|
return roleList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoleList(List<Role> roleList) {
|
||||||
|
this.roleList = roleList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Role getRole() {
|
||||||
|
return role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRole(Role role) {
|
||||||
|
this.role = role;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserVO1{" +
|
||||||
|
"userId='" + userId + '\'' +
|
||||||
|
", userName='" + userName + '\'' +
|
||||||
|
", roleList=" + roleList +
|
||||||
|
", role=" + role +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-06-07
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class UserVO2 {
|
||||||
|
|
||||||
|
private String userId;
|
||||||
|
private String userName;
|
||||||
|
private Role role;
|
||||||
|
|
||||||
|
public String getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(String userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Role getRole() {
|
||||||
|
return role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRole(Role role) {
|
||||||
|
this.role = role;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserVO2{" +
|
||||||
|
"userId='" + userId + '\'' +
|
||||||
|
", userName='" + userName + '\'' +
|
||||||
|
", role=" + role +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-06-07
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class UserVO3 {
|
||||||
|
|
||||||
|
private String userId;
|
||||||
|
private String userName;
|
||||||
|
private Integer roleId;
|
||||||
|
private String roleKey;
|
||||||
|
private String roleName;
|
||||||
|
|
||||||
|
public String getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(String userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getRoleId() {
|
||||||
|
return roleId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoleId(Integer roleId) {
|
||||||
|
this.roleId = roleId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRoleKey() {
|
||||||
|
return roleKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoleKey(String roleKey) {
|
||||||
|
this.roleKey = roleKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRoleName() {
|
||||||
|
return roleName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoleName(String roleName) {
|
||||||
|
this.roleName = roleName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserVO3{" +
|
||||||
|
"userId='" + userId + '\'' +
|
||||||
|
", userName='" + userName + '\'' +
|
||||||
|
", roleId=" + roleId +
|
||||||
|
", roleKey='" + roleKey + '\'' +
|
||||||
|
", roleName='" + roleName + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,16 +1,17 @@
|
|||||||
# DataSource Config
|
# DataSource Config
|
||||||
spring:
|
spring:
|
||||||
h2:
|
# h2:
|
||||||
console:
|
# console:
|
||||||
enabled: true
|
# enabled: true
|
||||||
datasource:
|
datasource:
|
||||||
driver-class-name: org.h2.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
username: sa
|
url: jdbc:mysql://localhost:3306/mp_test
|
||||||
password:
|
username: root
|
||||||
sql:
|
password: 12345678
|
||||||
init:
|
# sql:
|
||||||
schema-locations: classpath:schema.sql
|
# init:
|
||||||
data-locations: classpath:data.sql
|
# schema-locations: classpath:schema.sql
|
||||||
|
# data-locations: classpath:data.sql
|
||||||
mybatis-flex:
|
mybatis-flex:
|
||||||
mapper-locations:
|
mapper-locations:
|
||||||
- classpath*:/mapper/*.xml
|
- classpath*:/mapper/*.xml
|
||||||
|
|||||||
@ -2,6 +2,21 @@
|
|||||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||||
<mapper namespace="com.mybatisflex.test.mapper.MyAccountMapper">
|
<mapper namespace="com.mybatisflex.test.mapper.MyAccountMapper">
|
||||||
|
|
||||||
|
<!-- <resultMap id="testResultMap" type="com.mybatisflex.test.model.UserVO">-->
|
||||||
|
<!-- <id column="user_id" property="userId"/>-->
|
||||||
|
<!-- <result column="user_name" property="userName"/>-->
|
||||||
|
<!-- <collection property="roleList" ofType="com.mybatisflex.test.model.Role">-->
|
||||||
|
<!-- <id column="role_id" property="roleId"/>-->
|
||||||
|
<!-- <result column="role_key" property="roleKey"/>-->
|
||||||
|
<!-- <result column="role_name" property="roleName"/>-->
|
||||||
|
<!-- </collection>-->
|
||||||
|
<!-- <association property="role" javaType="com.mybatisflex.test.model.Role">-->
|
||||||
|
<!-- <id column="role_id" property="roleId"/>-->
|
||||||
|
<!-- <result column="role_key" property="roleKey"/>-->
|
||||||
|
<!-- <result column="role_name" property="roleName"/>-->
|
||||||
|
<!-- </association>-->
|
||||||
|
<!-- </resultMap>-->
|
||||||
|
|
||||||
<!-- selectByName -->
|
<!-- selectByName -->
|
||||||
<select id="selectByName" resultType="com.mybatisflex.test.model.Account">
|
<select id="selectByName" resultType="com.mybatisflex.test.model.Account">
|
||||||
select * from `tb_account` where `user_name` = #{name}
|
select * from `tb_account` where `user_name` = #{name}
|
||||||
|
|||||||
@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.mapper;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
|
import com.mybatisflex.test.model.UserInfo;
|
||||||
|
import com.mybatisflex.test.model.UserVO1;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static com.mybatisflex.test.model.table.GoodTableDef.GOOD;
|
||||||
|
import static com.mybatisflex.test.model.table.OrderGoodTableDef.ORDER_GOOD;
|
||||||
|
import static com.mybatisflex.test.model.table.OrderTableDef.ORDER;
|
||||||
|
import static com.mybatisflex.test.model.table.RoleTableDef.ROLE;
|
||||||
|
import static com.mybatisflex.test.model.table.UserOrderTableDef.USER_ORDER;
|
||||||
|
import static com.mybatisflex.test.model.table.UserRoleTableDef.USER_ROLE;
|
||||||
|
import static com.mybatisflex.test.model.table.UserTableDef.USER;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-06-07
|
||||||
|
*/
|
||||||
|
@SpringBootTest
|
||||||
|
class UserMapperTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSelectOne() {
|
||||||
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
|
.select(USER.USER_ID, USER.USER_NAME, ROLE.ALL_COLUMNS)
|
||||||
|
.from(USER.as("u"))
|
||||||
|
.leftJoin(USER_ROLE).as("ur").on(USER_ROLE.USER_ID.eq(USER.USER_ID))
|
||||||
|
.leftJoin(ROLE).as("r").on(USER_ROLE.ROLE_ID.eq(ROLE.ROLE_ID))
|
||||||
|
.where(USER.USER_ID.eq(3));
|
||||||
|
System.out.println(queryWrapper.toSQL());
|
||||||
|
// UserVO userVO = userMapper.selectListByQueryAs(queryWrapper, UserVO.class);
|
||||||
|
UserVO1 userVO = userMapper.selectOneByQueryAs(queryWrapper, UserVO1.class);
|
||||||
|
// UserVO2 userVO = userMapper.selectListByQueryAs(queryWrapper, UserVO2.class);
|
||||||
|
// UserVO3 userVO = userMapper.selectListByQueryAs(queryWrapper, UserVO3.class);
|
||||||
|
System.err.println(userVO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSelectList() {
|
||||||
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
|
.select(USER.USER_ID, USER.USER_NAME, ROLE.ALL_COLUMNS)
|
||||||
|
.from(USER.as("u"))
|
||||||
|
.leftJoin(USER_ROLE).as("ur").on(USER_ROLE.USER_ID.eq(USER.USER_ID))
|
||||||
|
.leftJoin(ROLE).as("r").on(USER_ROLE.ROLE_ID.eq(ROLE.ROLE_ID))
|
||||||
|
.where(USER.USER_ID.ge(2));
|
||||||
|
System.out.println(queryWrapper.toSQL());
|
||||||
|
// List<UserVO> userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO.class);
|
||||||
|
List<UserVO1> userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO1.class);
|
||||||
|
// List<UserVO2> userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO2.class);
|
||||||
|
// List<UserVO3> userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO3.class);
|
||||||
|
userVOS.forEach(System.err::println);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testComplexSelectList() {
|
||||||
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
|
.select(USER.ALL_COLUMNS, ROLE.ALL_COLUMNS, ORDER.ALL_COLUMNS, GOOD.ALL_COLUMNS)
|
||||||
|
.from(USER.as("u"))
|
||||||
|
.leftJoin(USER_ROLE).as("ur").on(USER.USER_ID.eq(USER_ROLE.USER_ID))
|
||||||
|
.leftJoin(ROLE).as("r").on(ROLE.ROLE_ID.eq(USER_ROLE.ROLE_ID))
|
||||||
|
.leftJoin(USER_ORDER).as("uo").on(USER.USER_ID.eq(USER_ORDER.USER_ID))
|
||||||
|
.leftJoin(ORDER).as("o").on(ORDER.ORDER_ID.eq(USER_ORDER.ORDER_ID))
|
||||||
|
.leftJoin(ORDER_GOOD).as("og").on(ORDER.ORDER_ID.eq(ORDER_GOOD.ORDER_ID))
|
||||||
|
.leftJoin(GOOD).as("g").on(GOOD.GOOD_ID.eq(ORDER_GOOD.GOOD_ID));
|
||||||
|
System.err.println(queryWrapper.toSQL());
|
||||||
|
List<UserInfo> userInfos = userMapper.selectListByQueryAs(queryWrapper, UserInfo.class);
|
||||||
|
userInfos.forEach(System.err::println);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user