使用mybatis进行一对多嵌套查询时出错:输出结果:Country{id=2, name='美国', minister=[null]}
即Minister类作为Country类的关联属性。 查询的输出结果是:Country{id=2, name='美国', minister=[null]}
<!--mapper.xml内容-->
<!--第二次查询-->
<select id="findMinisterById" resultType="com.abc.beans.Minister">
select mid,mname from minister where countryId = #{cid}
</select>
<resultMap id="AndSelectCountry" type="com.abc.beans.Country">
<id property="id" column="cid"/>
<result property="name" column="cname"/>
<collection property="minister"
column="cid"
ofType="com.abc.beans.Minister"
select="findMinisterById">
</collection>
</resultMap>
<!--第一次查询-->
<select id="findById" resultMap="AndSelectCountry">
select cid,cname from country where cid = #{id}
</select>
//测试类
@Test
public void test01()
{
Country country = cmDao.findById(2);
System.out.println(country);
}
这种错误是不容易找的,因为没有报错信息!!
总结:
1. 在resultMap中的collection或者association范围内:
查看collection标签中column的值是不是第一次所查询 (此时我这个是findById查询)的数据表中的字段名。比如我这个查询出了cid字段,cname字段。
column="cid“,也没有错误
2.在第二次查询的范围内找:
结果找到了,因为 select mid,mname from minister where countryId = #{cid}中mid,mname和Minister实体类的属性不一致,导致数据没法封装进Minister,所以Country的minister属性才为空。
mybatis中用resultMap解决属性名和字段名不一致的问题
最终又增加一个resultMap标签解决了:
<select id="findMinisterById" resultMap="com">
select mid,mname from minister where countryId = #{cid}
</select>
<resultMap id="com" type="com.abc.beans.Minister">
<id property="id" column="mid"/>
<result property="name" column="mname"/>
</resultMap>
<resultMap id="AndSelectCountry" type="com.abc.beans.Country">
<id property="id" column="cid"/>
<result property="name" column="cname"/>
<collection property="minister"
column="cid"
ofType="com.abc.beans.Minister"
select="findMinisterById">
</collection>
</resultMap>
<select id="findById" resultMap="AndSelectCountry">
select cid,cname from country where cid = #{id}
</select>
输出结果:Country{id=2, name='美国', minister=[Minister{id=4, name='赵柳'}, Minister{id=3, name='王五'}]}
使用mybatis进行一对多嵌套查询时出错:输出结果:Country{id=2, name='美国', minister=[null]}的更多相关文章
- mybatis 13: 一对多关联查询
业务背景 根据客户id查询客户基本信息,以及客户存在的订单信息 两张数据表 客户表 订单表 实体类 客户实体类:Customer private Integer id; private String ...
- Mybatis使用MySQL进行模糊查询时输入中文检索不到结果
Mybatis使用MySQL进行模糊查询时输入中文检索时,需要在jdbcURL后增加参数 ?useUnicode=true&characterEncoding=UTF-8
- mybatis collection 一对多关联查询,单边分页的问题总结!
若想直接通过sql实现多级关联查询表结构得有2 个必不可少的字段:id ,parentId,levelId id:主键id, parentId:父id levelId:表示第几级(表本身关联查询的时候 ...
- MyBatis:一对多关联查询
MyBatis从入门到放弃四:一对多关联查询 前言 上篇学习了一对一关联查询,这篇我们学习一对多关联查询.一对多关联查询关键点则依然是配置resultMap,在resultMap中配置collecti ...
- mybatis处理一对多的查询
//查询出某个班级对应的所有老师和学生 1.使用嵌套结果 <select id="findClasses3" parameterType="int" re ...
- mybatis实现一对多连接查询
问题:两个对象User和Score,它们之间的关系为一对多. 底层数据库为postgresql,ORM框架为mybatis. 关键代码如下: mybatis配置文件如下: mybatis.xml文件内 ...
- mybatis一对一 和 一对多 嵌套查询
实际项目中的,接口对外VO 会出现 一对一 和 一对多的情况,举例:小区 下面有 楼栋 ,楼栋 下面有 房屋 , 房屋里面又房间 小区Vo : districtVo { id: nam ...
- mybatis中使用where in查询时的注意事项
我使用的时候collection值为mapper的参数名如:int deleteRoleByUserIds(@Param("userIds") String[] userIds); ...
- mybatis查询时使用基本数据类型接收报错-attempted to return null from a method with a primitive return type (int)
一.问题由来 自己在查看日志时发现日志中打印了一行错误信息为: 组装已经放养的宠物数据异常--->Mapper method 'applets.user.mapper.xxxMapper.xxx ...
随机推荐
- HTML- 锚点实例
<!DOCTYPE HTML> <html> <head> <meta charset='utf-8'> <title>锚点实例</t ...
- create Excel file - snippet
http://www.codepal.co.uk/show/Line_breaks_lost_and_br_tags_show_when_exporting_to_Excel_file Protec ...
- Python运行出错
(1)ValueError: You are trying to load a weight file containing 6 layers into a model with 5 layers. ...
- Dubbo 系列(07-2)集群容错 - 服务路由
目录 Dubbo 系列(07-2)集群容错 - 服务路由 1. 背景介绍 1.1 继承体系 1.2 SPI 2. 源码分析 2.1 创建路由规则 2.2 RouteChain 2.3 条件路由 Dub ...
- VM虚拟机启动centos出现内部错误
内部错误 解决办法 1.关闭虚拟机后,单击VM,右键,以管理员身份运行. 2.找到电脑的 管理— Vmware workstation server ,默认状态下是手动,把他改为自动重启就可以啦.
- 一个spark SQL和DataFrames的故事
package com.lin.spark import org.apache.spark.sql.{Row, SparkSession} import org.apache.spark.sql.ty ...
- 银行贷款(dp)
链接:https://www.nowcoder.com/acm/contest/79/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536KS ...
- PHP字符串操作函数练习20191025
<?php$arr=["tom","peter","mary"];$str=implode("+",$arr);/ ...
- HTML页面仿iphone数字角标
做仿iphone样式的数字角标,用简单的css来实现 <html><head><title>角标数字</title><style type=&qu ...
- LeetCode Linked List Easy 21. Merge Two Sorted Lists
Description Merge two sorted linked lists and return it as a new list. The new list should be made b ...