本文主要是介绍BD错误集锦8——在集成Spring MVC + MyBtis编写mapper文件时需要注意格式 You have an error in your SQL syntax,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
报错的文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yuan.dao.YuanUserDao"><!-- 获取所有用户 --><select id="findAll" resultType="com.yuan.model.YuanUser">selecta.id as "id",a.name as "name"a.password as "password"from mytable as a</select>
</mapper>
select语句第二行少了","
正确的如下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yuan.dao.YuanUserDao"><!-- 获取所有用户 --><select id="findAll" resultType="com.yuan.model.YuanUser">selecta.id as "id",a.name as "name",a.password as "password"from mytable as a</select>
</mapper>
异常信息如下:
Type Exception ReportMessage Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: Description The server encountered an unexpected condition that prevented it from fulfilling the request.Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. 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 'a.password as "password"from yuan_user as a' at line 4
### The error may exist in file [F:\Java\ideaIU-2018.3.3\SSM-test\target\222\WEB-INF\classes\mapper\YuanUserMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select a.id as "id", a.name as "name" a.password as "password" from yuan_user as a
### 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 'a.password as "password"from yuan_user as a' at line 4
; 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 'a.password as "password"from yuan_user as a' at line 4org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)javax.servlet.http.HttpServlet.service(HttpServlet.java:635)org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)javax.servlet.http.HttpServlet.service(HttpServlet.java:742)org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)Root Cause
org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. 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 'a.password as "password"from yuan_user as a' at line 4
### The error may exist in file [F:\Java\ideaIU-2018.3.3\SSM-test\target\222\WEB-INF\classes\mapper\YuanUserMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select a.id as "id", a.name as "name" a.password as "password" from yuan_user as a
### 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 'a.password as "password"from yuan_user as a' at line 4
; 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 'a.password as "password"from yuan_user as a' at line 4org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)com.sun.proxy.$Proxy15.selectList(Unknown Source)org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:139)org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:76)org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)com.sun.proxy.$Proxy18.findAll(Unknown Source)com.yuan.service.impl.YuanUserServiceImpl.findAll(YuanUserServiceImpl.java:18)com.yuan.controller.YuanTestController.hello(YuanTestController.java:20)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)java.lang.reflect.Method.invoke(Method.java:497)org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)javax.servlet.http.HttpServlet.service(HttpServlet.java:635)org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)javax.servlet.http.HttpServlet.service(HttpServlet.java:742)org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)Root 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 'a.password as "password"from yuan_user as a' at line 4sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)java.lang.reflect.Constructor.newInstance(Constructor.java:422)com.mysql.jdbc.Util.handleNewInstance(Util.java:411)com.mysql.jdbc.Util.getInstance(Util.java:386)com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625)com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1362)com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.execute(NewProxyPreparedStatement.java:67)org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:63)org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:326)org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)java.lang.reflect.Method.invoke(Method.java:497)org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)com.sun.proxy.$Proxy15.selectList(Unknown Source)org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:139)org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:76)org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)com.sun.proxy.$Proxy18.findAll(Unknown Source)com.yuan.service.impl.YuanUserServiceImpl.findAll(YuanUserServiceImpl.java:18)com.yuan.controller.YuanTestController.hello(YuanTestController.java:20)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)java.lang.reflect.Method.invoke(Method.java:497)org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)javax.servlet.http.HttpServlet.service(HttpServlet.java:635)org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)javax.servlet.http.HttpServlet.service(HttpServlet.java:742)org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)Note The full stack trace of the root cause is available in the server logs.
这篇关于BD错误集锦8——在集成Spring MVC + MyBtis编写mapper文件时需要注意格式 You have an error in your SQL syntax的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!