java - Comparing timestamps with mybatis, postgresql -
i'm using mybatis query on postgres db, problem comparaison between timestamps, think not working since doesn't retrun needed results not throwing exception. here code
<select id="select_count" parametertype="map" resulttype="map"> select count(*) count, to_char(created_on, #{xaxis}) xaxis,state my_table 1 = 1 <if test="fromdate != null"> , created_on >= #{fromdate} </if> <if test="todate != null"> , created_on <= #{todate} </if> <if test="state != null"> , state = #{state} </if> group xaxis, state </select>
and here java code
public list<map<string, object>> getstatescount(date fromdate, date todate, string state, string xaxis) { map<string, object> params = new hashmap<string, object>(); params.put("fromdate", fromdate); params.put("todate", todate); params.put("state", state); params.put("xaxis", "yyyy-mm-dd"); list<map<string, object>> sqlresults = (list<map<string, object>>) template.selectlist(select_count, params); return sqlresults; }
and code creating my_table
create sequence my_table_seq start 1 increment 1; create table my_table ( id int8 not null default nextval('my_table_seq'), state varchar(20), created_on timestamp time zone, primary key (id), );
i know table has many rows 'created_on' prop set , let's say, today's date. when pass 2 date params or 1 one of them , select query doesn't return data.
so how query timestamps , java.util.date
?
i posting correct answer.
here code
<select id="select_count" parametertype="map" resulttype="map"> select count(*) count, to_char(created_on, #{xaxis}) xaxis,state my_table 1 = 1 <if test="fromdate != null"> , created_on >= #{fromdate} </if> <if test="todate != null"> , created_on <= #{todate} </if> <if test="state != null"> , state = #{state} </if> group xaxis, state </select>
and here java code
public list<map<string, object>> getstatescount(date fromdate, date todate, string state, string xaxis) { map<string, object> params = new hashmap<string, object>(); params.put("fromdate", fromdate); params.put("todate", todate); params.put("state", state); params.put("xaxis", "yyyy-mm-dd"); list<map<string, object>> sqlresults = (list<map<string, object>>) template.selectlist(select_count, params); return sqlresults; }
and code creating my_table
create sequence my_table_seq start 1 increment 1; create table my_table ( id int8 not null default nextval('my_table_seq'), state varchar(20), created_on timestamp time zone, primary key (id), );
Comments
Post a Comment