mybatis关联查询时两张表有相同字段导致映射错误表1
表2
两张表都存在name字段
xml中的配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.liziy.dao.StudentDao">
<!-- 查询所有学⽣信息, 多表连接使⽤resultMap -->
<select id="selectAllStu" resultMap="studentResultMap">
SELECT
s.id,
s.name,
s.sex,
s.age,
class_id,
c.id ,
c.name
FROM tb_student s
LEFT JOIN tb_class c ON class_id = c.id
</select>
<!-- 映射Student对象的resultMap -->
sql语句怎么查询两张表的数据<resultMap id="studentResultMap" type="ity.Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<!-- 多对⼀关联:association -->
<association property="clazz" javaType="ity.Clazz">
<id property="id" column="id"/>
<result property="code" column="code"/>
<result property="name" column="name"/>
</association>
</resultMap>
</mapper>
控制台的输出
页⾯的json数据
clazz-name的值错误了
解决⽅法
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.liziy.dao.StudentDao">
<!-- 查询所有学⽣信息, 多表连接使⽤resultMap -->
<select id="selectAllStu" resultMap="studentResultMap">
SELECT
s.id,
s.name,
s.sex,
s.age,
class_id,
c.id cid,
c.name cname
FROM tb_student s
LEFT JOIN tb_class c ON class_id = c.id
</select>
<!-- 映射Student对象的resultMap -->
<resultMap id="studentResultMap" type="ity.Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<!-- 多对⼀关联:association -->
<association property="clazz" javaType="ity.Clazz">
<id property="id" column="cid"/>
<result property="code" column="code"/>
<result property="name" column="cname"/>
</association>
</resultMap>
</mapper>
为SQL语句增加别名关联的部分也要修改(红⾊部分为修改的内容)
问题解决
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论