手把手教你--Mybatis 动态表名,查询数据

最近项目中使用到根据时间进行查询表的需求(即表名是根据时间变化的)

mapper.java 文件

public Gpsinfo selectGpsByPlateNo(@Param("tableName")String tableName,@Param("plateNo")String plateNo);


mapper.xml 文件

以${}就相当于字符串拼接,而#{}相当于占位符,所有这里需要在${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;  
}

表名是我根据时间进行拼接的







你可能感兴趣的:(手把手教你--Mybatis 动态表名,查询数据)