Skip to content
网游世界
网游世界

吾生有涯,而知无涯。

  • 首页
  • PHP
    • ThinkPHP
    • FastAdmin
    • webman
  • JavaScript
    • jQuery
    • AdminLTE
  • Free Pascal
  • Java
    • JeeSite
    • 若依
    • ruoyi-vue-pro
  • 其它
    • 操作系统
    • 树莓派
    • 前端
    • Null
  • 关于
网游世界

吾生有涯,而知无涯。

RuoYi Vue 表字段使用别名

3Vshej, 2024年3月15日 周五2024年3月15日 周五

在开发中需要用到表联查,而联查的表正好有重名字段。这时候再获取子表值时,会获取错误,将拿到的是父表的值。

像如下 WechatCategoryArticleListMapper.xml 文件:

留意 association 添加的位置 ,否则会报错误:Caused by: org.xml.sax.SAXParseException: 元素类型为 “resultMap” 的内容必须匹配 “(constructor?,id*,result*,association*,collection*,discriminator?)”

<?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.techpsp.wechatapi.mapper.WechatCategoryArticleListMapper">

    <resultMap type="WechatCategoryArticleList" id="WechatCategoryArticleListResult">
        <result property="id" column="id"/>
        <result property="categoryId" column="category_id"/>
        <result property="name" column="name"/>
        <result property="banner" column="banner"/>
        <result property="tips" column="tips"/>
        <result property="content" column="content"/>
        <result property="productName" column="product_name"/>
        <result property="productList" column="product_list"/>
        <result property="itemName" column="item_name"/>
        <result property="itemBanner" column="item_banner"/>
        <result property="itemList" column="item_list"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <association property="wechatCategory" javaType="WechatCategory" resultMap="WechatCategoryResult"/>
    </resultMap>

    <resultMap id="WechatCategoryResult" type="WechatCategory">
        <id property="id" column="id"/>
        <result property="categoryName" column="category_name"/>
        <result property="childName" column="child_name"/>
    </resultMap>

    <sql id="selectWechatCategoryArticleListVo">
        select a.id,
               a.category_id,
               a.name,
               a.banner,
               a.tips,
               a.content,
               a.product_name,
               a.product_list,
               a.item_name,
               a.item_banner,
               a.item_list,
               a.create_by,
               a.create_time,
               a.update_by,
               a.update_time,
               b.name as category_name,
               b.child_name
        from wechat_category_article as a
                 LEFT JOIN wechat_category as b ON a.category_id = b.id
    </sql>

    <select id="selectWechatCategoryArticleList" parameterType="WechatCategoryArticleList"
            resultMap="WechatCategoryArticleListResult">
        <include refid="selectWechatCategoryArticleListVo"/>
        <where>
            <if test="name != null  and name != ''">and a.name like concat('%', #{name}, '%')</if>
        </where>
    </select>

    <select id="selectWechatCategoryArticleListById" parameterType="Integer"
            resultMap="WechatCategoryArticleListResult">
        <include refid="selectWechatCategoryArticleListVo"/>
        where a.id = #{id}
    </select>
</mapper>

1、通过 as 定义表别名,通过 as 定义字段别名

2、对子表返回类型重新使用新的字段别名

3、实体类中定义 get/set 方法

如 domain/WechatCategory.java 文件

private String categoryName;

public void setCategoryName(String categoryName)
{
    this.categoryName = categoryName;
}

public String getCategoryName()
{
    return categoryName;
}

这就造成,WechatCategory 实体对象,返回一个表中并不存在的字段。而在联查操作时会有值,单独使用时,会是 null。

另外,记录下另一个问题:

WARN o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver – [logException,208] – Resolved [org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘art’ in ‘class com.techpsp.wechatapi.domain.WechatCategoryArticleList’]

<select id="selectWechatCategoryArticleList" parameterType="WechatCategoryArticleList"
        resultMap="WechatCategoryArticleListResult">
    <include refid="selectWechatCategoryArticleListVo"/>
    <where>
        <if test="a.name != null  and a.name != ''">and a.name like concat('%', #{name}, '%')</if>
    </where>
</select>

错误原因是,<if test=”a.name != null and a.name != ””>,使用了a 别名,if test 时,无法获取这个属性。

移除 a 别名即可,即修改后:

<if test=”name != null and name != ””>and a.name like concat(‘%’, #{name}, ‘%’)</if>

相关文章:

  1. RuoYi Vue 实现字段排序 默认情况下,RuoYi Vue 并没有附加排序条件。这时候需要增加一个默认排序。...
  2. RuoYi Vue 接口注解示例 若依框架分离版,使用 Swagger 来生成接口文档,本文简单介绍下,如何编写这个接口文档。...
  3. RuoYi Vue 代码生成 若依 VUE 版提供了代码生成功能,这节省大量重复性工作。...
  4. RuoYi Vue 生成代码结构介绍 以 /ruoyi-student/src/main/ 目录为示例,也就是 com.ruoyi.stu......
Java 若依 RuoYi Vue 框架表字段

文章导航

Previous post
Next post

近期文章

  • Android Studio Gradle 配置国内镜像
  • 为什么重新发明轮子
  • ruoyi-vue-pro 匿名访问
  • VUE 中接收 code 异常
  • 关于 AI

归档

  • 2025 年 4 月
  • 2025 年 3 月
  • 2025 年 2 月
  • 2025 年 1 月
  • 2024 年 12 月
  • 2024 年 11 月
  • 2024 年 10 月
  • 2024 年 9 月
  • 2024 年 8 月
  • 2024 年 7 月
  • 2024 年 6 月
  • 2024 年 5 月
  • 2024 年 4 月
  • 2024 年 3 月
  • 2024 年 2 月
  • 2024 年 1 月
  • 2023 年 12 月
除非特殊说明,本站作品采用知识共享署名 4.0 国际许可协议进行许可。
豫公网安备 41010402002622号 豫ICP备2020029609号-3
©2025 3Vshej