BD错误集锦8——在集成Spring MVC + MyBtis编写mapper文件时需要注意格式 You have an error in your SQL syntax

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1090027

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

SQL中的外键约束

外键约束用于表示两张表中的指标连接关系。外键约束的作用主要有以下三点: 1.确保子表中的某个字段(外键)只能引用父表中的有效记录2.主表中的列被删除时,子表中的关联列也会被删除3.主表中的列更新时,子表中的关联元素也会被更新 子表中的元素指向主表 以下是一个外键约束的实例展示

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

关于数据埋点,你需要了解这些基本知识

产品汪每天都在和数据打交道,你知道数据来自哪里吗? 移动app端内的用户行为数据大多来自埋点,了解一些埋点知识,能和数据分析师、技术侃大山,参与到前期的数据采集,更重要是让最终的埋点数据能为我所用,否则可怜巴巴等上几个月是常有的事。   埋点类型 根据埋点方式,可以区分为: 手动埋点半自动埋点全自动埋点 秉承“任何事物都有两面性”的道理:自动程度高的,能解决通用统计,便于统一化管理,但个性化定