mybatis中resultMap的几种用法
来源:互联网 发布:mac品牌竞争对手 编辑:程序博客网 时间:2024/06/05 14:51
基本定义
- resultMap自定义结果集映射规则
- resultMap与resultType只能二选一
<resultMap type="emp" id="emps"> <id column="id" property="id" /> <result column="last_name" property="lastName" /> <result column="email" property="email" /> <result column="gender" property="gender"/> </resultMap> <select id="getResultMap" resultMap="emps" databaseId="mysql"> select last_name, email,id,gender from employee where id = #{id} </select>
- ID标签对应主键属性
2.resultMap要对应相应resultMap的id
关联查询,级联属性封装
<resultMap type="emp" id="emps"> <id column="id" property="id" /> <result column="last_name" property="lastName" /> <result column="email" property="email" /> <result column="gender" property="gender"/> <result column="id" property="departMent.id"/> <result column="dept_name" property="departMent.departmentName" /> </resultMap>
<select id="getEmpAndDep" resultMap="emps" databaseId="mysql"> select * from employee e,department d where e.d_id=d.id and e.id=#{id} </select>
利用association定义关联对象封装规则
- association可以指定联合的javaBean对象
- property=“”指定哪个属性是联合的对象
- javaType 指定这个属性对象的类型(不能省略)
<resultMap type="emp" id="empsk"> <id column="id" property="id" /> <result column="last_name" property="lastName" /> <result column="email" property="email" /> <result column="gender" property="gender"/> <association property="departMent" javaType="dep"> <id column="did" property="id" /> <result column="dept_name" property="departmentName" /> </association> </resultMap> <select id="getEmpAndDep" resultMap="empsk" databaseId="mysql"> select e.last_name last_name, e.email email, e.id,e.gender gender,e.d_id ,d.id did, d.dept_name dept_name from employee e,department d where e.d_id=d.id and e.id=#{id} </select>
利用association进行分步查询
- 先按照员工id查询员工信息
- 根据查询员工信息中的d_id值去部门表查询
- 部门设置到员工中
- select表明当前属性是调用select指定的方法查出的结果
- column 指定将哪一列的值传给这个方法
<resultMap type="emp" id="emps3"> <id column="id" property="id" /> <result column="last_name" property="lastName" /> <result column="email" property="email" /> <result column="gender" property="gender"/> <association property="departMent" select="com.dao.DepartMentMapper.getDepartMent" column="d_id"> </association> </resultMap> <select id="getEmpAndDep" resultMap="emps3" databaseId="mysql"> select last_name,email,id,gender,d_id from employee e where id=#{id} </select>
分布查询之延迟加载
<setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/>
- 需要在全局配置文件中加入这两个设置
- lazyLoadingEnabled 会导致分布查询时候按需加载,只有在用到某个属性或对象的时候才会去查询
- aggressiveLazyLoading 侵入懒加载是指,当需要某个对象的级联属性时,其它属性也会顺便加载,而false则只加载当前属性。
关联查询 collection定义关联集合封装规则
<resultMap type="dep" id="depls"> <id column="did" property="id"/> <result column="dept_name" property="departmentName"/> <collection property="empls" ofType="emp"> <id column="eid" property="id"/> <result column="last_name" property="lastName" /> <result column="email" property="email" /> <result column="gender" property="gender"/> </collection> </resultMap> <select id="getDepartMentBylist" resultMap="depls" databaseId="mysql"> select d.id did,d.dept_name,e.id eid,e.last_name,e.email,e.gender from department d left join employee e on d.id=e.d_id where d.id=#{id} </select>
- collection标签的oftype是指集合中元素的类型
- property属性指明属于哪一个属性
collection分步查询
<resultMap type="dep" id="deplf"> <id column="id" property="id"/> <result column="dept_name" property="departmentName"/> <collection property="empls" fetchType="lazy" select="com.dao.EmployeeMapper.getEmpByDid" column="id"></collection> </resultMap>
<select id="getDepartMent" resultMap="deplf"> select id, dept_name from department where id=#{id} </select>
- 分步查询需要将第一次查询的某个字段值传递给第二步作为参数
- 传递多列的值,将多列的值封装成map进行传递
column="{key=column1,key2=column2}"
,然后根据key取出对应值 - fetchType=”lazy”表示使用延迟加载,eager立即加载
discriminator 监视器属性
- 这个属性会根据某个字段值的情况,动态选择不同的映射
- 注意在case标签里面注明返回的类型,即resultType
<resultMap type="emp" id="empd"> <id column="id" property="id" /> <result column="last_name" property="lastName" /> <result column="email" property="email" /> <result column="gender" property="gender"/> <discriminator javaType="int" column="gender"> <case value="1" resultType="emp"> <association property="departMent" javaType="dep"> <id column="did" property="id" /> <result column="dept_name" property="departmentName" /> </association> </case> <case value="0" resultType="emp"> <id column="id" property="id" /> <result column="email" property="lastName" /> <result column="email" property="email" /> <result column="gender" property="gender"/> </case> </discriminator> </resultMap>
阅读全文
0 0
- mybatis中resultMap的几种用法
- Mybatis的ResultMap用法
- mybatis 中resulttype 与resultmap用法
- mybatis关联查询的的resultMap 用法
- mybatis的resultType和resultMap基础用法
- MYBATIS中resultMap和parameterMap的使用
- mybatis中resultMap和resultType的区别
- MYBATIS中resultMap和parameterMap的使用
- MyBatis中resultMap和resultType的使用
- MyBatis中ResultMap和ResultType的区别
- MyBatis中resultType和resultMap的区别
- mybatis中resulttype和resultmap的区别
- MyBatis中resultType和resultMap的区别
- MyBatis中resultMap和parameterMap的使用
- mybatis 中 foreach标签的几种用法
- Mybatis中resultMap
- Mybatis中resultMap
- Mybatis中resultMap
- AlexNet论文学习
- 【Python】类的多重继承 、深度优先、广度优先
- MyCnCart专业版之高级评论功能
- Spark2 ML包之决策树分类Decision tree classifier详细解说
- LintCode 练习-734. 形式为a^i b^j c^k的子序列数量
- mybatis中resultMap的几种用法
- HDOJ 1096 A+B for Input-Output Practice (VIII)
- 安卓蓝牙开发
- 微信扫码登录原理解析
- codeforces 266A Stones on the Table
- 神经网络之激活函数(sigmoid、tanh、ReLU)
- Java集合:HashMap源码剖析
- 【转】 设置Ubuntu上的MySQL可以远程访问
- Win10x64下Vbox安装Ubuntux64共享文件夹报错:/sbin/mount.vboxsf: mounting failed with the error: Protocol error