mybatis 一对一嵌套查询,并没有执行两次sql

需求:假设有一个User类,一个Car类,一个User对应这一辆Car,查询User信息的同时查询出他的Car的信息.
结果:
图片说明

car.java

package com.wantao.bean;
import lombok.Data;

@Data
public class Car {
    private Integer carId;
    private String  carName;

}

user.java

package com.wantao.bean;
import lombok.Data;

@Data
public class User {
    private Integer userId;
    private String userName;
    private Integer cId;
    private Car car;
}

CarMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wantao.dao.CarDao">
    <select id="findCarById" resultType="com.wantao.bean.Car">
        select car_id,car_name
        from
        tb_car
        where car_id=#{id}
    </select>
</mapper>

UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wantao.dao.UserDao">
    <resultMap type="com.wantao.bean.User" id="map1">
        <id column="user_id" property="userId"></id>
        <result column="user_name" property="userName" />
        <result column="c_id" property="cId" />
        <association select="com.wantao.dao.CarDao.findCarById"
            property="car" column="car_id">
        </association>
    </resultMap>
    <select id="findUserById" resultMap="map1">
        select
        *
        from tb_user u
        where u.user_id=#{id}
    </select>
</mapper>

只希望使用association的select进行嵌套查询,我发现com.wantao.dao.CarDao.findCarById根本没有执行,不知道为什么


项目目录
图片说明

2个回答

问题已解决,column=c_id"'



Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问