Mapper XML Specification

Contributor contributes new data tables to Apache Linkis. When writing Mapper XML, the following specifications must be followed for development.

  • In mapper.xml namespace is equal to java interface address
  • The method name in the java interface is the same as the id of the statement in XML
  • The input parameter type of the method in the java interface is the same as the type specified by the parameterType of the statement in XML
  • The return value type of the method in the java interface is the same as the type specified by the resultType of the statement in XML
  • All mysql keywords in XML use lowercase uniformly
  • Abstract SQL fragments for excessive query fields
  • It is recommended to use Integer for the integer return value type, which can distinguish between unassigned and 0 cases. For example, if the return value is determined to be a number, int can be used. Other data types are similar.
  • For placeholders, use #{name} instead of ${name}. Fuzzy query can use CONCAT(‘%’,#{sname},’%’)
  • For sql statement writing, no annotation method is used, and it is uniformly written in the XML file
Method NameDescriptionCore PointsRecommendations
insertNew dataIf it is an auto-incrementing primary key, it should return the primary key ID
deleteByIdDelete data according to the primary key IDsql adds limit 1 by default to prevent multiple deletion of dataThis method is not recommended, it is recommended to logically delete
updateByIdModify data according to the primary key IDsql adds limit 1 by default to prevent multiple data modification
selectByIdQuery data by primary keyQuery a piece of data
selectByIdForUpdateQuery data according to the primary key lockQuery a piece of data by locking, for transaction processing
queryListByParamQuery data list according to input conditionsMulti-parameter query list
queryCountByParamThe total number of queries based on input conditionsThe number of multi-parameter queries

The java interface must contain @Param, and the XML can not contain parameterType

  1. // java interface
  2. User selectUserById(@Param("id") Integer id);
  3. // XML file
  4. <select id="selectUserById" resultType="userMap">
  5. select id, name
  6. from user
  7. where id = #{id}
  8. </select>
  1. // java interface
  2. List<User> userListByIds(@Param("ids") List<Integer> ids);
  3. // XML file
  4. <select id="userListByIds" resultMap="userMap">
  5. select id, name
  6. from user
  7. where id in
  8. <foreach collection="ids" separator="," open="(" close=")" item="item">
  9. #{item}
  10. </foreach>
  11. </select>
  1. // java interface
  2. User queryByParams(@Param("map") Map<String, Object> parasms);
  3. // XML file
  4. <select id="queryByParams" resultMap="userMap">
  5. select id, name
  6. from user
  7. where id = #{map.id} and name = #{map.name}
  8. </select>
  1. // java interface
  2. User queryByUser(@Param("user") User user);
  3. // XML file
  4. <select id="queryByUser" resultMap="userMap">
  5. select id, name
  6. from user
  7. where id = #{user.id} and name = #{user.name}
  8. </select>
  1. // java interface
  2. User queryByIdAndName(@Param("id") Integer id, @Param("name") String name);
  3. // XML file
  4. <select id="queryByIdAndName" resultMap="userMap">
  5. select id, name
  6. from user
  7. where id = #{id} and name = #{name}
  8. </select>

Use spaces and indentation reasonably to enhance readability. Examples of various types of SQL statements are as follows

  1. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  2. <mapper namespace="org.apache.linkins.dao.mapper.UserMapper">
  3. -- add a statement
  4. <insert id="insert">
  5. insert into user (id, name)
  6. values ​​(1, 'z3');
  7. </insert>
  8. -- delete statement
  9. <delete id = "deleteUserByIdAndName">
  10. delete from user
  11. where name = #{name}
  12. and id = #{id}
  13. </delete>
  14. -- modify the statement
  15. <update id="updateUserById">
  16. update user
  17. set name = #{name}
  18. where id = #{id}
  19. </update>
  20. -- Check for phrases
  21. <select id="selectUserByName" resultMap="userMap">
  22. select id, name
  23. from user
  24. where name = 'z3'
  25. </select>
  26. -- sql fragment
  27. <sql id="user">
  28. id,
  29. name
  30. </sql>
  31. -- Quote
  32. <include refid="user"/>
  33. -- resultMap
  34. <resultMap type="Assets" id="userMap">
  35. <id property="id" column="id" />
  36. <result property="name" column="name" />
  37. </resultMap>
  38. -- Quote
  39. <select id="queryListByParam" parameterType="map" resultMap="userMap">
  40. do...
  41. </select>
  42. -- conditional judgment
  43. <if test="name != null and name != ''">
  44. name = #{name}
  45. </if>
  46. -- sub query
  47. <select id="selectUserByTeacherIdAndName" resultMap="userMap">
  48. select u.id, u.name
  49. from user u
  50. where u.name in (
  51. select t.name
  52. from teacher t
  53. where t.id = 1
  54. and t.name = 'z3'
  55. )
  56. and u.id = 2
  57. </select>
  58. </mapper>