associations with annotations in MyBatis

0

Good day family, I have a rest project mounted on Spring. I have an endpoind that works for me and returns an array of objects:

[
  {
    "accountBankId": "24083410-1d00-43a9-9a14-09bd865a1381",
    "accountId": 1,
    "accountName": "Banco de Oriente",
    "bankId": 2,
    .
    .
    .
  },
  {
    "accountBankId": "98ec6daf-0736-4e80-a9aa-0a0a8df59c78",
    "accountId": 1,
    "accountName": "Nombre Cuenta",
    "bankId": 3,
    .
    .
    .
  }
]

However now I have the need to return the same object but with an object inside one of its fields like this:

{
  "accountBankId": "98ec6daf-0736-4e80-a9aa-0a0a8df59c78",
  "accountId": 1,
  "accountName": "Nombre Cuenta",
  "bank": {
    "name":"...",
    "address": "Avenida Siempreviva ..."
  },
  .
  .
  .
}

In this case bankId is replaced by bank

  

The code ...

The main mapper:

public interface AsobancariaMapper {
final static String GET_ASOBANCARIA_ACCOUNTS = "select aso.nombre_cuenta, aso.datos_bancarios_asobancaria_id, aso.cuenta_id, aso.banco_id, aso.tipo_cuenta, "
        + " aso.numero_cuenta, aso.codigos_servicio_pse, aso.fecha_creacion, aso.fecha_ultima_actualizacion, aso.medio_pago, "
    + " aso.sesion_modificacion_id, aso.usuario_modificacion_id "
    + " from pps.cuenta c "
    + " left join pps.datos_bancarios_asobancaria aso on c.cuenta_id=aso.cuenta_id "
    + " where c.cuenta_id is not null "
    + " and c.cuenta_id = #{accountId} ";

  @Select(GET_ASOBANCARIA_ACCOUNTS)
    @Results(value = {
            @Result(property = "accountName", javaType = String.class, column = "nombre_cuenta"),           
            @Result(property = "accountBankId", javaType = String.class, column = "datos_bancarios_asobancaria_id"),
            @Result(property = "accountId", javaType = Integer.class, column = "cuenta_id"),
            @Result(property = "bank", javaType=BankFull.class,  column="banco_id", [email protected](select="com.payulatam.ppp4.secure.api.mappers.BankMapper.getBankFull")),
            @Result(property = "accountType", javaType = String.class, column = "tipo_cuenta"),
            @Result(property = "accountNumber", javaType = String.class, column = "numero_cuenta"),
            @Result(property = "pseServiceCodes", javaType = String.class, column = "codigos_servicio_pse"),
            @Result(property = "creationDate", javaType = Date.class, column = "fecha_creacion"),
            @Result(property = "lastUpdatedDate", javaType = Date.class, column = "fecha_ultima_actualizacion"),
            @Result(property = "lastUpdateSession", javaType = String.class, column = "sesion_modificacion_id"),
            @Result(property = "lastUpdateUser", javaType = Integer.class, column = "usuario_modificacion_id"),
            @Result(property = "paymentMethodMain", javaType = String.class, column = "medio_pago") 
    })
    public List<AccountAsobancariaBank> getAccounts(@Param("accountId") final Integer accountId) throws Exception;
  }

The mapper of the object that must be included in the previous one ( getBankFull )

public interface BankMapper {

    public static final String ALL_BANKS = "select banco_id, descripcion, pais_iso_3166 from pps.banco";    

    @Select(GET_BANKFULL_BY_ID)
    @Results(value ={
            @Result(property = "bankId", javaType = Integer.class, column = "banco_id"),
            @Result(property = "accountId", javaType = Integer.class, column = "cuenta_id"),
            @Result(property = "altTag", javaType = String.class, column = "alt_tag"),
            @Result(property = "description", javaType = String.class, column = "descripcion"),
            @Result(property = "image1", javaType = String.class, column = "imagen_1"),
            @Result(property = "image2", javaType = String.class, column = "imagen_2"),
            @Result(property = "pseCode", javaType = String.class, column = "codigo_pse"),
            @Result(property = "bankCountryCode", javaType = String.class, column = "pais_iso_3166"),
            @Result(property = "enable", javaType = Boolean.class, column = "habilitado"),
            @Result(property = "monthsInterestFree", javaType = Boolean.class, column = "meses_sin_intereses"),
            @Result(property = "checkoutSvailable", javaType = Boolean.class, column = "disponible_checkout"),
            @Result(property = "bankCode", javaType = Integer.class, column = "codigo_banco"),
            @Result(property = "clasification", javaType = String.class, column = "clasificacion")
    })
    public BankFull getBankFull(Integer bankId) throws Exception;
}

The BankFull model is a POJO.

    
asked by Jean 17.03.2016 в 20:19
source

1 answer

1

Dear, to do this you can do 2 ways in the XML configuration file. where you include the sub-class

or in the .java file as follows

@Select("SELECT H.getNombre  as NOMBRE, C.idLugar AS LUGAR "+
    "FROM HOLA H " +
    "INNER JOIN CHAO C ON C.ID = H.ID " +
    "WHERE H.ID= #{id}")   
  @Results({
    @Result(property = "idEvaluacion", column = "NOMBRE", javaType = HOLA.class, typeHandler = HOLA.class),
    @Result(property = "idLugar", column = "LUGAR", javaType = CHAO.class, typeHandler = IntegerTypeHandler.class),
})

where javaType are objects ( HOLA.class and CHAO.class ) and typeHandler type data (IntegerTypeHandler.class)

now if the object is inside another

in the property you must put object.attribute, you can be several objects inside other objects.

example

property = "bank.name" 

greetings

    
answered by 27.05.2016 в 00:33