diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java index 09a8f42f..65563ac1 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java @@ -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()); diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Account.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Account.java index 3d7fa200..6f0d21f9 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Account.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Account.java @@ -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 { - @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; diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/BaseEntity.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/BaseEntity.java new file mode 100644 index 00000000..09b7a559 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/BaseEntity.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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 extends IdEntity { + + protected T userName; + + public T getUserName() { + return userName; + } + + public void setUserName(T userName) { + this.userName = userName; + } +} \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/IdEntity.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/IdEntity.java new file mode 100644 index 00000000..2baa5af4 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/IdEntity.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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 implements Serializable { + + @Id(keyType = KeyType.Auto) + protected T id; + + public T getId() { + return id; + } + + public void setId(T id) { + this.id = id; + } +} \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/User.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/User.java index 1ff8f244..1d928e86 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/User.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/User.java @@ -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 roleList; public Integer getUserId() { return userId; @@ -58,12 +62,21 @@ public class User { this.password = password; } + public List getRoleList() { + return roleList; + } + + public void setRoleList(List roleList) { + this.roleList = roleList; + } + @Override public String toString() { return "User{" + "userId=" + userId + ", userName='" + userName + '\'' + ", password='" + password + '\'' + + ", roleList=" + roleList + '}'; } } \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/FieldTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/FieldTest.java new file mode 100644 index 00000000..5f7587e0 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/FieldTest.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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 genericParameters = StringUtil.getGenericParameters(genericString); + System.out.println(genericParameters); + } + + @Test + void test02() { + Class 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); + } + +} \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AccountMapperTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AccountMapperTest.java new file mode 100644 index 00000000..84ed06ed --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AccountMapperTest.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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); + } + +} \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java index 38db650c..108cc632 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java @@ -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 users = userMapper.selectListByQueryAs(QueryWrapper.create(), User.class); + users.forEach(System.err::println); List userVOS = userMapper.selectListByQueryAs(QueryWrapper.create(), UserVO.class); userVOS.forEach(System.err::println); }