最近项目中使用到根据时间进行查询表的需求(即表名是根据时间变化的)
mapper.java 文件
public Gpsinfo selectGpsByPlateNo(@Param("tableName")String tableName,@Param("plateNo")String plateNo);
<select id="selectGpsByPlateNo" parameterType="String" resultMap="BaseResultMap" statementType="STATEMENT">
SELECT *FROM
${tableName}
where plateNo='${plateNo}'
order by createDate desc limit 1
</select>
以${}就相当于字符串拼接,而#{}相当于占位符,所有这里需要在${plateNo}前后加上单引号
serviceImpl.java文件
@Override
public Gpsinfo selectGpsByPlate(String plateNo) {
Gpsinfo gpsinfo = new Gpsinfo();
Date date=new Date();
DateFormat format=new SimpleDateFormat("yyyyMMdd");
String tableName="gpsinfo"+format.format(date);
gpsinfo = myGpsMapper.selectGpsByPlateNo(tableName,plateNo);
return gpsinfo;
}
表名是我根据时间进行拼接的