MyBatis:collection via annotation in one query MyBatis:collection via annotation in one query sql sql

MyBatis:collection via annotation in one query


AFAIK, you cannot use JOINs if you are using mapping with annotations.

From the doc, about the usage of @Many,

A mapping to a collection property of a complex type. Attributes: select, which is the fully qualified name of a mapped statement (i.e. mapper method) that can load a collection of instances of the appropriate types, fetchType, which supersedes the global configuration parameter lazyLoadingEnabled for this mapping. NOTE You will notice that join mapping is not supported via the Annotations API. This is due to the limitation in Java Annotations that does not allow for circular references.

You can directly use your ResultMap with annotations if you like:

@Select("SELECT QUERY")@ResultMap("readItemsRM")public List<Item> select();


I found out that you can actually do one-to-many or one-to-one joins using Java annotations on MyBatis

public class Master {    private String nama;    private Short usia;    private List<Contoh> contohs;}public interface MasterMapper {    @Select("SELECT master.nama, master.usia FROM test.master WHERE master.nama = #{nama}")    @Results(value = {         @Result(property="nama", column="nama"),         @Result(property="usia", column="usia"),         @Result(property="contohs", javaType=List.class, column="nama",          many=@Many(select="getContohs"))    })    Master selectUsingAnnotations(String nama);    @Select("SELECT contoh.id, contoh.nama, contoh.alamat "         + " FROM test.contoh WHERE contoh.nama = #{nama}")    List<Contoh> getContohs(String nama);}

There are more details in this link


Inner join queries using ibatis annoation's Example.

@Select("SELECT t1.column1, t1.column2, t2.column3, t2.column4 "            + "FROM table1 AS t1 "            + "INNER JOIN table2 AS t2 "            + "ON (t1.column1 = t2.column3)  WHERE t1.column1=#{id}")    @Results(value = {            @Result(property = "val1", column = "column1"),            @Result(property = "val2", column = "column2"),            @Result(property = "val3", column = "column3"),            @Result(property = "val4", column = "column4")})    MyBean getInnerJoinResult(@Param("id") Integer id);