MyBatis Mapper Xml中使用枚举进行比较导致的异常:invalid comparison

背景

<if test="aggByAdDate">
    <choose>
        <when test="granularity == @com.example.enums.Granularity@DAY">
            DATE(record_time),
        </when>
        <when test="granularity == @com.example.enums.Granularity@HOUR">
            DATE_FORMAT(record_time,'%Y-%m-%d %H'),
        </when>
        <otherwise>
            record_time,
        </otherwise>
    </choose>
</if>
@Getter
@AllArgsConstructor
public enum Granularity implements EnumBase {
    /**
     * 时间粒度.
     */
    DAY(1, "天级") {
        @Override
        public LocalDateTime truncate(LocalDateTime dateTime) {
            return dateTime.truncatedTo(ChronoUnit.DAYS);
        }

        @Override
        public LocalDateTime plus(LocalDateTime dateTime, int delta) {
            return dateTime.plusDays(delta);
        }
    }   
}

上面MyBatis的mapper xml定义会出现下面的异常:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IllegalArgumentException: invalid comparison: com.example.enums.Granularity$3 and com.example.enums.Granularity$1
### Cause: java.lang.IllegalArgumentException: invalid comparison: com.example.enums.Granularity$3 and com.example.enums.Granularity$1

分析和问题分解

1、考虑到是否为依赖包版本的问题,导致类型不兼容的问题

2、在相应的堆栈打上断点,查看具体的值和类型

问题解决记录

1、回退之前的版本是可以成功执行

2、对比现今版本,主要是枚举在声明时进行了方法重写。

3、在相应的堆栈打上断点,查看具体的值和类型。

结果:两个枚举的值的类型不一致,导致Ognl无法进行比较。

4、为什么会出现类型不一致呢?

枚举重写方法时,会生成相应的匿名类,导致每个枚举的类型和原类型不一致,从而导致比较时出现不兼容的现象

解决:

<if test="aggByAdDate">
    <choose>
        <when test="granularity.code == @com.example.enums.Granularity@DAY.getCode()">
            DATE(record_time),
        </when>
        <when test="granularity.code == @com.example.enums.Granularity@HOUR.getCode()">
            DATE_FORMAT(record_time,'%Y-%m-%d %H'),
        </when>
        <otherwise>
            record_time,
        </otherwise>
    </choose>
</if>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
MyBatis Mapper XML is a configuration file used in MyBatis, a Java-based persistence framework, to define SQL mappings between Java objects and database tables. The Mapper XML file contains SQL statements and mapping rules that are used to interact with the database. In the Mapper XML file, you define the SQL statements such as SELECT, INSERT, UPDATE, DELETE, etc., using the MyBatis XML syntax. You also define the mapping rules to map the result of the SQL queries to Java objects or vice versa. Here is an example of a simple Mapper XML file: ```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.example.UserMapper"> <select id="getUserById" resultType="com.example.User"> SELECT * FROM users WHERE id = #{id} </select> <insert id="insertUser" parameterType="com.example.User"> INSERT INTO users (id, name, email) VALUES (#{id}, #{name}, #{email}) </insert> <update id="updateUser" parameterType="com.example.User"> UPDATE users SET name = #{name}, email = #{email} WHERE id = #{id} </update> <delete id="deleteUser" parameterType="int"> DELETE FROM users WHERE id = #{id} </delete> </mapper> ``` In this example, the Mapper XML file defines four SQL statements: `getUserById`, `insertUser`, `updateUser`, and `deleteUser`. Each statement has an ID, parameterType (input parameter), resultType (output result mapping), and the actual SQL query. You can then use the defined SQL statements in your Java code by referencing the Mapper XML file and the statement ID using MyBatis API. Note that the actual mapping between Java objects and database tables is usually done through Java annotations or XML configuration files in addition to the Mapper XML file. The Mapper XML file primarily focuses on defining the SQL statements and their mappings. I hope this gives you a basic understanding of MyBatis Mapper XML. Let me know if you have any further questions!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木子的木木

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值