mybatis output mapping resultType&resultMap

  • Use conditions of resultType&resultMap
    • When the fields of the table are consistent with the attributes of the class, that is, when the attributes of the class correspond to the fields of the table one-to-one, use resultType, otherwise use resultMap.
    • If you output a simple type, a single POJO object, or a list of POJOs, use resultMap.
    • Code to achieve the number of queries
      • Methods in the interface
      /**
       * 查询user的个数
       * @param vo
       * @return
       */
      public int findUserCount(UserQueryVo vo);
      
      • Configuration of the mapping file
      <select id="findUserCount" parameterType="userQueryVo" resultType="int">
          SELECT COUNT(*) FROM user WHERE sex = #{user.sex}
      </select>
      
      • Implementation
      @Test
      public void test1(){
          UserMapper userMapper = session.getMapper(UserMapper.class);
          //获取数据
      
          //通过模型的包装类来查询用户
          UserQueryVo query = new UserQueryVo();
      
          User user = new User();
          user.setSex("2");
          query.setUser(user);
          int count = userMapper.findUserCount(query);
          System.out.println("女性的个数为:"+count);
      
      }
      
    • Use resultMap when the value queried by the table no longer corresponds to the attribute value of the class one-to-one
      • In addition to writing the corresponding method in the interface, configuring the mapping file and implementing the corresponding method, you also need to set the return data resultMap
        • Profile code
        <resultMap id="userResultMap" type="com.Marsoft.model.User">
            <id property="id" column="id_"></id>
            <result property="username" column="username_"></result>
            <result property="sex" column="sex_"></result>
            <result property="birthday" column="birthday_"></result>
            <result property="address" column="address_"></result>
        </resultMap>
        <select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap">
            SELECT id id_,
            username username_,
            sex sex_,
            birthday birthday_,
            address address_
            FROM USER WHERE id = #{id};
        </select>
        
        1. The id of the resultMap tag is the value of the resultMap attribute of the select tag. Here, the query is based on the id, so other attributes of the table are in the result tag.
        2. The value of type is a specific entity class
        3. The value of the property in the result tag corresponds to the property in the entity class
        • Methods in the interface
        /**
         * 查询的结果作为resultMap传出
         * @param id
         * @return
         */
        public User findUserByIdResultMap(int id);
        
        • Implementation
        @Test
        public void test2(){
            UserMapper userMapper = session.getMapper(UserMapper.class);
            //获取数据
        
            //通过模型的包装类来查询用户
            User user = userMapper.findUserByIdResultMap(1);
            System.out.println(user);
        
        }
        

Guess you like

Origin blog.csdn.net/weixin_45566730/article/details/114638419