Mybatis mapper study notes

Get the primary key value of the auto-increment property:
        useGeneratedKeys="true" keyProperty="id"
            mysql supports auto-incrementing primary key, the acquisition of auto-incrementing primary key value, mybatis also uses statement.getGeneratedKeys();
        useGeneratedKeys="true"; Increase the primary key to obtain the primary key value strategy
        keyProperty; specify the corresponding primary key property, that is, after mybatis obtains the primary key value, which property of javaBean will encapsulate this value to
obtain non-self-incrementing properties:
        Oracle does not support self-incrementing; Oracle uses sequence to simulate auto-increment;
        <insert id="addEmp" databaseId="oracle">
        <!--
        keyProperty: which property of javaBean the detected primary key value is encapsulated
        order="BEFORE": the current sql is inserting sql Before running
               AFTER: the current sql runs after inserting the sql
        resultType: the return value type of the detected data
        
        BEFORE running order:
            first run the selectKey query id sql; find the id value encapsulated in the id attribute of javaBean
            When running the inserted sql; you can take out the value corresponding to the id attribute
        AFTER running order:
            run the inserted sql first (take out the new value from the sequence as the id);
            then run the selectKey query id sql;
         -->
        <selectKey keyProperty=" id" order="BEFORE" resultType="Integer">
            <!-- Write the sql statement to query the primary key-->
            <!-- BEFORE-->
            select EMPLOYEES_SEQ.nextval from dual
            <!-- AFTER:
             select EMPLOYEES_SEQ.currval from dual -->
        </selectKey>
        
        <!-- The primary key when inserting is obtained from the sequence -->
        <!-- BEFORE:-->
        insert into employees(EMPLOYEE_ID,LAST_NAME,EMAIL)
        values(#{ id},#{lastName}, #{email<!-- ,jdbcType=NULL -->})
        <!-- AFTER:
        insert into employees(EMPLOYEE_ID,LAST_NAME,EMAIL)
        values(employees_seq.nextval,#{lastName},#{email}) -->
    </insert>

Pass in multiple parameters for query:
        method in interface :
        public Employee getEmpByIdAndLastName(@Param("id")Integer id,@Param("lastName")String lastName);
        Incoming statement:
        <select id="getEmpByIdAndLastName" resultType="com.atguigu.mybatis.bean.Employee ">
         select * from tbl_employee where id = #{id} and last_name=#{lastName}
             </select>
        POJO:
        If multiple parameters happen to be the data model of our business logic, we can directly pass in the pojo;
        #{attribute name}: Take out the attribute value    

        Map of the incoming pojo:
        If multiple parameters are not the data in the business model, there is no corresponding pojo, and they are not used often, for convenience, we can also pass in map
        #{key}: get the corresponding values ​​in the map
Summary :
        public Employee getEmp(@Param(" id")Integer id,String lastName);
        value: id==>#{id/param1} lastName==>#{param2}

        public Employee getEmp(Integer id,@Param("e")Employee emp);
        get Value: id==>#{param1} lastName===>#{param2.lastName/e.lastName}

        ##Special attention: If it is a Collection (List, Set) type or an array, it
        will also be handled specially. It also encapsulates the incoming list or array in a map.
            key: Collection (collection), if it is List, you can also use this key (list)
                array (array)
        public Employee getEmpById(List<Integer> ids);
        value: take out the value of the first id: #{list[0] }
    Acquisition of parameters:
        #{}: You can get the value in the map or the value of the pojo object property;
        ${}: You can get the value in the map or the value of the pojo object property;


        select * from tbl_employee where id=${id} and last_name=#{ lastName}
        Preparing: select * from tbl_employee where id=2 and last_name=?
    Difference:
        #{}: In precompiled form, the parameters are set to the SQL statement; PreparedStatement; Prevent SQL injection
        ${}: The extracted value is directly Assembled in sql statement; there will be security issues; in
        most cases, we should use #{} for the value of the parameter; where
        
        native jdbc does not support placeholders, we can use ${} for value
        such as table division , sort. . . ;Split the table by year
            select * from ${year}_salary where xxx;
            select * from tbl_employee order by ${f_name} ${order}

    #{}: Richer usage:
        some rules for specifying parameters:
        javaType, jdbcType , mode (stored procedure), numericScale,
        resultMap, typeHandler, jdbcTypeName, expression (functions to be supported in the future);

        jdbcType usually needs to be set under certain conditions:
        when our data is null, some databases may not recognize the default handling of null by mybatis. For example, Oracle (error);
        
        JdbcType OTHER: invalid type; because mybatis maps all nulls to the OTHER type of native Jdbc, oracle cannot handle it correctly;
        
        because in the global configuration: jdbcTypeForNull=OTHER; oracle does not support; two methods
        1. #{email,jdbcType=OTHER};
        2. jdbcTypeForNull=NULL
            <setting name="jdbcTypeForNull" value="NULL"/>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324785564&siteId=291194637