Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple parameters in MyBatis?

Tags:

java

mybatis

I know this question is asked many times and I still get problems when I follow the guide when 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:

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.

like image 214
jerry_sjtu Avatar asked Sep 19 '12 05:09

jerry_sjtu


People also ask

What is parameter type MyBatis?

MyBatis provides various attributes for insert mapper, but largely we use id and parameter type. id is unique identifier used to identify the insert statement. On the other hand, parametertype is the class name or the alias of the parameter that will be passed into the statement.

Does MyBatis use prepared statement?

MyBatis does four main things: It executes SQL safely and abstracts away all the intricacies of JDBC. It maps parameter objects to JDBC prepared statement parameters. It maps rows in JDBC result sets to objects.

Is MyBatis a framework?

MyBatis is an open source persistence framework which simplifies the implementation of database access in Java applications. It provides the support for custom SQL, stored procedures and different types of mapping relations. Simply put, it's an alternative to JDBC and Hibernate.


2 Answers

Just wrap you SQL statement in CDATA:

<![CDATA[
   SELECT * FROM CampaignStats WHERE statsDate >= #{start} AND statsDate <= #{end}
]]>
like image 186
Isaac Levin Avatar answered Oct 16 '22 02:10

Isaac Levin


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

like image 24
jerry_sjtu Avatar answered Oct 16 '22 03:10

jerry_sjtu