MyBatis: ResultMap implements advanced result set mapping

Posted by Bendude14 on Sun, 09 Jan 2022 16:25:45 +0100

  • When taking values, #{} the corresponding position in the extracted sql statement is placeholder?, ${} fills in the value.

    • {} set parameters to sql statements by precompiling. You can use sql injection.

    • The value taken out by ${} is directly spliced in the sql statement.
    • In most cases, use #{}.
    • Use ${} when placeholders are not supported by native jdbc
  • javaType: usually needs to be set in some specific environment.

    • Some databases that do not support null values, such as Oracle

    • When passing values, indicate that jdbcType=NULL

      #{email,jdbcType=null}
      
    • You can also modify JDBC typefornull to NULL in the global configuration file

  • When querying, if the return value type is list, set the type of elements in the list collection when setting the return value in the mapping file configuration.

  • When querying the map of a record, set the return value to map when configuring in the mapping file.

    • Key is the column name and Value is the corresponding Value
  • When querying multiple records and encapsulating them into a Map, if you need to specify a key, you need to set it through annotation.

    @MapKey("id")
    

ResultMap implements advanced result set mapping

  • ResultMap can customize result set mapping rules.
  • Only one ResultMap and ResultType can be used
  • Add a class attribute to map each data in the database. And give the id (unique id) to the resultMap on the select tag
<resultMap id="MyGuild" type="com.boerk.mybatis.pojo.Guild">
   <id column="guild_id" property="guildID"></id>
   <result column="guild_name" property="guildName"></result>
   <result column="guild_pwd" property="guildPwd"></result>
   <result column="guild_job" property="guildJob"></result>
   <result column="guild_belief" property="guildBelief"></result>
   <result column="guild_influence" property="guildInfluence"></result>
   <result column="guild_specialty" property="guildSpecialty"></result>
   <result column="guild_level" property="guildLevel"></result>
</resultMap>
<select id="getGuildId" resultMap="MyGuild">
  • When you join queries, you can use cascading attributes to encapsulate the result set.

    • When an entity class is a property of another entity class.

    • For example, deptNO and deptName are attributes of the dept object, and the dept object is an attribute of emp. Therefore, when packaging

      <result column="dept_name" property="dept.deptName"/>
      
    • Or use the association tag directly: you can specify the federated javabean object. (defined inside resultMap)

      <!--property="dept": Specify which is a federated object
      	javaType="com.boerk.batis.pojo.Dept": Specifies the type of Federated object.
      -->
      <association property="dept" javaType="com.boerk.batis.pojo.Dept"></association>
      
    • You can query step by step through the association tag

      <!--select="xx.xx"Indicates that the current property is a call select Results found by the specified method
      	column="d.id": Specifies which column value to pass to this method-->
      <association property="dept" select="xxx.xx" column="d.id"></association>
      
    • Delayed loading can be achieved by configuring lazyloading enabled = true and aggressiveLazyLoading=false in the global configuration. sql will be executed only when necessary to save resources.

    • Use the clooection tag to define the encapsulation rules of the attributes associated with the collection type.

      <resultMap id="hello" type="com.boerk.mybatis.pojo.Department">
          <id column="department_id" property="departmentId"></id>
          <result column="department_name" property="departmentName"></result>
          <collection property="list" ofType="com.boerk.mybatis.pojo.Employee">
               <id column="employee_id" property="employeeId"></id>
               <result column="last_name" property="lastName"></result>
               <result column="email" property="email"></result>
          </collection>
      </resultMap>
      
    • Discriminator: discriminator. mybatis can use discriminator to determine the value of a column, and then change the encapsulation behavior according to the value of a column

      • column: judge according to the value of this class
      • javaType: the type of this class
      • Judge by changing the value in value (similar to switch)