我希望Maven执行sql文件,它生成的数据库模式稍后将在我的程序中使用。但它不起作用,可能是由'DELIMITER'引起的。当我执行'mvn sql:execute'时,它打印出
[ERROR] Failed to execute: DELIMITER $$
这是我的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>
这是我的sql文件的一部分
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
$$
是否因为Maven调用的MySQL Java连接器不支持关键字“DELIMITER”。那我怎么解决呢?
答案 0 :(得分:1)
在阅读您的评论后,我认为最佳解决方案是accepted here。也就是说,只使用一个分隔符(最好是默认值;
)并使用sql-maven-plugin
配置要求分隔符在其自己的行上发生实际上是分隔符。
<configuration>
<delimiterType>row</delimiterType>
</configuration>
(归功于@ Zheka)
您的触发器将如下所示:
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
答案 1 :(得分:0)
您可以在sql-maven-plugin
配置中指定新的分隔符,如下所示:
<configuration>
<delimiter>$$</delimiter>
</configuration>
有关此page的此配置参数的更多信息。
答案 2 :(得分:0)
如果您不想使用<delimiterType>row</delimiterType>
另一种方式来做,除了肖恩的答案是摆脱DELIMITER $$和$$并在插件中使用分号以外的分隔符。
首先更改插件中的分隔符,例如
<delimiter>;;</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;;
或者您可以使用其他内容作为插件中的分隔符并更改触发器;相应