Review mybatis framework (1) ---- mapping file

  Refer to the blogger's article and respect the original: https://blog.csdn.net/qq_35246620/article/details/54837618

1. Give a summary of the mapping file Mapper.xml:

① Set the namespace namespace in order to distinguish the methods in the mapping file;

② The result set resultMap is the biggest feature of MyBatis, and the ORM of the object is converted by it:

  • In the result set, including the primary key id and the common attribute result;
  • In the result set, two commonly used properties are: property, which represents the property of the entity; column, which represents the column of the result set of the SQL query.

③ In the mapping file, there are four commonly used tags: select, insert, update and delete:

  • Each tag has an id attribute, and the id is not allowed to be repeated in the same mapper file;
  • The parameter parameterMap has been abandoned , and now its purpose is to be compatible with previous projects;
  • The parameter parameterType supports many types, such as int, Integer, String, Double, List, Map or entity objects;
  • The return value resultType is used for simple types;
  • The return value resultMap is used for complex types;
  • When the parameter and return value is a collection, it declares the element type in the collection;
  • SQL statements are not case-sensitive, and it uses PrepareStatement by default, which is precompiled and can prevent SQL injection.

④ The way to get parameters is #{ field name }

 

Second, the dynamic SQL statement:

  <!-- Query function, parameterType sets the parameter type, resultType sets the return value type -->
    <select id="findAll" parameterType="yeepay.payplus.Person" resultType="Person">  <!-- 书写 SQL 语句 -->
        SELECT id,name,age FROM person
        <where>
            <if test="name =! null">
                name = #{name}
            </if>
            <if test="age =! null">
                and age = #{age}
            </if>
        </where>
    </select>

 

    <!-- Modify function-->
    <update id="update" parameterType="yeepay.payplus.Person">
        UPDATE person
        <set>
            <if test="name =! null">
                name = #{name},
            </if>
            <if test="age =! null">
                age=#{age},
            </if>
        </set>
        WHERE id = #{id}
    </update>

  

3. Delete function, three methods are given to complete the batch delete function. The parameter types are: Array, List and Map.

<!-- Batch deletion, the id to be deleted of Array type is taken in the following array: new Integer[]{ 2, 3, 4} -->
    <delete id="deleteArray" parameterType="integer">
        DELETE FROM person WHEN id IN
        <foreach collection="array" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>  

 

    <!-- Batch delete, List type list={1,2,3} --> 
    < delete id ="deleteList" parameterType ="integer" >
        DELETE FROM person WHEN id IN
        <foreach collection="list" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

 

   <!-- Batch delete, Map type map.put{} -->
<!-- When testing this method with the following two statements, only records with age = 18 in id = 3, 4, 5 are actually deleted -->
map.put("ids", new Integer[]{2, 3, 4}); map.put("age",18);
<delete id="deleteMap" parameterType="Map"> DELETE FROM person WHERE id IN <foreach collection="ids" item="id" open="(" close=")" separator=","> #{id} </foreach> AND age = #{age} </delete>

 4. Summary

When using dynamic SQL statements, we need to pay more attention to the following points:

  1. The if tag is used to determine whether the field is empty. If it is empty, it will not participate in the SQL statement by default, and the comma can be automatically omitted;
  2. The condition completion judgment is output through the where tag, which can automatically omit redundant and and commas;
  3. The modification operation is completed through the set tag. When the field value is null, it does not participate in the SQL statement;
  4. In the foreach tag, the collection attribute represents the incoming parameter set, item represents the name of each element variable, open represents the start character, close represents the end character, and separator represents the separator;
  5. Any parameter can be encapsulated in a Map, which takes the value by key.

 

5. Note that when using the ORACLE database, you need to add the default jdbcType to each field

When testing the above functions, an exception will be reported, that is, "java.sql.SQLException: invalid column type (in outsourcing projects, the third party requires Oracle, and this problem is often encountered ) ". The reason:

In the Oracle database, when adding or modifying operations, if the field value is null, the default type of the field must be specified.

 <!-- In Oracle data, complete the modification function -->
<!-- The type of jdbcType is the type of the field in the database, which requires strict correspondence. -->

     <update id="update" parameterType="yeepay.payplus.Person">

        UPDATE person
        <set>
            name = #{name,jdbcType=VARCHAR},
            age=#{age,jdbcType=INTEGER}
        </set>
        WHERE id = #{id}
    </update>

 

Guess you like

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