码迷,mamicode.com
首页 > 数据库 > 详细

maven执行SQL语句

时间:2014-12-10 02:05:22      阅读:384      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   ar   os   使用   sp   java   

前言

之所以要用maven来执行sql语句或者文件,是因为在日常开发或者测试过程中保证每次构建完之后是一个“干净”的项目呈现在面前。

怎么用

使用maven执行sql语句需要依赖sql-maven-plugin这个插件,下面就直接从项目pom文件的配置入手进行简要介绍:

    <build>
        <finalName>buglife-data-access</finalName>
        <plugins>
            <!--添加sql-maven-plugin插件-->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sql-maven-plugin</artifactId>
                <version>1.5</version>
                <!---因为该实例中用到的数据库是mysql,故需依赖于mysql连接驱动-->
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.27</version>
                    </dependency>
                </dependencies>
                <!--为该插件配置数据库连接信息-->
                <configuration>
                    <driver>com.mysql.jdbc.Driver</driver>
                    <url>jdbc:mysql://localhost:3306</url>
                    <username>root</username>
                    <password>root</password>
                </configuration>
                <executions>
                    <!--创建数据库-->
                    <execution>
                        <id>create-db</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <!--设置自动提交事务-->
                            <autocommit>true</autocommit>
                            <srcFiles>
                                <srcFile>src/main/resources/create_db.sql</srcFile>
                            </srcFiles>
                        </configuration>
                    </execution>
                    <!--创建数据表-->
                    <execution>
                        <id>create-table</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <autocommit>true</autocommit>
                            <srcFiles>
                                <srcFile>src/main/resources/create_table.sql</srcFile>
                            </srcFiles>
                        </configuration>
                    </execution>
                    <!--插入数据-->
                    <execution>
                        <id>insert-data</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <orderFile>ascending</orderFile>
                            <fileset>
                                <basedir>${basedir}</basedir>
                                <includes>
                                    <include>src/main/resources/init_data.sql</include>
                                </includes>
                            </fileset>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


以上pom文件中皆是执行的sql文件而没选择执行sql命令,原因是为了改SQL语句的时候可以集中在一块修改,而不去轻易的修改项目文件(POM).若要执行sql命令则可以使用类似于下面的语句替代<srcFiles/>

<sqlCommand>create database `webutil`;</sqlCommand>

当然另一点需要注意的是<phase>即指定执行在maven生命周期的哪个环节,关于maven生命周期的说明可以参考http://my.oschina.net/crazyharry/blog/353978





maven执行SQL语句

标签:des   blog   http   io   ar   os   使用   sp   java   

原文地址:http://my.oschina.net/crazyharry/blog/354369

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!