本文介绍了MyBatis String作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MyBatis中为Select语句使用String参数。
我的mapper.xml:

I want to use a String parameter for a Select Statement in MyBatis.My mapper.xml:

<select id="selectAll" parameterType="String" resultMap="fastXMLResultMap">
        SELECT CREATIONDATE, DOCUMENTID, TITEL, REGTITEL, INFORCEDATE, DOCTYPE
        FROM #{databBaseTable}
</select>

调用函数:

public List<FastXMLObject> selectAll(String databBaseTable) {

    SqlSession session = sqlSessionFactory.openSession();

    System.out.println("Table: "+databBaseTable);

    try {
        List<FastXMLObject> list = session.selectList("FastXMLObject.selectAll",databBaseTable);
        return list;
    } finally {
        session.close();
    }
}

字符串dataBaseTable是我的表的名称

The string dataBaseTable is the name of the table of my database (who would have thought that) because I want to get data dynamically from verious tables.

但是不幸的是,这不工作:错误:ORA-00903:UngültigerTabellenname无效表名),但它isnt。当我打印出databBaseTable的值时,它是表的确切名称。当我把表的名字写到我的mapper.xml没有变量它工作。
我做错了什么?

But unfortunatelly this does not work: Error: ORA-00903: Ungültiger Tabellenname (invalid table name) but it isnt. When I print out the value of "databBaseTable" it is the exact name of the table. And when I write the name of the table to my mapper.xml without a variable it works.What do I do wrong?

推荐答案

使用 $ {dataBaseTable} 而不是'#'。不同之处在于'#'用于PreparedStatement替换。 '$'是直接的字符串替换。

Use ${dataBaseTable} instead of '#'. The difference is that '#' is used for PreparedStatement substitution. '$' is for direct String substitution.

但是,如果这样做,你不能将表名作为参数传递给 selectList()调用。您需要将表名设置为属性。可以通过使用MyBatis config.xml中的< properties> 元素或直接在代码中使用 Configuration.getVariables code>。

However, if you do this, you can't pass the table name in as a parameter to the selectList() call. You need to set the table name as a property. Properties can be set by using the <properties> element in the MyBatis config.xml or directly in code by using Configuration.getVariables().

请参阅。

这篇关于MyBatis String作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:22