mybatis系列-07-输出映射

时间:2023-03-09 07:29:56
mybatis系列-07-输出映射

7.1     resultType

  使用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="UserQueryVo" resultType="int">
SELECT count(*) FROM USER where sex = #{userCustom.sex} AND username LIKE '%${userCustom.username}%'
  </select>

7.1.1.3              mapper.java

 //用户信息综合查询总数
public int findUserCount(UserQueryVo userQueryVo);

7.1.1.4              测试代码

  @Test
public void testFindUserCount(){
SqlSession sqlSession = sqlSessionFactory.openSession(); //创建UserMapper对象,mybatis自动生成mapper代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); //创建包装对象,设置查询条件
UserQueryVo userQueryVo = new UserQueryVo();
User user = new User();
user.setSex("1");
user.setUsername("张");
userQueryVo.setUserCustom(user); //调用userMapper的方法
int count = userMapper.findUserCount(userQueryVo);
System.out.println(count);
}

7.1.1.5              小结

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

7.1.2     输出pojo对象和pojo列表

  不管是输出的pojo单个对象还是一个列表(list中包括pojo),在mapper.xml中resultType指定的类型是一样的。

  在mapper.java指定的方法返回值类型不一样:

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

    //根据id查询用户信息
  public User findUserById(int id);

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

//根据用户名模糊查找用户列表
public List<User> findUserByUsername(String name);

  生成的动态代理对象中是根据mapper方法的返回值类型确定是调用selectOne(返回单个对象调用)还是selectList (返回集合对象调用 ).

7.2     resultMap

  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              定义reusltMap

   <!-- 定义resultMap
将SELECT id id_,username username_ FROM USER 和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);

7.2.2.4              测试

   @Test
public void testFindUserByIdResultMap() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession(); //创建UserMapper对象,mybatis自动生成mapper代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); //调用userMapper的方法
User user = userMapper.findUserByIdResultMap(1); System.out.println(user.getUsername());
}

7.3     小结

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

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