首页 > 其他 > 详细

Mybatis 的输出结果封装

时间:2019-12-16 23:02:03      阅读:107      评论:0      收藏:0      [点我收藏+]
resultType 配置结果类型
  Dao 接口
/**
* 查询总记录条数
* @return
*/
int findTotal();
  映射配置
<!-- 查询总记录条数 --> 
<select id="findTotal" resultType="int">
    select count(*) from user;
</select>
resultMap 标签
可以建立查询的列名和实体类的属性名称不一致时建立对应关系。从而实现封装。
在 select 标签中使用 resultMap 属性指定引用即可。同时 resultMap 可以实现将查询结果映射为复杂类
型的 pojo,比如在查询结果映射对象中包括 pojo 和 list 实现一对一查询和一对多查询。
定义 resultMap
<!-- 建立 User 实体和数据库表的对应关系
传智播客——专注于 Java、.Net 和 Php、网页平面设计工程师的培训
北京市昌平区建材城西路金燕龙办公楼一层 电话:400-618-9090
type 属性:指定实体类的全限定类名
id 属性:给定一个唯一标识,是给查询 select 标签引用用的。
--> 
<resultMap type="com.itheima.domain.User" id="userMap"> 
  <id column="id" property="userId"/>   <result column="username" property="userName"/>   <result column="sex" property="userSex"/>   <result column="address" property="userAddress"/>   <result column="birthday" property="userBirthday"/> </resultMap> id 标签:用于指定主键字段 result 标签:用于指定非主键字段 column 属性:用于指定数据库列名 property 属性:用于指定实体类属性名称
映射配置
<!-- 配置查询所有操作 -->
<select id="findAll" resultMap="userMap">
  select * from user
</select>

 

Mybatis 的输出结果封装

原文:https://www.cnblogs.com/naigai/p/12051695.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!