简体   繁体   中英

Mapping mybatis results with annotations

I have a POJO that I am trying to map using mybatis annotations. It will fetch the correct number of rows, but it returns all nulls. Is there something I am doing wrong?

POJO

public class Vendor {   
    private String vendorCode;
    private String vendorName;
}

Mapper

@Select("SELECT V.AGNC_MGT_SYS_VNDR_CDE, V.AGNC_MGT_SYS_VNDR_NME FROM CPED_CPM1S.AGNC_MGT_VNDR_T V")
    @Results({
            @Result(property = "vendorCode", column = "CPED_CPM1S.AGNC_MGT_SYS_VNDR_CDE"),
            @Result(property = "vendorName", column = "CPED_CPM1S.AGNC_MGT_SYS_VNDR_NME")
            })
    List<Vendor> selectAllVendors();

Change the select to:

    @Results({
        @Result(property = "vendorCode", column = "AGNC_MGT_SYS_VNDR_CDE"),
        @Result(property = "vendorName", column = "AGNC_MGT_SYS_VNDR_NME")
        })

The column names should exactly match the output columns from the query. In the above snippet, CPED_CPM1S. has been removed.

And btw, another way to do this without giving the result mapping is to change the query to have the output with column name as the bean property names:

@Select("SELECT V.AGNC_MGT_SYS_VNDR_CDE as vendorCode, V.AGNC_MGT_SYS_VNDR_NME as vendorName FROM CPED_CPM1S.AGNC_MGT_VNDR_T V")

Note the 'as propertyName' addition to the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM