zoukankan      html  css  js  c++  java
  • mybatis关联查询时 两张表有相同字段导致映射错误

    表1

    表2

    两张表都存在name字段

    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="cn.liziy.dao.StudentDao">
    <!-- 查询所有学生信息, 多表连接使用resultMap -->
        <select id="selectAllStu" resultMap="studentResultMap">
            SELECT
                    s.id,
                    s.name,
                    s.sex,
                    s.age,
                    class_id,
                    c.id ,
                    c.code,
                    c.name
            FROM tb_student s
            LEFT JOIN tb_class c ON class_id = c.id
        </select>
    
    <!-- 映射Student对象的resultMap   -->
        <resultMap id="studentResultMap" type="cn.liziy.entity.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="cn.liziy.entity.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 "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/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.code,
                    c.name cname
            FROM tb_student s
            LEFT JOIN tb_class c ON class_id = c.id
        </select>
    
    <!-- 映射Student对象的resultMap   -->
        <resultMap id="studentResultMap" type="cn.liziy.entity.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="cn.liziy.entity.Clazz">
                <id property="id" column="cid"/>
                <result property="code" column="code"/>
                <result property="name" column="cname"/>
            </association>
        </resultMap>
    
    </mapper>

    为SQL语句增加别名 关联的部分也要修改(红色部分为修改的内容)

    问题解决

  • 相关阅读:
    数据库学习笔记3--基本的SQL语句
    数据库学习笔记2--MySQL数据类型
    数据库学习笔记1----MySQL 5.6.21的安装和配置(setup版)
    JavaWeb学习笔记1---http协议
    Spring学习笔记18--通过工厂方法配置Bean
    Spring学习笔记17--在XML中使用SPEL
    Spring 学习笔记16---properties文件的两种方式
    Spring学习笔记15--注解Bean
    Spring4.0学习笔记1---开发环境搭建
    Installed JREs时 Standard 1.1.x VM与Standard VM的区别
  • 原文地址:https://www.cnblogs.com/lzy1212/p/13452257.html
Copyright © 2011-2022 走看看