mybatis @Select 静态工厂关键字 sql 编译时出错?

mybatis @Select with a static factory of sql keyword get a error when compile?

当我使用 @Select 时,它是带有代码的 ibatis 注释:

@Select({"SELECT ", selectField, " FROM ", table, "WHERE finished = #{finished}"})
 List<ToDo> listToDoByFinished(@Param("finished") Integer finished);

它工作正常并且可以编译通过,但是当我更改代码时:

//FIXME
@Select({SELECT, selectField, FROM, table, WHERE, finished, EQUAL, "#{finished}"})
 List<ToDo> listToDoByFinished(@Param("finished") Integer finished);

无法编译通过,错误信息为:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project blog: Compilation failure
[ERROR] /home/freeze/my_project/lbfreeze-blog/src/main/java/me/freezehome/blog/dao/ToDoDAO.java:[32,54] 错误(error): 不兼容的类型(type mismatch): Integer无法转换为String(Interger cannot convert to String)
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

SqlFactory中定义的sql关键词:

package me.freezehome.blog.factory;

public  class SqlFactory {
    private SqlFactory(){}
    public static final  String SELECT = " SELECT ";
    public static final  String INSERT = " INSERT ";
    public static final  String UPDATE = " UPDATE ";
    public static final  String DELETE = " DELETE ";

    public static final  String WHERE = " WHERE ";
    public static final  String FROM = " FROM ";
    public static final  String AND = " AND ";
    public static final  String OR = " OR ";
    public static final  String IN = " IN ";
    public static final  String INTO = " INTO ";
    public static final  String VALUES = "VALUES";
    public static final  String EQUAL = " = ";


}

接口中定义的其他:

package me.freezehome.blog.dao;

import me.freezehome.blog.driver.MyInLanguageDriver;
import me.freezehome.blog.model.ToDo;
import org.apache.ibatis.annotations.*;

import java.util.List;

import static me.freezehome.blog.config.StringConfig.strSeparator;
import static me.freezehome.blog.factory.SqlFactory.*;

public interface ToDoDAO {
    String table = "todo";
    String id = "id";
    String title = "title";
    String content = "content";
    String finished = "finished";

    String selectField = id + strSeparator + title + strSeparator + content + strSeparator + finished;
    String insertField = title + strSeparator + content;
}

其他信息:

我猜你有 import static SqlFactory。*

在 ToDoDAO 接口中,所有 String 都应为 static final,因为注释需要常量参数。

我已经在我的小应用程序上试过这段代码。结果,Java 编译器将尝试引用与常量同名的方法参数。

在这种情况下,Java 编译将尝试引用方法参数(finished 定义为 Integer)而不是 String finished = "finished";(常量)。因此在编译时发生类型转换错误。

我不知道它是否是 Java 编译器的规范。

也许您可以通过将常量名称转换为大写来解决此问题。