spring+liquibase开发实例

    这两天想玩liquibase,去网上看了看,大多是那一篇文章抄来抄去的,一点不实用,而且没说咋跟spring结合,自己研究了研究,现在把研究结果贴出来。

    首先声明,我实在spring3.0.6+hibernate3.6.8+struts2.2.3+maven2的基础上加入的liquibase框架,不知道这个框架怎么搭的请参见http://doujiang327.iteye.com/blog/1238767

 

    1.修改pom文件,加入liquibase所需包

      

<dependency>
  	<groupId>org.liquibase</groupId>
  	<artifactId>liquibase-core</artifactId>
  	<version>1.9.1</version>
</dependency>

 

    不能用2.1以及以上版本,因为它们已经修改了对spring支持的类。

 

    2. 修改web.xml的配置文件,加入对liquidbase的支持

      

<!-- 配置spring的配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:resources/spring/applicationContext-liquibase.xml,
			classpath:resources/spring/applicationContext.xml
		</param-value>
	</context-param>

 

    3. applicationContext-liquibase.xml

   

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
       default-lazy-init="false">
       
    <bean id="liquibase" class="liquibase.spring.SpringLiquibase">
        <property name="dataSource" ref="dataSource" />
        <property name="changeLog" value="classpath:resources/liquibase/changelog.xml" />        
    </bean>

</beans>

 

    dataSource请参见applicationContext.xml:http://doujiang327.iteye.com/blog/1238767

 

    4. changelog.xml

   

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.8"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.8 http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.8.xsd">


	<changeSet author="zhangxin" id="baseframe-ddl-1">
		<createTable tableName="user">
			<column name="user_id" type="VARCHAR(36)">
				<constraints nullable="false" primaryKey="true" />
			</column>
			<column name="user_login" type="VARCHAR(36)" />
			<column name="user_name" type="VARCHAR(36)" />
			<column name="user_password" type="VARCHAR(12)" />
			<column name="user_createID" type="VARCHAR(36)" />
			<column name="user_createTime" type="DATETIME" />
			<column name="user_updateID" type="VARCHAR(36)" />
			<column name="user_updateTime" type="DATETIME" />
		</createTable>
		
		<insert tableName="user">
            <column name="user_id" valueNumeric="00001" />
            <column name="user_login" value="admin" />
            <column name="user_name" value="管理员" />
            <column name="user_password" value="admin" />
        </insert>
	</changeSet>
</databaseChangeLog>

 

    运行吧,liquibase就这么简单。

      

你可能感兴趣的:(spring,入门,实例,指南,liquibase)