Solution of inconsistency between method return generic type and resulttype in mybatis


The generic type returned by the mybatis method is inconsistent with the resulttype

When the return value type of the method of is list < a >, and the resulttype of the corresponding SQL in xxxmapper.xml is specified as B object, there will be no package error (even if there is no relationship between a and b)

Cause analysis:

1. Collection objects are storage objects, which can be different object lists

2. Type conversion exceptions often occur when directly handling list types, jdk5 resulting in generic types, which makes programmers store objects of the same type in the list

3. Generics are used in the compilation stage only to prevent type confusion and type conversion exceptions

4. Reflection is adopted when encapsulating beans in mybatis result set, which is carried out at runtime. At this time, it is easy to enter the misunderstanding when looking at generics


public class A
public class B{
public class AImpl implements AService {
private AMapper  aMapper ;
public List<B> increTrade(Map<String, Object> map) {
List<B> lst =aMapper.increTrade(map); // Here, debug sees that the list is of type A, and the generic type is misleading
return lst;
public interface AMapper {
List<B>increTrade(Map<String, Object> map);

XML file corresponding to amapper

<select parameterType="java.util.Map" resultType="cn.easier.admin.model.A">

The map generic in the list corresponds to the return type of mybatis

List < map < string, Object > > corresponds to mybatis

  <select resultType="java.util.HashMap"  >
 DATE_FORMAT( report_date, '%Y%m' ) label ,
 COUNT( 0 ) value
 WHERE tsdr_ type !=  "Fire alarm"
@Apioperation (value = "count the number of alarms by month", notes = "count the number of alarms by month")
@GetMapping(value = "warnCountStatistics")
@Actionlog (value = "count all alarms by month")
    public  List<Map<String,Object>>  warnCountStatistics(){
        List list = smokeDetectorRecordMapper.selectWarnCountBymonths();
        return list;

Return results

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer.