Mybatis----Mapper.xml中的输出映射

7.输出映射

7.1resultType

  使用resultType进行输出映射,只有查询出来的列名和POJO中的属性名一致,该列才可以映射成功。

如果查询出来的列名和POJO中的属性名全部不一致,没有创建POJO对象。

只有查询出来的列名和POJO中的属性有一个一致,就会创建POJO对象。

7.1.1输出简单类型

7.1.1.1 需求

用户信息的综合查询列表总数,通过查询总数和上边用户综合查询列表才可以实现分页。

7.1.1.2 mapper.xml

        <!-- 用户综合信息查询总数
        parameterType:指定输入类型和findUserList一样
        resultType:输出结果的类型
     -->
    <select id="findUserCount" parameterType="com.xjs.mybatis.po.UserQueryVo" resultType="int">
        select count(*) from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
    </select>

7.1.1.3 mapper.java

    //用户信息的查询总数
    public int findUserCount(UserQueryVo userQueryVo)throws Exception;

7.1.1.4 测试

//条数查询
        @Test
        public void testFindUserCount() throws Exception {
            SqlSession sqlSession=sqlSessionFactory.openSession();
            
            //创建UserMapper(dao接口)对象,mybatis自动生成dao接口实现类的代理对象
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            
            //创建包装对象 ,设置查询条件
            UserQueryVo userQueryVo=new UserQueryVo();
            UserCustom userCustom=new UserCustom();
            userCustom.setSex("1");
            userCustom.setUsername("小明");
            userQueryVo.setUserCustom(userCustom);
            //调用userMapper的方法
            int count = userMapper.findUserCount(userQueryVo);
            System.out.println("查询出来的总记录数:"+count);
        }

7.1.1.5 小结

查询出来的结果集只有一行且一列,可以使用简单类型进行输出映射。

7.1.2 输出POJO对象和POJO列表

不管是输出的单个对象还是一个列表(list中包括pojo),在mapper.xml中resultType指定的类型是一样的。在mapper.java(dao接口)指定的方法返回值类型不一样。

1、输出单个pojo对象,方法返回值是单个对象类型。

2、输出pojo对象list,方法返回值是list<pojo>。

生成的动态代理对象中,是根据mapper方法的返回值类型确定是调用selectOne还是selectList。

7.2resultMap

mybatis中使用resultMap完成高级输出结果映射。

7.2.1 resultMap使用方法

如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间做一个映射关系。

1、定义resultMap

2、使用resultMap作为statement的输出映射类型

7.2.2 将下边的sql使用User完成映射

select id id_,username username_ from user where id=#{value}

User类中属性名和上边查询列名不一致。

7.2.2.1 定义resultMap

    <!-- 定义resultMap
    将select id id_,username username_ from user where id=#{value}和user类中的属性做一个映射
    type:resultMap最终映射的Java对象类型,可以使用别名
    id:对resultMap的唯一标识
     -->
    <resultMap type="user" id="userresultMap">
        <!-- id表示查询结果集中唯一标识
            column:查询出来的列名
            property:type指定的pojo中的属性名
            最终resultMap对column和property做一个映射关系
         -->
         <id column="id_" property="id"/>
         <!-- result:对普通列的映射定义
             column:查询出来的列名
            property:type指定的pojo中的属性名
            最终resultMap对column和property做一个映射关系
          -->
         <result column="username_" property="username"/>
    </resultMap>

7.2.2.2 使用resultMap作为statement的输出映射类型

    <!-- 使用resultMap进行输出映射 
    resultMap:指定定义resultMap的id,如果这个resultMap在其它的mapper文件,前边需要加namespace
    -->
    <select id="findUserByIdResultMap" parameterType="int" resultMap="userresultMap">
        select id id_,username username_ from user where id=#{value}
    </select>

7.2.2.3 mapper.java

    //使用id查询用户信息,使用resultMap进行输出
    public User findUserByIdResultMap(int id)throws Exception;

7.2.2.4 测试

    // 使用resultMap
    @Test
    public void testFindUserByIdResultMap() throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 创建UserMapper(dao接口)对象,mybatis自动生成dao接口实现类的代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        
        User user = userMapper.findUserByIdResultMap(35);
        System.out.println(user);
    }

结果:---只查了两列

User [id=35, username=IU, sex=null, birthday=null, address=null]

 

posted @ 2019-07-25 11:20  微微亮  阅读(719)  评论(0编辑  收藏  举报