java MyBatis 中的多个参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12489022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:06:52  来源:igfitidea点击:

Multiple parameters in MyBatis?

javamybatis

提问by jerry_sjtu

I know this question is asked many times and I still get problems when I follow the guidewhen including multiple parameters in my select query. Here is my configuration file:

我知道这个问题被问了很多次,当我按照指南在我的选择查询中包含多个参数时,我仍然遇到问题。这是我的配置文件:

<select id="selectByDate" parameterType="map" resultMap="campaignStats">
    SELECT * FROM CampaignStats WHERE statsDate >= #{start} AND statsDate <= #{end}
</select>

Here is my Java code:

这是我的Java代码:

public List<DpCampaignStats> selectByDate(Date start, Date end){
    SqlSession session = sqlSessionFactory.openSession();
    try {
        Map<String, Date> map = new HashMap<String, Date>();
        map.put("start", start);
        map.put("end", end);
        List<DpCampaignStats> list = session.selectList("DpCampaignStats.selectByDate", map);
        return list;
    } finally {
        session.close();
    }
}

But I get the error: java.lang.ExceptionInInitializerError which means that I have some errors in my configuration file and I cannot find the reason.

但是我收到错误消息:java.lang.ExceptionInInitializerError 这意味着我的配置文件中有一些错误,我找不到原因。

采纳答案by jerry_sjtu

I have found the answer by myself: '<' and '>' have certain meanings in xml files, so the '>=' should be '&gt;=' while the '<=' should be '&lt;='.

我自己找到了答案:'<' 和 '>' 在 xml 文件中有一定的含义,所以 '>=' 应该是 ' > =' 而 '<=' 应该是 ' < ='。

回答by Isaac Levin

Just wrap you SQL statement in CDATA:

只需将您的 SQL 语句包装在 CDATA 中:

<![CDATA[
   SELECT * FROM CampaignStats WHERE statsDate >= #{start} AND statsDate <= #{end}
]]>