MybatisMapper mapping

MyBatis-input mapping and output mapping of mapper

The mapper.xml file is the way we configure database statements when we make mybatis. Each statement corresponds to a method, with input parameters and output parameters. Each parameter has different types. The following is how to configure different types of parameters

Input mapping

  • Simple type

    That is, the data types originally owned in the java class library, such as Integar, Long, String, etc.

    Configuration method, directly add the type to the parameterType attribute of the sql statement, the code is as follows

        <select id="findUserById" parameterType="int" resultType="cn.itcast.pojo.User">  
            SELECT * FROM user where id=#{id}  
        </select>  
    
  • pojo object type

    The input parameter is a pojo object, the parameters that can be used in the sql statement are the attributes of the object, and the value in the #{} or ${} brackets is the pojo attribute name.
    Before use, you need <typeAliases>to declare the pojo object under the tag in mapper-config.xml , such as

    <typeAlias alias="PageParam" type="com.param.PageParam"/>
    

    When you need to calculate the attribute value, just write the calculation process in one of the brackets, such as the second line from the bottom of the code below.

    <select id="getStockPage" parameterType="stockParam" resultMap="BaseStockResultMap">
        SELECT BOOK.*,num
        FROM BOOK,stock
        where book.book_id=stock.book_id
        and classification_id like #{classification_id}
        and book.name like '%${keyword}%'
        ORDER BY num ${sort_method}
        LIMIT ${(currentPage-1)*pageSize},${pageSize}
    </select>
    

    Using this type of input mapping can facilitate the use of multiple parameters to call the sql statement. You only need to encapsulate the parameters into a class and use this type of object as the input mapping.

  • Pojo packaging object type

    Commonly used when multiple comprehensive conditions are required to query, such as when querying book details, the basic information condition is a condition, which can be queried based on the basic information of the book. Book transaction/borrowing records can also be used as additional conditions to filter books based on borrowing records .

    Packaging object : Need to use another pojo object as an attribute value in a pojo object.

    Reference : Same as the above pojo object reference, declared in mapper-config.xml.

    Sample code

    <select id="MYPOJO" parameterType="Pojo">
        SELECT * FROM `user` WHERE username LIKE "%"#{user.username}"%" 
    </select>
    

    At the end #{user.username}, user represents the variable name of a User class object member declared in the mapping Pojo object, and username is an attribute name of the User class object.

Output map resultMap

Guess you like

Origin blog.csdn.net/INER1/article/details/109295120