若依pageHelper分页报错
需求:使用mybatis分页查询数据库表,以HashMap格式返回
报错信息:pageHelper获取到表总数后,查询时出了问题
16:02:03.439 [http-nio-8080-exec-1] INFO o.a.c.c.C.[.[.[/] - [log,173] - Initializing Spring DispatcherServlet 'dispatcherServlet'
16:02:03.560 [http-nio-8080-exec-1] DEBUG c.r.p.m.D.queryDynamicTableDetails_COUNT - [debug,135] - ==> Executing: SELECT count(0) FROM table1
16:02:03.565 [http-nio-8080-exec-1] DEBUG c.r.p.m.D.queryDynamicTableDetails_COUNT - [debug,135] - <== Total: 1
16:02:03.566 [http-nio-8080-exec-1] DEBUG c.r.p.m.D.queryDynamicTableDetails - [debug,135] - ==> Executing: select * from table1 LIMIT ?
16:02:03.597 [http-nio-8080-exec-1] ERROR c.r.f.w.e.GlobalExceptionHandler - [handleRuntimeException,100] - 请求地址'/project/dynamicTable/list',发生未知异常.
org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: java.sql.SQLSyntaxErrorException: 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 '?' at line 2
### The error may exist in file [E:\RuoYi-Vue\ruoyi-system\target\classes\mapper\project\DynamicTableMapper.xml]
### The error may involve com.ruoyi.project.mapper.DynamicTableMapper.queryDynamicTableDetails
### The error occurred while executing a query
### SQL: select * from table1 LIMIT ?
### Cause: java.sql.SQLSyntaxErrorException: 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 '?' at line 2
- controller
@RestController
@RequestMapping("/project/dynamicTable")
public class DynamicTableController extends BaseController {
@Autowired
private IDynamicTableService dynamicTableService;
@ApiOperation("获取表格信息")
@GetMapping("/list")
public TableDataInfo list(@RequestParam String tableName, Integer pageNum, Integer pageSize)
{
startPage();
List<HashMap<String, Objects>> hashMaps = dynamicTableService.queryDynamicTableDetails(tableName);
return getDataTable(hashMaps);
}
}
- iservice
public interface IDynamicTableService {
public List<HashMap<String, Objects>> queryDynamicTableDetails(String tableName);
}
- serviceImpl
@Service
public class DynamicTableServiceImpl implements IDynamicTableService {
@Resource
private DynamicTableMapper dynamicTableMapper;
@Override
public List<HashMap<String, Objects>> queryDynamicTableDetails(String tableName) {
List<HashMap<String, Objects>> list = dynamicTableMapper.queryDynamicTableDetails(tableName);
return list;
}
}
- mapper
public interface DynamicTableMapper {
public List<HashMap<String, Objects>> queryDynamicTableDetails(String tableName);
}
- mapper.xml
<mapper namespace="com.ruoyi.project.mapper.DynamicTableMapper">
<select id="queryDynamicTableDetails" resultType="HashMap" statementType="STATEMENT">
select * from ${tableName}
</select>
</mapper>
如果不使用分页功能,可以正常返回数据。
尝试在serviceImpl中使用PageHelper.startPage(pageNum, pageSize)方法,还是同样的问题
求大佬指点
机器人说的对着呢,
statementType="STATEMENT"
错了,只有prepareStatement
才能预编译,你不要加这个设置项另外你这个SQL这么写,SQL注入严重,这个接口可以查到库中所有的数据