Mybatis映射配置文件<select>的学习

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>返回一条记录的mapkey就是列名,值就是对应的值

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

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

 

<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>

 

posted @ 2017-11-16 10:10  宇的季节  阅读(679)  评论(0编辑  收藏  举报