thymeleaf用法使⽤mybatis-generator代码⽣成器时出现sql语句异常(这⾥的主要问题为数
据。。。
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 ‘.department’ at line 1
The error may exist in com/practice/springbootpracticemybatis/dao/DepartmentMapper.java (best guess)
The error may involve defaultParameterMap
The error occurred while setting parameters
SQL: SELECT did,dname,dpassword FROM interview…department
Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual th
at corresponds to your MySQL server version for the right syntax to use near ‘.department’ 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 ‘.department’ at line 1
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:234)
at org.springframework.jdbc.anslate(AbstractFallbackSQLExceptionTranslator.java:72)
batis.anslateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
batis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
at com.sun.proxy.$Proxy64.selectList(Unknown Source)
batis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)
at org.apache.ibatis.uteForMany(MapperMethod.java:139)
at org.apache.ibatis.ute(MapperMethod.java:76)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy83.selectAll(Unknown Source)
at com.practice.stInsert(SpringBootPracticeMybatisApplicationTests.java:33) at java.base/flect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/flect.Method.invoke(Method.java:564)
at org.del.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.del.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.del.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at st.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at st.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at st.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at st.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at st.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at st.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at st.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at st.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at st.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at st.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at cution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at cution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at cution.junit.JUnitStarter.main(JUnitStarter.java:70)
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 ‘.department’ at line 1
sql.ateSQLException(SQLError.java:120)
sql.ateSQLException(SQLError.java:97)
sql.anslateException(SQLExceptionsMapping.java:122)
sql.cj.uteInternal(ClientPreparedStatement.java:974)
sql.cj.ute(ClientPreparedStatement.java:391)
at com.alibaba.druid.ute(DruidPooledPreparedStatement.java:497)
at org.utor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:63)
at org.utor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
at org.utor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
at org.utor.BaseExecutor.queryFromDatabase(BaseExecutor.java:326)
at org.utor.BaseExecutor.query(BaseExecutor.java:156)
at org.utor.CachingExecutor.query(CachingExecutor.java:109)
at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:143)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61)
at com.sun.proxy.
Proxy97.query(UnknownSource)atorg.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.j 148)atorg.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:
141)atjava.base/flect.NativeMethodAccessorImpl.invoke0(NativeMethod)atjava.base/fle 62)atjava.base/flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: 43)atjava.base/flect.Method.invoke(Method.java:batis.spring.SqlSessionT emplate SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
… 37 more
解决办法
在配置⽂件中,不要使⽤数据库加表的形式配置,直接给每个表配置
<!-- 这个是原来的配置-->
<!-- 配置需要指定⽣成的数据库和表,% 代表所有表 -->
<table catalog="interview"tableName="%">
<!-- mysql 配置 -->
<generatedKey column="id"sqlStatement="Mysql"identity="true"/>
</table>
<!-- 这个是现在的配置-->
<table tableName="department"domainObjectName="Department">
</table>
<table tableName="student"domainObjectName="Student">
</table>
虽然⼀个⼀个表设置是⿇烦了点,但是稳啊
这⾥再附带上我的所有配置吧,毕竟是成功了
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-////DTD MyBatis Generator Configuration 1.0//EN"
"/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 引⼊数据库连接配置 -->
<properties resource="jdbc.properties"/>
<context id="Mysql"targetRuntime="MyBatis3Simple"defaultModelType="flat">
<property name="beginningDelimiter"value="`"/>
<property name="endingDelimiter"value="`"/>
<!-- 配置 tk.mybatis 插件 -->
<plugin type="tk.ator.MapperPlugin">
<property name="mappers"value="com.practice.batis.MyMapper"/>
</plugin>
<!-- 配置数据库连接 -->
<jdbcConnection
driverClass="${jdbc.driverClass}"
connectionURL="${tionURL}"
userId="${jdbc.username}"
password="${jdbc.password}">
</jdbcConnection>
<!-- 配置实体类存放路径 -->
<javaModelGenerator targetPackage="com.ity"targetProject="src/main/java"/>
<!-- 配置 XML 存放路径 -->
<sqlMapGenerator targetPackage="mapper"targetProject="src/main/resources/generator"/>
<!-- 配置 DAO 存放路径 -->
<javaClientGenerator
targetPackage="com.practice.springbootpracticemybatis.dao"
targetProject="src/main/java"
type="XMLMAPPER"/>
<!-- 配置需要指定⽣成的数据库和表,% 代表所有表 -->
<table tableName="department"domainObjectName="Department">
</table>
<table tableName="student"domainObjectName="Student">
</table>
</context>
</generatorConfiguration>
jdbc.properties
jdbc.sql.cj.jdbc.Driver
jdbc.username=root
jdbc.password=root
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"xsi="/2001/XMLSchema-instance"
schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<version>2.1.8.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<groupId>com.practice</groupId>
<artifactId>spring-boot-practice-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-practice-mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>localrepository.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>batis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId&ator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/l</configurationFile>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论