Documentation

36 views
Skip to first unread message

somid3

unread,
Mar 28, 2011, 8:47:55 PM3/28/11
to mybati...@googlegroups.com
These are just some notes so that perhaps it is included in the MyIbatis User Guide, and for all future delevelopers who are starting from scratch with MyIbates.

MyIbatis's lazy loading and entity mappings are so awesome and easy to use once you get it all tied-up! I spent about 2 days going through all the pitfalls looking at examples, tutorials, etc ... but now my domain/dao/services layer is so sweet.

If you want a true domain layer with entity references you must:

(1) In your mapper configurations, in your "select" elements, you MUST use "resultMap" and NOT "resultType". For example:

    <resultMap id="contentResultMap" type="Content">
        <id property="id" column="id" />
        <result property="createdOn" column="created_on"/>
        <result property="content" column="content"/>
        <association property="createdByUser" column="created_by_user_id" select="User.selectById" />
        <association property="approvedByUser" column="approved_by_user_id" select="User.selectById" />
        <collection property="tags" column="id" ofType="Tag" select="Tag.selectById" />
        <collection property="contentVotes" column="id" ofType="ContentVote" select="ContentVote.selectById" />
    </resultMap>
    
<select id="Content.selectById" parameterType="int" resultMap="contentResultMap">
        select * from contents where id = #{id}
    </select>
Do not try to be clever and set the "resultType='Content'" because that will not work. Apparently resultType is only to be used when no resultMap exists in your mapper configuration.

(2) Setup your Log4J to print out the DEBUG level, that way you can see all the queries MyIbatis is sending your database. Once your Log4J is setup, then start playing with the lazy settings to see how awesome MyIbatis is. Look at the PetStore example for a copy-and-paste example of what your Log4J properties should look like.

(3) I strongly suggest you use the CGLIB library. Look at the PetStore example pom.xml if you're using Maven.

(4) Now, this is somewhat of an awesome feature. After some testing it seems MyIbatis objects are still "attached" or "hot" or "active" (whatever word you use) with the getters once your object is outside your SqlSession. That is, in my tests, once I have already closed my session and call author.getBooks(), if books is a lazy property, somehow MyIbatis is capable of retrieving a connection and loading the books collection. This is similar to perpetually attached objects in Hibernate, although Hibernate has that feature off by default.

Thanks again for this great library.
Reply all
Reply to author
Forward
0 new messages