Liquibase使用

1. Liquibase使用背景:

现实中数据库没法保证一成不变,如如有改动,测试和开发都有些许不便,改动的工做多是重复的而且手工改动也可能形成数据不一致。 在这种状况下,使用Liquibase将有利于咱们更好地管理数据。java

2. Liquibase具有以下特性:

  • 不依赖于特定的数据库,目前支持包括Oracle/Sql Server/DB2/MySql/Sybase/PostgreSQL/Caché等12种数据库,这样在数据库的部署和升级环节可帮助应用系统支持多数据库。
  • 提供数据库比较功能,比较结果保存在XML中,基于该XML你可用Liquibase轻松部署或升级数据库。
  • 以XML存储数据库变化,其中以做者和ID惟一标识一个变化(ChangSet),支持数据库变化的合并,所以支持多开发人员同时工做。
  • 在数据库中保存数据库修改历史(DatabaseChangeHistory),在数据库升级时自动跳过已应用的变化(ChangSet)。
  • 提供变化应用的回滚功能,可按时间、数量或标签(tag)回滚已应用的变化。经过这种方式,开发人员可轻易的还原数据库在任什么时候间点的状态。
  • 可生成数据库修改文档(HTML格式)
  • 提供数据重构的独立的IDE和Eclipse插件

3. Liquibase优势:

  1. 将全部数据库的变化保存在xml文件中
  2. 不损害现有数据库,只须要少许配置,便能与spring项目集成。
  3. ChangeSet的ID+修改ChangeSet的做者标示+包含ChangeSet的文件名标识,能够避免多人协同开发的冲突

4. 工程结构

这里写图片描述

5. 须要引入jar包

这里写图片描述

6. Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	
	<!-- spring 配置 begin -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-context.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- spring 配置 end -->
	
</web-app>

7. Spring-context.xml

<!-- 引用资源文件 -->
	<context:property-placeholder location="classpath:jdbc.properties" />

<!-- 配置数据源 dbcp -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	
<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
		<property name="dataSource" ref="dataSource" />
		<property name="changeLog" value="classpath:Changelog.xml" />
	</bean>

8. Changelog.xml

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

	<!-- 执行sql -->
	<changeSet id="sql_file" author="anniweiya">
		<sqlFile path="anniweiya.sql" />
	</changeSet>

	<!-- 新建表 -->
	<changeSet id="create_table" author="anniweiya">
		<createTable tableName="t_anniweiya_test">
			<column name="id" type="int">
				<constraints primaryKey="true" nullable="false" />
			</column>
			<column name="user" type="varchar(255)">
				<constraints nullable="false" />
			</column>
			<column name="updatetime" type="date">
				<constraints nullable="false" />
			</column>
		</createTable>
	</changeSet>

	<!-- 新增列 -->
	<changeSet id="add_column" author="anniweiya">
		<addColumn tableName="t_anniweiya_test">
			<column name="password" type="varchar(255)">
				<constraints nullable="false" />
			</column>
		</addColumn>
	</changeSet>

	<!-- 修改列类型 -->
	<changeSet id="modify_datatype" author="anniweiya">
		<modifyDataType tableName="t_anniweiya_test" columnName="password" newDataType="varchar(55)" />
	</changeSet>

	<!-- 删除列 -->
	<changeSet id="drop_column" author="anniweiya">
		<dropColumn tableName="t_anniweiya_test" columnName="updatetime" />
	</changeSet>

	<!-- 修更名 -->
	<changeSet id="modify_name" author="anniweiya">
		<renameColumn tableName="t_anniweiya_test" oldColumnName="password" newColumnName="password1" columnDataType="varchar(255)" />
	</changeSet>

	<!-- 执行sql语句 -->
	<changeSet id="t_anniweiya_test_sql" author="anniweiya">
		<preConditions onFail="MARK_RAN">
			<not>
				<columnExists tableName="t_anniweiya_test" columnName="fcreator_id" />
			</not>
		</preConditions>
		<sql>ALTER TABLE `t_anniweiya_test` Add COLUMN fcreator_id int not null AFTER `user` </sql>
	</changeSet>


</databaseChangeLog>

9. Main入口函数

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UpDataBase {
	
public static void main(String[] args) {
		
		System.out.println("start");
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "spring-context.xml"});
		System.out.println("ok");
	}
	
}
相关文章
相关标签/搜索