[已解决]mybatis联合查询能查询到数据,但不能绑定到bean上去的问题

今天写一个mybatis的联合查询,出现了一个非常奇怪的问题,一个message bean里面绑定了两个其他bean,想通过联合查询将查询到的数据绑定上去,可是发现死活都行不通
public class Message {
    private Integer messageId;

    private Integer doctorId;

    private Integer patientId;

    private Date talkingdate;

    private String messageInfo;
    
    private Doctor doctor;
    
    private Patient patient;
<resultMap id="BaseResultMap" type="com.wx.bobby99.bobbyClinic.bean.Message" >
    <id column="message_id" property="messageId" jdbcType="INTEGER" />
    <result column="doctor_id" property="doctorId" jdbcType="INTEGER" />
    <result column="patient_id" property="patientId" jdbcType="INTEGER" />
    <result column="talkingDate" property="talkingdate" jdbcType="DATE" />
  </resultMap>
  <resultMap id="ResultMapWithBLOBs" type="com.wx.bobby99.bobbyClinic.bean.Message" extends="BaseResultMap" >
    <result column="message_info" property="messageInfo" jdbcType="LONGVARCHAR" />
    <association property="doctor" select="getDoctorById" column="doctor_id"></association>
    <association property="patient" select="getPatientById" column="patient_id"></association>
  </resultMap>
  <select id="getDoctorById" parameterType="java.lang.String" resultType="com.wx.bobby99.bobbyClinic.bean.Doctor">
  	select * from doctor where doctor_id = #{doctor_id}
  </select>
  <select id="getPatientById" parameterType="java.lang.String" resultType="com.wx.bobby99.bobbyClinic.bean.Patient">
  	select * from patient where doctor_id = #{patient_id}
  </select>

然后用mybatis的后台打印sql语句的方法看下mybatis到底有没有运行我写的这条语句,结果发现SQL语句正常执行,而且结果都查询出来了,但就是没有绑定到doctor和patient两个bean上去。

最后想了一个奇招,doctor和patient本身的mapper里面不是有写select语句吗(使用逆向工程生成的代码)?我就引用里面的,本来我就是用主键查询数据,然后把两个自己写的select都删掉,引用自己生成的selectByPrimaryKey

<mapper namespace="com.wx.bobby99.bobbyClinic.mapper.MessageMapper" >
  <resultMap id="BaseResultMap" type="com.wx.bobby99.bobbyClinic.bean.Message" >
    <id column="message_id" property="messageId" jdbcType="INTEGER" />
    <result column="doctor_id" property="doctorId" jdbcType="INTEGER" />
    <result column="patient_id" property="patientId" jdbcType="INTEGER" />
    <result column="talkingDate" property="talkingdate" jdbcType="DATE" />
  </resultMap>
  <resultMap id="ResultMapWithBLOBs" type="com.wx.bobby99.bobbyClinic.bean.Message" extends="BaseResultMap" >
    <result column="message_info" property="messageInfo" jdbcType="LONGVARCHAR" />
    <association property="doctor" select="com.wx.bobby99.bobbyClinic.mapper.DoctorMapper.selectByExample" column="doctor_id"></association>
    <association property="patient" select="com.wx.bobby99.bobbyClinic.mapper.PatientMapper.selectByExample" column="patient_id"></association>
  </resultMap>
select是对应的mapper的namespace加select名字,用"."隔开,最后问题解决!


展开阅读全文

没有更多推荐了,返回首页