Spring-Boot整合通用PageHelper插件遇到的问题

 

 

 

 

 

 

一、POM依赖(没问题的

 

 

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.5.12.RELEASE</version>
	<relativePath/>
	<!-- lookup parent from repository -->
</parent>

<!--PageHelper分页插件-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper-spring-boot-starter</artifactId>
	<version>1.1.3</version>
</dependency>

 

 

 

 

配置文件

 

# 主数据源,默认的
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#注意SpringBoot2.0+ 这里的数据源url是: jdbc-url 而不是 url
spring.datasource.url=jdbc\:mysql\://localhost\:3306/slave?useUnicode\=true&autoReconnect=true&useSSL=false&characterEncoding\=utf-8&useSSL=true&allowMultiQueries=true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root



#===========pagehelper插件的配置使用的是默认的参数,因此下面不需要配置,知道就行

#pagehelper.helperDialect=mysql
#pagehelper.reasonable=true
#pagehelper.supportMethodsArguments=true
#pagehelper.params=count=countSql

 

 

如上配置,没有问题!!!!!

 

 

 

 

 

 

二、POM依赖,升级PageHelper版本至1.2.+(有问题

 

 

 

同步GitHub上的版本,更新到最近的1.2.7版本如下

 

 

 

 

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.5.12.RELEASE</version>
	<relativePath/>
	<!-- lookup parent from repository -->
</parent>

<!--PageHelper分页插件-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper-spring-boot-starter</artifactId>
	<version>1.2.7</version>
</dependency>

 

 

 

 

配置文件properties需要改动一下,将原来的

 

pagehelper.helperDialect=mysql

 

改成

 

pagehelper.dialect=com.github.pagehelper.dialect.helper.MySqlDialect

 

 

因为Mybaits的版本在5.1.+后,这个地方有所变动

 

如果,你不改的话,仍然使用5.1.0之前的版本那种配置的话,项目启动是正常,但是查询的时候会异常

 

 

 

 

 

后台Console报错如下

 

 

### SQL: select                  id,name,sex               from  user                       WHERE             id in (1,2) LIMIT ?
### 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 '?' at line 8
; 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 '?' at line 8

 

 

提示查询语句语法有问题?

 

 

解决办法(其实也不是解决办法,就是这样做的),如上述说的,改PageHelper插件的配置

 

pagehelper.dialect=com.github.pagehelper.dialect.helper.MySqlDialect

 

 

改过来之后,重启项目,然后再次查询

 

 

 

 

 

这个问题因个人能力有限,无法跟踪问题,坐等作者解答吧

 

 

 

 

 

所以,最好的结合就是

 

spring-boot-starter-parent      :1.5.12.RELEASE   (2.0以下的均可)

pagehelper-spring-boot-starter  :1.1.3  (同样,1.2.0以下)

 

 

 

注意,PageHelper 1.1.0 +和 PageHelper 1.2.0+的区别 

 

 

1.2.0之前的:pagehelper.helperDialect=mysql

1.2.0之后的:pagehelper.dialect=com.github.pagehelper.dialect.helper.MySqlDialect

 

 

 

 

 

三、Spring Boot 1.0和2.0版本对比

 

 

(1)数据源不同

 

Spring Boot 1.0+  : spring.datasource.url = 数据源


Spring Boot 2.0+  : spring.datasource.jdbc-url = 数据源

 

 

(2) SpringBootServletInitializer类所在的包

 

 

Spring Boot 1.0+  : 

import org.springframework.boot.web.support.SpringBootServletInitializer;


Spring Boot 2.0+  : 

import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

 

 

(3)DataSourceBuilder类所在的包

 

 

Spring Boot 1.0+  : 

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;


Spring Boot 2.0+  : 

import org.springframework.boot.jdbc.DataSourceBuilder;

 

 

四、最后

 

 

(1)不要以为版本高就是好的,就是亮点,就能吸引我们冒险去升级我们项目中的依赖版本

 

(2)Spring-Boot 2.0+ 不支持  pagehelper-spring-boot-starter 1.1.0+版本,应该说 pagehelper-spring-boot-starter 1.1.0+和Spring-Boot 2.0+不兼容才对,但是1.2.0+又存在我上述讲过的问题

 

(3)Spring-Boot 2.0+的特性不适合我,目前项目中也用不到,所以建议不要急着升级Spring Boot的版本

 

(4)结论

 

 

 

 

  

 

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值