关于mybatis的思考(3)——ResultMaps的使用

 

    ResultMap元素在mybatis中非常重要,目的是告诉mybatis将从结果集中取出的数据转换为开发者需要的对象。

    UserMapping.xml

    <!-- selectAll操作
   resultType="map"表示返回的是一个map对象 -->
    <select id="selectAll" resultType="map">
        select * from tb_user
    </select>

    SelectMapTest.java

/**
 * fayuan.com Inc.
 * Copyright (c) 2017-2018 All Rights Reserved.
 */
package com.fayuan.test;

import com.fayuan.factory.SqlSessionFactoryClass;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

/**
 * 测试ResultMaps功能
 *
 * @author fayuan.fzw
 * @version $Id: SelectMapTest.java, v 0.1 2018年02月25日 下午11:54 fayuan.fzw Exp $
 */
public class SelectMapTest {
    public static void main(String[] args) {

        SqlSession sqlSession = SqlSessionFactoryClass.getSqlSession();

        List<Map<String, Object>> list = sqlSession.selectList("com.fayuan.mapper.UserMapper.selectAll");

        for (Map<String, Object> row : list) {
            System.out.println(row);
        }

        sqlSession.close();
    }
}

  执行打印出来的日志:

DEBUG [main] - ==>  Preparing: select * from tb_user 
DEBUG [main] - ==> Parameters: 
DEBUG [main] - <==      Total: 6
{sex=?, name=fayuan, id=2, age=24}
{sex=?, name=fayuan, id=3, age=24}
{sex=?, name=fayuan, id=4, age=24}
{sex=?, name=fayuan, id=5, age=24}
{sex=?, name=fayuan, id=6, age=24}
{sex=?, name=fayuan, id=7, age=24}

  

  

 

posted @ 2018-02-26 00:17  motivated_Dou  阅读(164)  评论(0编辑  收藏  举报