Java 将时间戳与mybatis、postgresql进行比较
我使用mybatis查询我的postgres DB,我的问题是时间戳之间的比较,我认为它工作不正常,因为它没有重新运行所需的结果,即使它没有引发任何异常。 这是我的密码Java 将时间戳与mybatis、postgresql进行比较,java,timestamp,alfresco,mybatis,Java,Timestamp,Alfresco,Mybatis,我使用mybatis查询我的postgres DB,我的问题是时间戳之间的比较,我认为它工作不正常,因为它没有重新运行所需的结果,即使它没有引发任何异常。 这是我的密码 <select id="select_count" parameterType="map" resultType="map"> select count(*) as count, TO_CHAR(created_on, #{xaxis}) as xaxis,state from my
<select id="select_count" parameterType="map" resultType="map">
select count(*) as count, TO_CHAR(created_on, #{xaxis}) as xaxis,state
from my_table
where 1 = 1
<if test="fromdate != null"> and created_on >= #{fromdate} </if>
<if test="todate != null"> and created_on <= #{todate} </if>
<if test="state != null"> and state = #{state} </if>
GROUP BY xaxis, state
</select>
<select id="select_count" parameterType="map" resultType="map">
select count(*) as count, TO_CHAR(created_on, #{xaxis}) as xaxis,state
from my_table
where 1 = 1
<if test="fromdate != null"> and created_on >= #{fromdate} </if>
<if test="todate != null"> and created_on <= #{todate} </if>
<if test="state != null"> and state = #{state} </if>
GROUP BY xaxis, state
</select>
我知道我的表中有很多行的“created_on”道具设置为(比如)今天的日期。但是,当我传递两个日期参数,甚至其中一个时,select查询不会返回任何数据
那么如何正确地查询时间戳和
java.util.Date
?我给出了正确的答案
这是我的密码
<select id="select_count" parameterType="map" resultType="map">
select count(*) as count, TO_CHAR(created_on, #{xaxis}) as xaxis,state
from my_table
where 1 = 1
<if test="fromdate != null"> and created_on >= #{fromdate} </if>
<if test="todate != null"> and created_on <= #{todate} </if>
<if test="state != null"> and state = #{state} </if>
GROUP BY xaxis, state
</select>
<select id="select_count" parameterType="map" resultType="map">
select count(*) as count, TO_CHAR(created_on, #{xaxis}) as xaxis,state
from my_table
where 1 = 1
<if test="fromdate != null"> and created_on >= #{fromdate} </if>
<if test="todate != null"> and created_on <= #{todate} </if>
<if test="state != null"> and state = #{state} </if>
GROUP BY xaxis, state
</select>
我想我发现了问题:日期参数的拼写不正确,现在一切都好了,ThxMyBatis生成了什么查询?(在MyBatis中启用SQL日志记录或在
postgresql.conf
中启用log_语句='all'
)如果您认为这个问题可能对其他人有用,请发布您自己的答案,并在堆栈溢出上的新用户计时器允许时接受它;我会投票。这是我的错,我没有注意到参数的名称,它就像一个符咒,顺便问一下如何启用mybatis日志记录?我不使用mybatis,所以我必须在文档、堆栈溢出搜索或谷歌上查找它,和你一样。
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;
}
CREATE SEQUENCE my_table_seq START WITH 1 INCREMENT BY 1;
CREATE TABLE my_table
(
id INT8 NOT NULL DEFAULT NEXTVAL('my_table_seq'),
state VARCHAR(20),
created_on TIMESTAMP WITH TIME ZONE,
PRIMARY KEY (id),
);