日期:2014-05-16  浏览次数:20398 次

在Web项目中使用LiquiBase实现数据库自动更新

???? 在Web项目中,我们可以通过配置servlet listener使用LiquiBase自动更新数据库。需要以下七个步骤:
1. 创建一个数据库变更日志(change log)文件。
2. 在变更日志文件内部创建一个变更集(change set)。
3. 配置JNDI数据源。
4. 在项目中加入包liquibase。
5. 配置项目中的web.xml文件。
6. 启动项目。
7. 检验数据库中的变更。


Changelog.xml:

?

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
<changeSet id="1" author="jim">

<createTable tableName="useraccount">
<column name="userid" type="bigint(20)">
<constraints primaryKey="true" />
</column>
<column name="accountstate" type="varchar(255)" />
<column name="expiredDate" type="datetime" />
<column name="username" type="varchar(255)" />
</createTable>

<addUniqueConstraint tableName="useraccount"
columnNames="username" constraintName="username" />

</changeSet>

<changeSet id="2" author="jim">

<addColumn tableName="useraccount">
<column name="gender" type="varchar(1)" value="M">
<constraints nullable="false"/>
</column>
</addColumn>
        
</changeSet>

</databaseChangeLog>

?

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>liquid</display-name>

<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

<context-param>
<param-name>LIQUIBASE_CHANGELOG</param-name>
<param-value>db.changelog.xml</param-value>
</context-param>

<context-param>
<param-name>LIQUIBASE_DATA_SOURCE</param-name>
<param-value>java:comp/env/jdbc/test</param-value>
</context-param>

<context-param>
<param-name>LIQUIBASE_FAIL_ON_ERROR</param-name>
<param-value>true</param-value>
</context-param>

<context-param>
<param-name>LIQUIBASE_CONTEXTS</param-name>
<param-value>jdbc/test</param-value>
</context-param>

<listener>
<listener-class>liquibase.servlet.LiquibaseServletListener</listener-class>
</listener>

</web-app>


???? 通过上面的web.xml文件,可以看到这里配置了一个listener,LiquibaseServletListener这个类继承了ServletContextListener,ServletContextListener 是 ServletContext 的监听者,如果 ServletContext 发生变化,如服务器启动时 ServletContext 被创建,服务器关闭时 ServletContext 将要被销毁。当服务器启动时,LiquibaseServletListener的 contextInitialized()方法被调用,这个方法会比较数据库与Changelog.xml的不同,并自动更新数据库。
?? ? 那么LiquiBase是怎么更新数据的呢?当LiquibaseServletListener第一次被调用时,会创建了两个特定的表。第一个特定于 LiquiBase 的表称为 databasechangelog,它跟踪应用到数据库的所有变更 — 有助于跟踪谁执行了数据库变更以及原因。第二个特定于 LiquiBase的表是 databasechangelock,标识出具有数据库变更锁的用户。LiquiBase通过比较databasechangelog中的记录和Changelog.xml中的changeSet,更新数据库。
?? ? liquid项目1中Changelog.xml中的只有1条changeset

<changeSet id="1" author="jim">

<createTable tableName="useraccount">
<column name="userid" type="bigint(20)">
<constraints primaryKey="true" />
</column>
<column name="accountstate" type="varchar(255)"