当前位置:网站首页>Why my order by create_ Time ASC becomes order by ASC

Why my order by create_ Time ASC becomes order by ASC

2022-06-12 18:34:00 InfoQ

Development is a “ Upgrade to play blame ” What happened , In various development environments , Monsters are frequent , Tactics are escalating .  Programmers want to get rid of the fog , Get a glimpse of the truth , You have to understand the moves first , Then solve it one by one .

The content of this issue “ Obtain raw material locally ”, Source self  LigaAI  Common problems in development , And pass 「 Scenario test — Analyze the differences — Propose solutions 」 Methods to help us avoid and identify this trap as soon as possible ~ Don't talk much , Come to Kangkang !

Problems encountered :
Two hairy ones sql, Paginated sql There's no problem with execution , Non paged sql There is a problem with implementation ?

01、  Scene preparation


>>  Test scenarios

CREATE TABLE `test_page_or_not` (
 `id` bigint(20) NOT NULL COMMENT ' Primary key number ',
 `name` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT ' name ',
 `create_time` datetime(3) NOT NULL COMMENT ' Creation time ',
 `create_by` bigint(20) NOT NULL COMMENT ' founder ',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT=' Test paging and non paging ';

INSERT INTO `test_page_or_not` (`id`, `name`, `create_time`, `create_by`) VALUES (1, 'name1', '2022-02-17 22:25:04.000', 0);
INSERT INTO `test_page_or_not` (`id`, `name`, `create_time`, `create_by`) VALUES (2, 'name2', '2022-02-17 22:25:04.000', 0);
INSERT INTO `test_page_or_not` (`id`, `name`, `create_time`, `create_by`) VALUES (3, 'name3', '2022-02-18 22:25:04.000', 0);
INSERT INTO `test_page_or_not` (`id`, `name`, `create_time`, `create_by`) VALUES (4, 'name4', '2022-02-19 22:25:04.000', 0);

some Java Class file :
public class TestWithPageDTO extends PageDTO {

 private static final long serialVersionUID = -970059691509424681L;

 private String name;

 private String orderBy;

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }

 public String getOrderBy() {
 return orderBy;
 }

 public void setOrderBy(String orderBy) {
 this.orderBy = orderBy;
 }

 @Override
 public String toString() {
 return "TestWithPageDTO{" +
 "name='" + name + '\'' +
 ", orderBy='" + orderBy + '\'' +
 '}';
 }
}


public class PageDTO extends BaseDTO {

 private static final long serialVersionUID = 2572899663737669356L;

 private Integer pageSize = 10;

 private Integer pageNum = 1;

 public Integer getPageSize() {
 return pageSize;
 }

 public void setPageSize(Integer pageSize) {
 this.pageSize = pageSize;
 }

 public Integer getPageNum() {
 return pageNum;
 }

 public void setPageNum(Integer pageNum) {
 this.pageNum = pageNum;
 }

 @Override
 public String toString() {
 return "PageDTO{" +
 "pageSize=" + pageSize +
 ", pageNum=" + pageNum +
 '}';
 }
}


public class BaseDTO implements Serializable {

 private static final long serialVersionUID = 9055050419546393543L;
}



public class TestWithoutPageDTO extends BaseDTO {

 private static final long serialVersionUID = 7862152821778815456L;

 private String name;

 private String orderBy;

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }

 public String getOrderBy() {
 return orderBy;
 }

 public void setOrderBy(String orderBy) {
 this.orderBy = orderBy;
 }

 @Override
 public String toString() {
 return "TestWithoutPageDTO{" +
 "name='" + name + '\'' +
 ", orderBy='" + orderBy + '\'' +
 '}';
 }
}

>>  Paging scene

Let's first look at the parameters of the paging scenario 、 Code and log printing , As can be seen from the log SQL There is no problem with the final implementation of . Request parameters :
{
 "name": "name",
 "orderBy": "ASC"
}
sql file :
<!-- Paging data -->
<select id=&quot;selectWithPage&quot; parameterType=&quot;com.peng.java_study.api.dto.TestWithPageDTO&quot; resultMap=&quot;BaseResultMap&quot;>
 SELECT * FROM test_page_or_not tpon
 WHERE
 tpon.name LIKE CONCAT(&quot;%&quot;, #{name, jdbcType=VARCHAR}, &quot;%&quot;)
 <if test=&quot;orderBy != null and orderBy != ''&quot;>
 ORDER BY tpon.create_time ${orderBy}
 </if>
</select>
journal :
2022-02-18 16:39:09.217 DEBUG 18036 --- [nio-8080-exec-3] c.p.j.c.m.T.selectWithPage_COUNT : ==> Preparing: SELECT count(0) FROM test_page_or_not tpon WHERE tpon.name LIKE CONCAT(&quot;%&quot;, ?, &quot;%&quot;)
2022-02-18 16:39:09.218 DEBUG 18036 --- [nio-8080-exec-3] c.p.j.c.m.T.selectWithPage_COUNT : ==> Parameters: name(String)
2022-02-18 16:39:09.234 DEBUG 18036 --- [nio-8080-exec-3] c.p.j.c.m.T.selectWithPage_COUNT : <== Total: 1
2022-02-18 16:39:09.236 DEBUG 18036 --- [nio-8080-exec-3] c.p.j.c.m.T.selectWithPage : ==> Preparing: SELECT * FROM test_page_or_not tpon WHERE tpon.name LIKE CONCAT(&quot;%&quot;, ?, &quot;%&quot;) ORDER BY tpon.create_time ASC LIMIT ?
2022-02-18 16:39:09.236 DEBUG 18036 --- [nio-8080-exec-3] c.p.j.c.m.T.selectWithPage : ==> Parameters: name(String), 10(Integer)
2022-02-18 16:39:09.238 DEBUG 18036 --- [nio-8080-exec-3] c.p.j.c.m.T.selectWithPage : <== Total: 4

>>  Non paged scenes

Let's look at the parameters of the normal scenario 、 Code and log printing : As can be seen from the log  SQL  The execution of the report is wrong , because  SQL  from  ORDER BY tpon.create_time ASC  Turned into  order by ASC. Request parameters :

{
 &quot;name&quot;: &quot;name&quot;,
 &quot;orderBy&quot;: &quot;ASC&quot;
}
sql  file :

<!-- Query data without paging -->
<select id=&quot;selectWithoutPage&quot; parameterType=&quot;com.peng.java_study.api.dto.TestWithoutPageDTO&quot; resultMap=&quot;BaseResultMap&quot;>
 SELECT * FROM test_page_or_not tpon
 WHERE
 tpon.name LIKE CONCAT(&quot;%&quot;, #{name, jdbcType=VARCHAR}, &quot;%&quot;)
 <if test=&quot;orderBy != null and orderBy != ''&quot;>
 ORDER BY tpon.create_time ${orderBy}
 </if>
</select>
journal :
2022-02-18 16:52:24.684 DEBUG 18036 --- [nio-8080-exec-5] c.p.j.c.m.T.selectWithoutPage : ==> Preparing: SELECT * FROM test_page_or_not tpon WHERE tpon.name LIKE CONCAT(&quot;%&quot;, ?, &quot;%&quot;) order by ASC
2022-02-18 16:52:24.684 DEBUG 18036 --- [nio-8080-exec-5] c.p.j.c.m.T.selectWithoutPage : ==> Parameters: name(String)
2022-02-18 16:52:24.687 ERROR 18036 --- [nio-8080-exec-5] c.p.java_study.rest.test.TestController : TestController.list(TestWithoutPageDTO) exception: 

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 'ASC' at line 1
### The error may exist in file [D:\file\project\1\java-study\java-study-provider\target\classes\mapper\TestPageOrNotMapper.xml]
### The error may involve com.peng.java_study.core.mapper.TestPageOrNotMapper.selectWithoutPage-Inline
### The error occurred while setting parameters
### SQL: SELECT * FROM test_page_or_not tpon WHERE tpon.name LIKE CONCAT(&quot;%&quot;, ?, &quot;%&quot;) order by ASC
### 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 'ASC' at line 1
; bad SQL grammar []; nested exception is 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 'ASC' at line 1
 at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:235) ~[spring-jdbc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) ~[spring-jdbc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:91) ~[mybatis-spring-2.0.6.jar:2.0.6]
 at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441) ~[mybatis-spring-2.0.6.jar:2.0.6]
 at com.sun.proxy.$Proxy92.selectList(Unknown Source) ~[na:na]
 at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224) ~[mybatis-spring-2.0.6.jar:2.0.6]
 at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147) ~[mybatis-3.5.6.jar:3.5.6]
 at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80) ~[mybatis-3.5.6.jar:3.5.6]
 at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:152) ~[mybatis-3.5.6.jar:3.5.6]
 at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85) ~[mybatis-3.5.6.jar:3.5.6]
 at com.sun.proxy.$Proxy93.selectWithoutPage(Unknown Source) ~[na:na]
 at com.peng.java_study.core.manager.impl.TestPageOrNotManagerImpl.selectWithoutPage(TestPageOrNotManagerImpl.java:36) ~[classes/:na]
 at com.peng.java_study.rest.test.TestController.list(TestController.java:150) ~[classes/:na]
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
 at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
 at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.31.jar:9.0.31]
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at com.peng.java_study.config.LanguageFilter.doFilter(LanguageFilter.java:39) ~[classes/:na]
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
 at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
 at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Caused by: 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 'ASC' at line 1
 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~[mysql-connector-java-8.0.23.jar:8.0.23]
 at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.23.jar:8.0.23]
 at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) ~[mysql-connector-java-8.0.23.jar:8.0.23]
 at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:370) ~[mysql-connector-java-8.0.23.jar:8.0.23]
 at com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44) ~[HikariCP-3.4.2.jar:na]
 at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute(HikariProxyPreparedStatement.java) ~[HikariCP-3.4.2.jar:na]
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
 at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
 at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59) ~[mybatis-3.5.6.jar:3.5.6]
 at com.sun.proxy.$Proxy132.execute(Unknown Source) ~[na:na]
 at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:64) ~[mybatis-3.5.6.jar:3.5.6]
 at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79) ~[mybatis-3.5.6.jar:3.5.6]
 at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63) ~[mybatis-3.5.6.jar:3.5.6]
 at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:325) ~[mybatis-3.5.6.jar:3.5.6]
 at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) ~[mybatis-3.5.6.jar:3.5.6]
 at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109) ~[mybatis-3.5.6.jar:3.5.6]
 at com.github.pagehelper.util.ExecutorUtil.pageQuery(ExecutorUtil.java:177) ~[pagehelper-5.1.11.jar:na]
 at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:104) ~[pagehelper-5.1.11.jar:na]
 at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61) ~[mybatis-3.5.6.jar:3.5.6]
 at com.sun.proxy.$Proxy130.query(Unknown Source) ~[na:na]
 at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147) ~[mybatis-3.5.6.jar:3.5.6]
 at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) ~[mybatis-3.5.6.jar:3.5.6]
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
 at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
 at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:427) ~[mybatis-spring-2.0.6.jar:2.0.6]
 ... 62 common frames omitted

02、 analysis


Two  sql  The statement is the same as the input parameter , The only difference is that one uses paging , The other one doesn't use paging . So let's carefully analyze the error report log , lookup  sql  The reason for the change .

>>  Analyze the specific code call chain

In the Journal 99 That's ok , We found a  PageHelper  Call stack .
 at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:104) ~[pagehelper-5.1.11.jar:na]

Enter this line of code , Found that it entered the paging query . The problem is coming. :

Obviously, the object we use is  TestWithoutPageDTO.class, Called  sql  It's also  selectWithoutPage, Why do you still enter paging query ? Let's explore how :

Below 1 The red circle can help us judge the current sql Whether paging is required . According to the data shown in the figure , I should have gone  else  The branch of ( No paging ,3 Green circle No ) Of , But in the end  if  Branch ( Pagination ,2 Red circle No ).

Continue to use  skip  Method view . According to the logic of the code ,4 The number in red circle  page  Should be  null  No paging ; Now that you're paging , That explains.  page  Not for  null .

utilize  getPage  Method , After debugging, we found that , Code entered 5 The position of red circle No . This shows that , or , The parameter object has implemented  IPage  Interface , or ,supportMethodsArguments  Parameter is  true.

According to the above  sql  Parameters , We know that the parameter is  TestWithoutPageDTO.class  Example , Its class inheritance diagram is as follows . This class does not inherit or implement  IPage  Interface , So it must be  supportMethodsArguments  Parameter is  true .



Get into PageHelper Official website , see  supportMethodsArguments  Parameter Introduction : Automatically identify whether paging is required according to the properties in the parameter object .

Then view the configuration of the current project , It is found that the current project is indeed configured with this parameter , Since it can really simplify some operations , Then this attribute cannot be changed .

supportMethodsArguments: Supported by  Mapper  Interface parameters to pass paging parameters , The default value is false, The paging plug-in will retrieve the parameter values from the query method , Automatically according to the above  params  Value in the configured field , When the appropriate value is found, the page will be automatically paged . You can refer to... In the test code for the use method  com.github.pagehelper.test.basic  Under bag  ArgumentsMapTest  and  ArgumentsObjTest.
pagehelper:
 helperDialect: mysql
 reasonable: true
 supportMethodsArguments: true
 params: count=countSql
 

Continue to enter 5 Inside the red circle  getPageFromObject  Method , Let's focus on 13 Yellow circle No , This circle uses reflection to get the parameters of the parameter object orderBy attribute .

Soon , Problem found :

In order to sort, the author adds  orderBy  attribute , therefore  hasOrderBy  by  true; Get into 9 After the code block circled in red , Created a paged object , Lead to  PageHelper  The plug-in puts the  sql  Statements are treated as paging queries , Naturally ORDER BY create_time ASC  The sentence has changed .

Find out the problem , The next step is to solve it .

03、 solve


>>  Scheme 1 :

take  supportMethodsArguments  Attribute to  false , But this change is likely to affect the existing sql Query statements have a certain impact , Therefore, it is not recommended .

pagehelper:
 helperDialect: mysql
 reasonable: true
 supportMethodsArguments: false
 params: count=countSql
 

>>  Option two :

Put... In the entity class  orderBy  Change the name of another variable to , such as  orderByCreateTime , This can be avoided  PageHelper  Recognize the wrong paging parameter .
public class TestWithoutPageDTO extends BaseDTO {

 private static final long serialVersionUID = 7862152821778815456L;

 private String name;

 private String orderByCreateTime;

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }

 public String getOrderByCreateTime() {
 return orderByCreateTime;
 }

 public void setOrderByCreateTime(String orderByCreateTime) {
 this.orderByCreateTime = orderByCreateTime;
 }

 @Override
 public String toString() {
 return &quot;TestWithoutPageDTO{&quot; +
 &quot;name='&quot; + name + '\'' +
 &quot;, orderByCreateTime='&quot; + orderByCreateTime + '\'' +
 '}';
 }
}


<!-- Query data without paging -->
<select id=&quot;selectWithoutPage&quot; parameterType=&quot;com.peng.java_study.api.dto.TestWithoutPageDTO&quot; resultMap=&quot;BaseResultMap&quot;>
 SELECT * FROM test_page_or_not tpon
 WHERE
 tpon.name LIKE CONCAT(&quot;%&quot;, #{name, jdbcType=VARCHAR}, &quot;%&quot;)
 <if test=&quot;orderByCreateTime != null and orderByCreateTime != ''&quot;>
 ORDER BY tpon.create_time ${orderByCreateTime}
 </if>
</select>

The modified log , No abnormality :

2022-02-18 21:00:58.928 DEBUG 9768 --- [nio-8080-exec-1] c.p.j.c.m.T.selectWithoutPage : ==> Preparing: SELECT * FROM test_page_or_not tpon WHERE tpon.name LIKE CONCAT(&quot;%&quot;, ?, &quot;%&quot;) ORDER BY tpon.create_time ASC
2022-02-18 21:00:58.944 DEBUG 9768 --- [nio-8080-exec-1] c.p.j.c.m.T.selectWithoutPage : ==> Parameters: name(String)
2022-02-18 21:00:58.960 DEBUG 9768 --- [nio-8080-exec-1] c.p.j.c.m.T.selectWithoutPage 


LigaAI  A new generation of intelligent R & D cooperation platform  
Give Way  AI  Provide personalized services for your R & D team 、 Intelligent project collaboration experience , Simplify the complexity , Help developers focus on 、 Efficient creation !LigaAI  Will continue to share more with agile development 、 Articles related to project management ~
The author of this article :rookie0peng
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202281558519342.html