!63 fix: 实体类不支持泛型。

Merge pull request !63 from 王帅/main
This commit is contained in:
Michael Yang 2023-06-13 07:43:27 +00:00 committed by Gitee
commit f1a527906d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 236 additions and 11 deletions

View File

@ -133,7 +133,8 @@ public class TableInfoFactory {
TableInfo tableInfo = new TableInfo();
tableInfo.setEntityClass(entityClass);
tableInfo.setReflector(new Reflector(entityClass));
Reflector reflector = new Reflector(entityClass);
tableInfo.setReflector(reflector);
//初始化表名
Table table = entityClass.getAnnotation(Table.class);
@ -206,7 +207,7 @@ public class TableInfoFactory {
continue; // ignore
}
Class<?> fieldType = field.getType();
Class<?> fieldType = reflector.getGetterType(field.getName());
//满足一下 3 中情况不支持该类型
if ((column == null || column.typeHandler() == UnknownTypeHandler.class) // 未配置 typeHandler
@ -285,7 +286,7 @@ public class TableInfoFactory {
Id id = field.getAnnotation(Id.class);
ColumnInfo columnInfo;
if (id != null) {
columnInfo = new IdInfo(columnName, field.getName(), field.getType(), id);
columnInfo = new IdInfo(columnName, field.getName(), fieldType, id);
idInfos.add((IdInfo) columnInfo);
} else {
columnInfo = new ColumnInfo();
@ -294,7 +295,7 @@ public class TableInfoFactory {
columnInfo.setColumn(columnName);
columnInfo.setProperty(field.getName());
columnInfo.setPropertyType(field.getType());
columnInfo.setPropertyType(fieldType);
if (column != null && column.typeHandler() != UnknownTypeHandler.class) {
Class<?> typeHandlerClass = column.typeHandler();
@ -306,7 +307,7 @@ public class TableInfoFactory {
ColumnMask columnMask = field.getAnnotation(ColumnMask.class);
if (columnMask != null && StringUtil.isNotBlank(columnMask.value())) {
if (String.class != field.getType()) {
if (String.class != fieldType) {
throw new IllegalStateException("@ColumnMask() only support for string type field. error: " + entityClass.getName() + "." + field.getName());
}
columnInfo.setMaskType(columnMask.value().trim());

View File

@ -15,18 +15,16 @@
*/
package com.mybatisflex.test.model;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import java.util.Date;
@Table(value = "tb_account",onSet = AccountOnSetListener.class)
public class Account {
public class Account extends BaseEntity<String, Long> {
@Id(keyType = KeyType.Auto)
private Long id;
private String userName;
/*@Id(keyType = KeyType.Auto)
private Long id;*/
//private String userName;
private Integer age;
private Date birthday;

View File

@ -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.test.model;
/**
* @author 王帅
* @since 2.0
*/
public class BaseEntity<T, ID> extends IdEntity<ID> {
protected T userName;
public T getUserName() {
return userName;
}
public void setUserName(T userName) {
this.userName = userName;
}
}

View File

@ -0,0 +1,40 @@
/*
* 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.KeyType;
import java.io.Serializable;
/**
* @author 王帅
* @since 2023-06-13
*/
public class IdEntity<T> implements Serializable {
@Id(keyType = KeyType.Auto)
protected T id;
public T getId() {
return id;
}
public void setId(T id) {
this.id = id;
}
}

View File

@ -19,6 +19,8 @@ package com.mybatisflex.test.model;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table;
import java.util.List;
/**
* 用户
*
@ -33,6 +35,8 @@ public class User {
private Integer userId;
private String userName;
private String password;
//@Column(ignore = true)
private List<Role> roleList;
public Integer getUserId() {
return userId;
@ -58,12 +62,21 @@ public class User {
this.password = password;
}
public List<Role> getRoleList() {
return roleList;
}
public void setRoleList(List<Role> roleList) {
this.roleList = roleList;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", roleList=" + roleList +
'}';
}
}

View File

@ -0,0 +1,71 @@
/*
* 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.common;
import com.mybatisflex.core.util.StringUtil;
import com.mybatisflex.test.model.Account;
import com.mybatisflex.test.model.BaseEntity;
import com.mybatisflex.test.model.UserVO;
import org.apache.ibatis.reflection.Reflector;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
/**
* @author 王帅
* @since 2023-06-13
*/
class FieldTest {
@Test
void test() {
String genericString = BaseEntity.class.toGenericString();
System.out.println(genericString);
List<String> genericParameters = StringUtil.getGenericParameters(genericString);
System.out.println(genericParameters);
}
@Test
void test02() {
Class<Account> accountClass = Account.class;
Method[] declaredMethods = accountClass.getMethods();
Arrays.stream(declaredMethods)
.filter(e -> e.getName().startsWith("get"))
.forEach(System.out::println);
}
@Test
void test03() {
Reflector reflector = new Reflector(Account.class);
Class<?> id = reflector.getGetterType("id");
Class<?> userName = reflector.getGetterType("userName");
Class<?> age = reflector.getGetterType("age");
System.out.println(id);
System.out.println(userName);
System.out.println(age);
}
@Test
void test04() {
Reflector reflector = new Reflector(UserVO.class);
Class<?> roleList = reflector.getGetterType("roleList");
System.out.println(roleList);
}
}

View File

@ -0,0 +1,65 @@
/*
* 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.Account;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Date;
/**
* @author 王帅
* @since 2023-06-13
*/
@SpringBootTest
@SuppressWarnings("all")
class AccountMapperTest {
@Autowired
private AccountMapper accountMapper;
@Test
void testInsert() {
Account account = new Account();
account.setBirthday(new Date());
account.setUserName("张三");
account.setAge(18);
accountMapper.insert(account);
}
@Test
void testUpdate() {
Account account = new Account();
account.setId(1L);
account.setAge(58);
accountMapper.update(account);
}
@Test
void testDelete() {
accountMapper.deleteById(1L);
}
@Test
void testSelect() {
accountMapper.selectListByQuery(QueryWrapper.create()).forEach(System.err::println);
}
}

View File

@ -18,6 +18,7 @@ package com.mybatisflex.test.mapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.test.model.User;
import com.mybatisflex.test.model.UserInfo;
import com.mybatisflex.test.model.UserVO;
import org.junit.jupiter.api.Test;
@ -102,6 +103,8 @@ class UserMapperTest {
@Test
void testSelectListNoJoin() {
List<User> users = userMapper.selectListByQueryAs(QueryWrapper.create(), User.class);
users.forEach(System.err::println);
List<UserVO> userVOS = userMapper.selectListByQueryAs(QueryWrapper.create(), UserVO.class);
userVOS.forEach(System.err::println);
}