使用mybatis的项目,从MySQL数据库换成了Oracle数据库,驱动都修改完了,用MySQL时正常,换成Oracle报错
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用mybatis的项目,从MySQL数据库换成了Oracle数据库,驱动都修改完了,用MySQL时正常,换成Oracle报错相关的知识,希望对你有一定的参考价值。
org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: java.sql.SQLException: ORA-00933: SQL 命令未正确结束
### The error may involve front.product.loadHotProductShowInSuperMenu-Inline
### The error occurred while setting parameters
### Cause: java.sql.SQLException: ORA-00933: SQL 命令未正确结束
; bad SQL grammar []; nested exception is java.sql.SQLException: ORA-00933: SQL 命令未正确结束
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:98)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:65)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:345)
at $Proxy52.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:193)
at net.jeeshop.core.dao.BaseDao.selectList(BaseDao.java:108)
at net.jeeshop.services.front.product.dao.impl.ProductDaoImpl.loadHotProductShowInSuperMenu(ProductDaoImpl.java:92)
只有出现sql语法问题才会有这个提示。追问
这是报错开始的地方,帮我看看哪不对,谢谢!
因为你的sql是拼接,所以很难辨认哪个地方会有问题。我建议你一个一个if去掉调试下,这样就能很快定位问题在哪个地方,
追问试试看
参考技术B oracle 与mysql的 sql语句是有区别的。是不是因为这个原因追问也许是吧,我已经把所有的limit换成rownum,而且能通过,我把开始报错的那部分贴出来看看。
它上面的内容都能加载出来,但是从这开始就报上面的错了。
and t.name like CONCAT('%','$name','%') 改成 and t.name like '%$name%'
oracle 里 CONCAT('%','$name','%') 报错
好的我试试看
本回答被提问者和网友采纳maven项目重构-使用了mybatis generator插件
1.在worksapce下,dos窗口输入spring init -g=com.briup.apps -a=poll3 -d=mysql,mybatis,web poll3
2.下载依赖,cd到poll3,执行mvn install,会报错,因为没有配置数据库的初始数据
解决方法:eclipse导入poll3,打开/resources/application.properties,输入
spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/pro2.0 spring.datasource.username=root spring.datasource.password=root mybatis.mapper-locations= classpath:/mapper/**/*.xml
mybatis.mapper-locations=classpath:/mapper/**/*.xml 这个配置是为了让dao层找到mapper.xml文件的资源位置,和mapper文件夹下的mapper.xml映射
3.修改pox.xml,内容如下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.braup.apps</groupId> <artifactId>poll</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <!-- swagger API文档 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
4.三层架构
5.src/main/resources下新建mapper文件夹,用来保存mapper.xml文件
6.在config包下创建MybatisConfig.java,用来配置mybatis
package com.briup.apps.poll.config; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Configuration; @Configuration @MapperScan("com.briup.apps.poll") public class MybatisConfig { }
本来应该在DemoApplication.java中配置,已进行标记
package com.braup.apps.poll; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.braup.apps.poll.dao") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
7.进行测试,mvn spring-boot:run,启动成功表明热部署,三层架构,swagger部署成功
8.编写数据库访问层代码
8.1.手写(时间长,费劲,代码冗余)
8. 2.使用mybatis generator(自动生成bean和dao以及mapper.xml文件)。
mybatis不是框架,是一个插件,不属于项目的一部分,产生的代码是项目的一部分。
8.3配置mabatis generator框架
8.3.1 pom.xml添加依赖
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> </plugin>
8.3.2添加配置文件src/main/resources下添加------------1.根据那些表产生bean. 2.产生的pojo放在哪里. 3.产生的mapper接口放在哪里 4.产生的mapper实现类放在哪里 ........
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <!-- mybatis-generator的核心配置文件 --> <generatorConfiguration> <classPathEntry location="/Users/lichunyu/springboot/repository/mysql/mysql-connector-java/5.1.45/mysql-connector-java-5.1.45.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/poll2.0" userId="root" password="root"> </jdbcConnection> <!--指定生成的类型为java类型,避免数据库中number等类型字段 --> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--自动生成的实体的存放包路径 --> <javaModelGenerator targetPackage="com.briup.apps.poll.bean" targetProject="./src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--自动生成的*Mapper.xml文件存放路径 --> <sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!--自动生成的*Mapper.java存放路径 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.briup.apps.poll.dao" targetProject="./src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 映射配置 --> <table tableName="poll_survy" domainObjectName="Survy" ></table> <!-- <table tableName="poll_school" domainObjectName="School" ></table> <table tableName="poll_grade" domainObjectName="Grade" ></table> <table tableName="poll_clazz" domainObjectName="Clazz" ></table> <table tableName="poll_course" domainObjectName="Course" ></table> <table tableName="poll_grade_course" domainObjectName="GradeCourse" ></table> <table tableName="poll_user" domainObjectName="User" ></table> <table tableName="poll_option" domainObjectName="Option" ></table> <table tableName="poll_question" domainObjectName="Question" ></table> <table tableName="poll_questionnaire" domainObjectName="Questionnaire" ></table> <table tableName="poll_questionnaire_question" domainObjectName="QuestionnaireQuestion" ></table> --> </context> </generatorConfiguration>
8.3.3开始工作-( 要关闭项目)
mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate
以上是关于使用mybatis的项目,从MySQL数据库换成了Oracle数据库,驱动都修改完了,用MySQL时正常,换成Oracle报错的主要内容,如果未能解决你的问题,请参考以下文章