resultMap的使用

当查询的表的列名与实体类的属性名不同时,有三种方式来处理:

1、使用SQL的别名 如:select user_name userName from user

2、如果符合驼峰命名,在setting中开启mapUnderscoreToCamelCase

3、设置resultMap,自定义结果集映射规则,不能和resultType同时使用

例:

Type指定映射实体类

<resultMap id="BaseResultMap" type="com.cky.pojo.User" >  

    <id  column="id"  property="id" jdbcType="INTEGER" />  

    <result column="user_name"  property="userName" jdbcType="VARCHAR" />  

    <result column="password"  property="password" jdbcType="VARCHAR" />  

    <result column="age"  property="age" jdbcType="INTEGER" />  

</resultMap>

<select>返回集合

返回List

如果返回的是一个集合,ResultType返回值类型依然要写集合中元素的类型

返回Map

<select>返回一条记录的map,key就是列名,值就是对应的值

一条记录时 设置 resultType=”map”

多条记录时 设置 resultType=”Entity”一条记录的实体类,方法添加@MapKey(“id”)指定哪个属性作为map的key

<select>级联查询1:1

Sql使用多表关联查询,然后指定resultMap 如下:

方法一、

<resultMap id="BaseResultMap" type="com.cky.pojo.User" >  

    <id  column="id"  property="id" jdbcType="INTEGER" />  

    <result column="user_name"  property="userName" jdbcType="VARCHAR" />  

    <result column="password"  property="password" jdbcType="VARCHAR" />  

  <result column="age"  property="age" jdbcType="INTEGER" />  

  <result column=”c_id”  property=”course.id”>

  <result column=”c_name” property=”course.name”>

</resultMap>

方法二、

 <resultMap type="DifResultMap" id="com.cky.pojo.User">

   <id  column="id"  property="id" jdbcType="INTEGER" />  

    <result column="user_name"  property="userName" jdbcType="VARCHAR" />  

    <result column="password"  property="password" jdbcType="VARCHAR" />  

    <result column="age"  property="age" jdbcType="INTEGER" />

    <association property="course" javaType="com.cky.pojo.Course">

     <id column="c_id" property="cId" />

     <result column="c_name" property="cName"/>

    </association>

</resultMap>

方法三、分步查询:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property

<resultMap type="StepResultMap" id="com.cky.pojo.User">

   <id  column="id"  property="id" jdbcType="INTEGER" />  

    <result column="user_name"  property="userName" jdbcType="VARCHAR" />  

    <result column="password"  property="password" jdbcType="VARCHAR" />  

    <result column="age"  property="age" jdbcType="INTEGER" />
  <!-- association中指定某一select的查询结果作为property指定的对象 -->
<association property="course" select="com.cky.dao.getCourse" column="c_id"> </association> </resultMap>

优点,可以使用延迟加载,开启方式:设置setting的

lazyLoadingEnabled true

aggressiveLazyLoading false

显式的配置可以防止版本更新带来的问题

<select>级联查询1:n

方法一、嵌套结果集,sql使用连接查询

<mapper namespace="com.cky.dao.CourseDao">

<resultMap type="com.cky.pojo.Course" id="myCourse">

<id column="c_id" property="cId"/>

<result column="c_name" property="cName"/>

<collection property="students" ofType="com.cky.pojo.Student">

<id column="id" property="id"/>

<result column="user_name" property="userName"/>

<result column="password" property="password"/>

<result column="age" property="age"/>

</collection>

</resultMap>

</mapper>

方法二、分步查询

<mapper namespace="com.cky.dao.CourseDao">

<resultMap type="com.cky.pojo.Course" id="myCourse">

<id column="c_id" property="cId"/>

<result column="c_name" property="cName"/>

<collection perperty=”students”

Select=”com.cky.dao.Student.getStudentByCId”

Column=”c_id”

</collection>

</resultMap>

</mapper>

最新文章

  1. 一张关于docker版本的图
  2. eclipse乱码解决方法
  3. python模块之time
  4. net.sf.json 时间格式的转化
  5. 如何用ZBrush快速雕刻LOL中的Lissandra
  6. Vernam密码
  7. appium关于定位元素
  8. Flex4 双选下拉列表的实现(源代码)
  9. @Async java 异步方法
  10. eth0: error fetching interface information: Device not found
  11. 暑假集训单切赛第二场 UVA 10982 Troublemakers
  12. Oracle SQL篇(四)group by 分组与分组的加强 rollup
  13. windows下安装ruby和 rails的痛苦经历
  14. oschina程序开发
  15. 写得好 git 提交信息
  16. jquery如何获取url中问号后面的数值
  17. J2EE进阶(九)org.hibernate.LazyInitializationException: could not initialize proxy - no Session
  18. Roundcube Webmail跨站脚本漏洞(CVE-2015-5381 )
  19. MySQL服务器的安装和配置,MySQL Workbench 8.0.12安装,MySQL的基本使用
  20. ActiveMQ_2安装

热门文章

  1. JavaScript------如何解决表单登录信息输入为空显示提示
  2. Spinner --- 功能和用法
  3. android实现解析webservices
  4. VS2010程序打包操作(结合图片详细讲解)
  5. cxGrid 隔行换色
  6. 【BZOJ4543】[POI2014]Hotel加强版 长链剖分+DP
  7. JRE not compatible with workspace .class file compatibility: 1.7
  8. 一个页面从输入URL到页面加载完成发生了...待细化
  9. Less-mixin判断(守卫)一
  10. 导出Excel功能的3种实现