Liquibase使用
来源:互联网 发布:冯大辉 知乎 匿名用户 编辑:程序博客网 时间:2024/06/05 05:57
1. Liquibase使用背景:
现实中数据库无法保证一成不变,如若有改动,测试和开发都有些许不便,改动的工作可能是重复的并且手工改动也可能造成数据不一致。
在这种情况下,使用Liquibase将有利于我们更好地管理数据。
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优点:
- 将所有数据库的变化保存在xml文件中
- 不损害现有数据库,只需要少量配置,便能与spring项目集成。
- 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"); }}
阅读全文
0 0
- Liquibase使用
- 如何使用Liquibase
- liquibase
- 在项目中使用Liquibase
- 使用 LiquiBase 管理数据库变更
- 使用LiquiBase管理数据库的迁移
- (转)在Web项目中使用LiquiBase实现数据库自动更新
- (转)在Web项目中使用LiquiBase实现数据库自动更新
- Liquibase+spring
- liquibase安装
- Liquibase 接触
- Liquibase 筆記
- 如何将liquibase部署到tomcat服务器上(使用postgresql数据库)
- Liquibase --- Database Change Management
- LiquiBase实战总结
- LiquiBase预判断
- spring boot 配置liquibase
- liquibase入门实战
- 仿拉勾网动画效果-鼠标进入图片的方向滑动特效
- Android Selector的使用
- Vue错误提示
- 单链表
- Android 技术重温系列 (10)- UI 组件
- Liquibase使用
- 分数化为埃及分数 跨度为5
- poj 3691 DAN repair
- C语言-一个简单的计算器。。。。。
- 多线程排序+快速排序
- java实时改变Jlabel的值
- Spark单机模式搭建
- Spring-Boot拦截器
- mysql-proxy数据库中间件架构(58沈剑)