how to map java.util.Date to MySql Date in Ibatis when I take input through JSON
Use Timestamp. Consider Joda time plug. ...and read this answer.
These three will absolutely do the magic.
Good luck!
Aneesh Garg
Hands-On Developer & Data Science Enthusiast – Enterprise Applications & Web Based Solutions Engineering Expertise in web and business software arenas. Full Stack Developer with genuine interest in all software technologies. Over 10 years of work experience on every layer of multi-layered software architecture. Expert in architecting, prototyping, designing, developing, testing and maintaining scalable end-to-end enterprise software products that deliver content and services to different client applications and customers. Proficiencies include: Agile/Scrum development High Performance Solutions Data Intensive Applications Production Support
Updated on June 04, 2022Comments
-
Aneesh Garg almost 2 years
I am taking date of birth as input using JSON
{"dateOfBirth":"1973-08-26"}
This field exists in
Person.java
classimport java.util.Date; public class Person { Date dateOfBirth; //Some other fields public void setDateOfBirth(Date dateOfBirth) { this.dateOfBirth = dateOfBirth; } public Date getDateOfBirth() { return dateOfBirth; } }
This is mapped to person table in mysql database. I am querying the database like:
entityId = (Long) session.selectOne("ValidatePerson", registerUserRequestParams);
Following are the entries I am making in my mapper.xml
<select id="ValidatePerson" parameterMap="ValidatePersonMap" resultType="long"> select person.entityId from person where //Some other Validation checks <if test="dateOfBirth != null"> and person.dateOfBirth = #{dateOfBirth} </if> ); </select>
I have a prameter Map as
<parameterMap id="ValidatePersonMap" type="java.util.HashMap"> <parameter property="dateOfBirth" javaType="java.util.Date" jdbcType="DATE" mode="IN"/> </parameterMap>
I am not able to get any result from database.It does not select any row even though value exists.I have checked that none of other validation checks are failing. If I pass dateOfBirth as null in JSON then then I get result.
I have also written a test case and setting request as follows:
Date dob = new Date(73,7,26); request.setDateOfBirth(dob);
When I pass values from test case as mentioned above I get result from database. Problem occurs only when i get request parameters using json.
- The format of JSOn and the format stored in DB are same
- One work around I have is to manually convert java.util.Date to String in above format and pass it as string. But this is pretty bad approach and client would not like it.
-
Aneesh Garg over 12 yearsthe formats are same in JSON and DB... this JSON is produced using gson library...