fix: ClassCastException error; close #I7MRWP

This commit is contained in:
开源海哥 2023-07-21 08:27:46 +08:00
parent 18b67db07e
commit 0e750622f6
8 changed files with 55 additions and 14 deletions

View File

@ -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);
}
}
}
// 实体类嵌套

View File

@ -64,11 +64,11 @@
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.h2database</groupId>-->
<!-- <artifactId>h2</artifactId>-->
<!-- <version>2.1.214</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.yaml</groupId>-->

View File

@ -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);
}

View File

@ -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);

View File

@ -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<Map<String,String>> 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<Map<String, String>> getOther() {
return other;
}
public void setOther(List<Map<String, String>> other) {
this.other = other;
}
}

View File

@ -7,7 +7,7 @@ spring:
# 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

View File

@ -18,7 +18,7 @@
<!-- </resultMap>-->
<!-- selectByName -->
<select id="selectByName" resultType="com.mybatisflex.test.model.Account">
<select id="selectByName" resultType="com.mybatisflex.test.model.AccountDto">
select * from `tb_account` where `user_name` = #{name}
</select>

View File

@ -177,14 +177,14 @@
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>${HikariCP.version}</version>
<scope>test</scope>
<optional>true</optional>
</dependency>
</dependencies>