mybatis入门(四)----输入映射和输出映射

一:输入映射 

  通过parameterType指定输入参数的类型,类型可以是简单类型、hashmappojo的包装类型。

1.1.传递pojo的包装对象

 1.1.1.需求描述

    完成用户信息的综合查询,需要传入的查询条件可能很复杂(可能包括用户信息,其它信息,比如,商品,订单等等)。

 1.1.2.定义包装类型的pojo

    针对上边的需求,建议使用自定义的包装类型pojo,在包装类型的pojo中将复杂的查询条件包装进去。

包装查询条件的pojo类UserQueryVo类的代码:

View Code

 UserCustom类的代码

View Code

 UserMapper.xml的代码

  在UserMapper.xml中定义用户信息综合查询(假设查询条件很复杂,通过高级查询复杂关联查询)

View Code

UserMapper.java类的代码

View Code

Junit单元测试代码

View Code

二:输出映射

1.resultType

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

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

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

1.1.输出简单类型

   需求:用户信息的综合查询列表,通过查询总数才能实现分页功能。‘

   UserMapper.xml的代码 

复制代码
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <!-- namespace命名空间,作用就是对sql进行分类化的管理,理解为sql隔离
 6     注意:使用mapper代理开发时,namespace有特殊作用,namespace等于mapper接口地址
 7  -->
 8 <!-- 用户信息综合查询总数
 9      parameterType:指定输入类型,和findUserList一致。
10      resultType:输出结果类型.    
11   -->
12 <mapper namespace="com.mybatis.mapper.UserMapper">
13     <select id="findUserCount" parameterType="com.mybatis.entity.UserQueryVo" resultType="int">
14         select count(*) from t_user where  sex=#{userCustom.sex} and username like '%${userCustom.username}%'
15     </select>
16 </mapper>
复制代码

UserMapper.java的代码

1 public interface UserMapper {
2     //用户信息综合查询总数
3     public int findUserCount(UserQueryVo userQueryVo);
4 }

Junit测试代码

View Code

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

1.2.输出pojo对象和pojo列表   

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

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

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

1 public interface UserMapper {
2     /** 根据ID查询用户信息 */
3     public User findUserById(int id);
4 }

 1.2.2.输出pojo对象list,方法返回值是List<pojo>

1 public interface UserMapper {
2     /** 根据用户名称模糊查询用户信息 */
3     public List<User> findUserByName(String username);
4 }

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

1.3.输出类型resultMap

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

 1.3.1.resultMap使用方法

    (1).定义resultMap

    (2).使用resultMap作为statement的输出映射类型

 1.3.2.demo例子

  (1).需求:将下面的sql使用User完成映射

    select id id_,username username_ from t_user where id=?

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

  (2).定义resultMap   

复制代码
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2  <!DOCTYPE mapper
 3  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5   <!-- namespace命名空间,作用就是对sql进行分类化的管理,理解为sql隔离
 6     注意:使用mapper代理开发时,namespace有特殊作用,namespace等于mapper接口地址
 7   -->
 8    <!-- 用户信息综合查询总数
 9      parameterType:指定输入类型,和findUserList一致。
10       resultType:输出结果类型.    
11    -->
12  <mapper namespace="com.mybatis.mapper.UserMapper">
13  <!-- 定义resultMap 
14      select id id_,username username_ from t_user和User类中的属性作为一个映射关系
15     type:resultMap最终映射的java对象类型,可以使用别名;
16      id:对resultMap的唯一标识
17  -->
18  <resultMap type="user" id="userResultMap">
19      <!--id表示查询结果中的唯一标识,
20          column:查询出来的列名
21          property:type指定的pojo类型中的属性名
22         最终resultMap对column和property作一个映射关系(对应关系)
23       -->
24      <id column="id_" property="id"/>
25      <!--result:表示对普通列名的映射,
26         column:查询出来的列名
27        property:type指定的pojo类型中的属性名
28          最终resultMap对column和property作一个映射关系(对应关系)
29        -->
30      <result column="username_" property="username"/>
31  </resultMap>
32  </mapper>
复制代码

  (3).使用resultMap作为statement的输出类型映射

复制代码
1 <!-- 使用resultMap作为输出映射
2     resultMap:指定定义的resultMap的id,如果这个resultMap在其它的mapper.xml文件中,前边需要添加namespace命名空间
3  -->
4 <select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap" >
5     select id id_,username username_ from t_user where id=#{id}
6 </select>
复制代码

UserMapper.java代码

public interface UserMapper {

    /** 根据ID查询用户信息,使用resultMap进行输出 */
    public User findUserByIdResultMap(int id);
}

Junit测试代码:

View Code

小结:

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

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

原文地址:https://www.cnblogs.com/yanyan0520/p/8758412.html