test代碼如下:
@Test
public void testHistoryData() throws ParseException {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDateTime = format.parse("2020-04-01 00:00:00");
Date endDateTime = format.parse("2020-04-01 05:00:00");
List<Baseline> data = mapper.findByTableName1(
"baseline_32_171",startDateTime,
endDateTime);
data.forEach(s -> {
System.out.println(s);
});
}
mapper介面如下:
List<Baseline> findByTableName1(@Param("tableName") String tableName,
@Param("startDateTime") Date startDateTime,
@Param("endDateTime") Date endDateTime
);
xml檔案如下:
<select id="findByTableName1" parameterType="wnlbswit.ssm.entity.BaselineExample"
resultMap="BaseResultMap" statementType="STATEMENT">
select * from ${tableName}
where DateTime
between #{startDateTime}
and #{endDateTime}
</select>
但是Java后臺報錯:
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?
and ?' at line 6
### The error may exist in wnlbswit/ssm/mapper/BaselineMapper.xml
### The error may involve wnlbswit.ssm.mapper.BaselineMapper.findByTableName1
### The error occurred while executing a query
### SQL: select * from baseline_32_171 -- where DateTime >= ? -- and DateTime <= ? where DateTime between ? and ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?
and ?' at line 6
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?
and ?' at line 6
請問是哪里的問題啊!感覺xml文件沒啥問題啊,大佬能指點一下嘛
uj5u.com熱心網友回復:
BETWEENDATE_FORMAT( #{startDateTime} ,'%Y-%m-%d %H:%i:%s')
AND
DATE_FORMAT( #{endDateTime},'%Y-%m-%d %H:%i:%s')
uj5u.com熱心網友回復:
select * from baseline_32_171 -- where DateTime >= ? -- and DateTime <= ? where DateTime between ? and ?
日志記錄的SQL為什么會帶有"-- where DateTime >= ? -- and DateTime <= ?"這一段
uj5u.com熱心網友回復:
好像還是不行,感覺引數沒傳進去uj5u.com熱心網友回復:
這是我之前的條件,注釋了,不知道為什么日志還會有uj5u.com熱心網友回復:
弄好了哈,謝謝轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/96016.html
標籤:Java EE
上一篇:[Verilog]同步FIFO案例 --- 引數化的module
下一篇:求大佬們幫忙
