执行 mvn sql:execute 时出错 [英] Error when executing mvn sql:execute
问题描述
我想让Maven执行sql文件,它生成的数据库schema以后会用到我的程序中.但它不起作用,可能是由DELIMITER"引起的.当我执行 'mvn sql:execute' 时,它会打印
[ERROR] 执行失败:DELIMITER $$
这是我的 pom.xml 的一部分
<插件><groupId>org.codehaus.mojo</groupId><artifactId>sql-maven-plugin</artifactId><version>1.5</version><依赖项><依赖><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.21</version></依赖></依赖项><执行><执行><id>init-schema</id><目标><目标>网站</目标></目标><阶段>部署</阶段></执行></执行><配置><driver>com.mysql.jdbc.Driver</driver><用户名>root</用户名><password>root</password><url>jdbc:mysql://127.0.0.1:3306/?useUnicode=true&characterEncoding=UTF-8</url><autocommit>false</autocommit><srcFile>src/main/sql/jellyjolly-schema.sql</srcFile></srcFiles></配置></插件>
这是我的sql文件的一部分
DELIMITER $$使用`jellyjolly_schema`$$如果存在`jellyjolly_schema`.`delete_user` $$,则删除触发器使用`jellyjolly_schema`$$创建触发器 delete_user删除后开 jj_users每行开始##删除属于用户的帖子DELETE FROM jj_blog_posts WHERE author_user_id=OLD.user_id;结尾$$
是否因为 Maven 调用的 MySQL Java 连接器不支持关键字DELIMITER".那么我该如何解决呢?
看了大家的评论,我认为最好的解决方案是 此处接受.也就是说,只使用一个分隔符(最好是默认值,;
)并使用 sql-maven-plugin
配置来要求分隔符出现在它自己的行上 实际上是一个分隔符.
(归功于@Zheka)
您的触发器将如下所示:
CREATE TRIGGER delete_user删除后开 jj_users每行开始##删除属于用户的帖子DELETE FROM jj_blog_posts WHERE author_user_id=OLD.user_id;# 这个分隔符被忽略结尾;# 这个分隔符被识别
I want Maven to execute the sql file, and the database schema it generates will be later used in my program. But it doesn't work, maybe caused by 'DELIMITER'. When I execute 'mvn sql:execute', it prints that
[ERROR] Failed to execute: DELIMITER $$
Here is part of my pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>1.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>init-schema</id>
<goals>
<goal>site</goal>
</goals>
<phase>deploy</phase>
</execution>
</executions>
<configuration>
<driver>com.mysql.jdbc.Driver</driver>
<username>root</username>
<password>root</password>
<url>jdbc:mysql://127.0.0.1:3306/?useUnicode=true&characterEncoding=UTF-8</url>
<autocommit>false</autocommit>
<srcFiles>
<srcFile>src/main/sql/jellyjolly-schema.sql</srcFile>
</srcFiles>
</configuration>
</plugin>
Here is part of my sql file
DELIMITER $$
USE `jellyjolly_schema`$$
DROP TRIGGER IF EXISTS `jellyjolly_schema`.`delete_user` $$
USE `jellyjolly_schema`$$
CREATE TRIGGER delete_user
AFTER DELETE
ON jj_users
FOR EACH ROW
BEGIN
## delete the posts that belong to the user
DELETE FROM jj_blog_posts WHERE author_user_id=OLD.user_id;
END
$$
Is it because the keyword 'DELIMITER' is not supported by MySQL Java connector which is invoked by Maven. So how can I solve it?
After reading your comments, I believe the best solution is the one accepted here. That is, use only one delimiter (preferably the default, ;
) and use the sql-maven-plugin
configuration to require the delimiter to occur on it's own line to actually be a delimiter.
<configuration>
<delimiterType>row</delimiterType>
</configuration>
(credit to @Zheka)
Your trigger would then look like:
CREATE TRIGGER delete_user
AFTER DELETE
ON jj_users
FOR EACH ROW
BEGIN
## delete the posts that belong to the user
DELETE FROM jj_blog_posts WHERE author_user_id=OLD.user_id; # this delimiter is ignored
END
; # this delimiter is recognized
这篇关于执行 mvn sql:execute 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!