Add, delete, check and modify the mapper.xml file in mybatis

1. Mybatis working principle

2. Inquiry

<select id="方法名" parameterType="方法参数类型" resultType="方法返回值类型,全类名" >
select 表中字段名 AS 结果字段名 from 表名 where 条件
<!--注意:结果字段名与属性名保持一致,区分大小写-->
</select>

3. Insert

<insert id="需要实现的接口里的方法名" parameterType="方法参数名,如果是对象,要写全类名">
insert into 表名 (字段1,字段2,...) values(#{参数1},#{参数2},...)
<!--注意属性名区分大小写-->
</insert>

4. Modify

<update id="updateCustomer" parameterType="com.wang.po.Customer">
         update t_customer set username=#{username},jobs=#{jobs}
         ,phone=#{phone} where id=#{id}
</update>

5. Delete

 <delete id="deleteCustomer" parameterType="Integer">
        delete from t_customer where id=#{id}
    </delete>

6.resultType analysis (I encountered this problem when I first wrote)

1. Basic type: resultType = basic type

2. List type: resultType = type of elements in List

3.map type: single record: resultType = map

                        Multiple records = type of value in Map

4. Object type: resultType = full class name of the object

7. Parameter processing

1. Single parameter: directly use # {parameter name} for value, mybatis has no special treatment, parameter name can be written casually

2. Multiple parameters: use # {parameter}, # {param2} to take the value

3. Named parameters: Use @param ("key") to specify the key in the package map, and then you can get the parameter value through # {key}

4. POJO: If multiple parameters happen to be the business model, then you can pass in the business model at this time, you can get the value through # {property name}

5.Map: If multiple parameters are not business models and are not used frequently, you can customize the Map to pass in

6. TO: If multiple parameters are not business models and are frequently used, you can customize a TO to transfer objects.

Reference: mybatis-parameter processing

8. The difference between # {} and $ {}

1. # {} Is a pre-compilation process, and $ {} is a string replacement.

2. When Mybatis is processing # {}, it will replace # {} in sql with a?, And call the set method of PreparedStatement to assign the value

3. When Mybatis processes $ {}, it replaces $ {} with the value of the variable.

4. Using # {} can effectively prevent SQL injection and improve system security.

Published 30 original articles · Like1 · Visits1158

Guess you like

Origin blog.csdn.net/chunchunlaila/article/details/104757404