MyBatis resultMap and aliases

Mybatis write sql If the return type is a kind of thing we need to write the full name (class name preceded by the package name) of this class, it will become redundant code that does not look good the way we can use an alias to an alias and then take a class Instead of this class can use aliases we need to directly Mybatis-config.xml file Add a typeAliases tag in alias which is provided corresponding to the class

<typeAliases>
        <typeAlias type="com.Jee.entity.User" alias="hello"/>
    </typeAliases>

This allows the bag below com.Jee.entity User class instead of a hello code is simple to many

What a resultMap attribute it does is it? We know ORM required fields in the database table and attribute names Java entity class then when we correspond if the table fields and class attributes inconsistent when it will not find the field in the database so that database-related fields become null so very bad that we can use resultMap to convert field mapping relationship and the entity class attributes our table

 	<select id="001" resultMap="map1" parameterType="int">
        select * from user where id = #{id}
    </select>
    
    <resultMap id="map1" type="com.Jee.entity.User">
        <result column="id" property="id"></result>
        <result column="name" property="name"></result>
        <result column="psd" property="password"></result>
    </resultMap>

Here is the column in the table column (field) property is a property named entity class we can use the result resultMap label tag to be on their maps .

Published 53 original articles · won praise 0 · Views 1966

Guess you like

Origin blog.csdn.net/XXuan_/article/details/104081516