1. 程式人生 > >MyBatis中 ofType和javaType區別

MyBatis中 ofType和javaType區別

轉自:https://blog.csdn.net/u013216156/article/details/78642920/

JavaType和ofType都是用來指定物件型別的,但是JavaType是用來指定pojo中屬性的型別,而ofType指定的是對映到list集合屬性中pojo的型別

pojo類:

publicclass User {

    privateint id;

    privateString username;

    privateString mobile;

    privateList<Post>posts;

}

 

user.xml:

<resultMap type="User" id="resultUserMap">

         <result property="id" javaType="int" column="user_id" />

         <result property="username"

javaType="string" column="username" />

         <result property="mobile"  column="mobile" />

                        <!--javatype指定的是user物件的屬性的型別(例如id,posts),而oftype指定的是對映到list集合屬性中pojo的型別(本例指的是post型別)-->

         <collection property="posts"  ofType="com.spenglu.Post"  javaType="java.util.ArrayList" column="userid">

             <id property="id" column="post_id" javaType="int" jdbcType="INTEGER"/>   

            <result property="title" column="title" javaType="string" jdbcType="VARCHAR"/>

            <result property="content" column="content" javaType="string" jdbcType="VARCHAR"/>

         </collection>

    </resultMap>