您的位置:首页 > 编程语言 > Java开发

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

2018-01-23 23:04 399 查看
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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: