JavaWeb学习笔记-mybatis-15-输出映射

resultType
使用resultType进行输出映射,只要查询出来的列名和POJO中的属性名一致,该列才能映射成功。
如果查询出来的列名和pojo中的属性名全部不一致,没有创建pojo对象
只要查询出来的列名和pojo的属性有一个一致,就会创建对象

简单类型
用户信息的综合查询列表总数,通过查询总数和上边用户综合查询列表才能实现分页

    <!--用户信息的综合查询-->
    <!--#{userCustom.sex}取出包装类型中性别值-->
    <!--${userCustom.username}取出包装类型中姓名值-->
    <select id="findUserList" parameterType="com.sws.entity.UserQueryVo" resultType="com.sws.entity.UserCustom">
      select * from user where  user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
    </select>

    <!--用户信息综合查询总数-->
    <select id="findUserCount" parameterType="com.sws.entity.UserQueryVo" resultType="int">
         select count(*) from user where  user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
    </select>
    //UserMapper.java
    //用户信息综合查询
    public UserCustom findUserList(UserQueryVo userQueryVo)throws Exception;

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

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

输出pojo和pojo列表
不管输出的是单个对象还是列表,mapper.xml中resultType指定的类型是一样的,在mapper.java中指定的方法值类型不一样
输出单个pojo,返回值是单个对象
输出列表,返回值list<pojo>

resultMap
mybatis中使用resultMap完成高级输出结果映射
如果查询出来的列名和pojo属性不一致,通过定义一个resultMap对列名和pojo属性名之间做一个映射关系

    <!--定义resultMap-->
    <!--type:resultMap最终映射的Java对象类型,可以使用别名-->
    <resultMap type="User" id="userResultMap">
        <!--id表示查询结果集中唯一标识-->
        <!--column:查询出来的列名-->
        <!--property:type指定的pojo属性名-->
        <id column="id_" property="id"/>
        <!--result:对普通列的对应-->
        <result column="username_" property="usernamae"/>
    </resultMap>
    <!--使用resultMao进行输出映射-->
    <!--resultMap:指定resultMap的id,如果resultMap在其他mapper文件中,前面需加上namespace-->
    <select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap">
        select id id_,username username_ from user where id = #{value}
    </select>
//使用resultMap输出
public User findUserByIdResultMap(int id)throws Exception;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值