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.
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.
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.
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.
Just wrap you SQL statement in CDATA:
<![CDATA[
SELECT * FROM CampaignStats WHERE statsDate >= #{start} AND statsDate <= #{end}
]]>
I have found the answer by myself: '<' and '>' have certain meanings in xml files, so the '>=' should be '>=' while the '<=' should be '<='.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With