From 0e750622f6fe1955439277eff132d3a142427f73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Fri, 21 Jul 2023 08:27:46 +0800 Subject: [PATCH] fix: ClassCastException error; close #I7MRWP --- .../core/table/TableInfoFactory.java | 6 ++- .../mybatis-flex-spring-boot-test/pom.xml | 10 ++--- .../test/controller/AccountController.java | 3 +- .../test/mapper/MyAccountMapper.java | 3 +- .../mybatisflex/test/model/AccountDto.java | 37 +++++++++++++++++++ .../src/main/resources/application.yml | 4 +- .../main/resources/mapper/accountMapper.xml | 2 +- pom.xml | 4 +- 8 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/AccountDto.java 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 12fbb3f2..b87d9b4f 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 @@ -218,8 +218,10 @@ public class TableInfoFactory { if (Collection.class.isAssignableFrom(fieldType)) { Type genericType = TypeParameterResolver.resolveFieldType(field, entityClass); if (genericType instanceof ParameterizedType) { - Class actualTypeArgument = (Class) ((ParameterizedType) genericType).getActualTypeArguments()[0]; - tableInfo.addCollectionType(field, actualTypeArgument); + Type actualTypeArgument = ((ParameterizedType) genericType).getActualTypeArguments()[0]; + if (actualTypeArgument instanceof Class){ + tableInfo.addCollectionType(field, (Class) actualTypeArgument); + } } } // 实体类嵌套 diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml b/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml index 989a614d..9e45e64d 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml @@ -64,11 +64,11 @@ - - com.h2database - h2 - 2.1.214 - + + + + + diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java index 2574fa48..180e9ec6 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java @@ -22,6 +22,7 @@ import com.mybatisflex.core.row.Row; import com.mybatisflex.test.mapper.AccountMapper; import com.mybatisflex.test.mapper.MyAccountMapper; import com.mybatisflex.test.model.Account; +import com.mybatisflex.test.model.AccountDto; import com.mybatisflex.test.service.AccountService; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; @@ -52,7 +53,7 @@ public class AccountController { @GetMapping("/account/byName/{name}") - Account selectName(@PathVariable("name") String name) { + AccountDto selectName(@PathVariable("name") String name) { return myAccountMapper.selectByName(name); } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java index f111c014..d05038eb 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java @@ -17,13 +17,14 @@ package com.mybatisflex.test.mapper; import com.mybatisflex.test.model.Account; +import com.mybatisflex.test.model.AccountDto; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; public interface MyAccountMapper extends AccountMapper { - Account selectByName(@Param("name") String name); + AccountDto selectByName(@Param("name") String name); @Select("select * from tb_account where id = #{id} and id =#{id}") Account selectById(@Param("id") Object id); diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/AccountDto.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/AccountDto.java new file mode 100644 index 00000000..a49730f0 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/AccountDto.java @@ -0,0 +1,37 @@ +package com.mybatisflex.test.model; + +import java.util.List; +import java.util.Map; + +public class AccountDto { + + private Long id; + + private String name; + + private List> other; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List> getOther() { + return other; + } + + public void setOther(List> other) { + this.other = other; + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml index 565acba8..36c7ddb6 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml @@ -4,10 +4,10 @@ spring: # console: # enabled: true datasource: - # driver-class-name: com.mysql.cj.jdbc.Driver +# driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/flex_test username: root - password: 12345678 + password: 123456 # driver-class-name: # datasource: # driver-class-name: org.h2.Driver diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml index 9538ea33..26631fae 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml @@ -18,7 +18,7 @@ - select * from `tb_account` where `user_name` = #{name} diff --git a/pom.xml b/pom.xml index 351d7c05..119bb412 100644 --- a/pom.xml +++ b/pom.xml @@ -177,14 +177,14 @@ mysql mysql-connector-java ${mysql.version} - test + true com.zaxxer HikariCP ${HikariCP.version} - test + true